Skip to main content
The BAGS Restream SDK provides a powerful, full-fledged, and resilient WebSocket client for connecting to the BAGS real-time data stream. It is designed to give developers a simple yet highly configurable way to subscribe to a wide range of on-chain Solana DeFi events, including token swaps, price updates, new token launches, and maker actions. This guide provides a general overview of the SDK’s core concepts, features, and common usage patterns.

Prerequisites

Before starting, make sure you have:

Key Features

The ReStream client is built to handle the complexities of real-time data streaming so you can focus on your application’s logic.
  • Automatic Reconnection: The client automatically handles dropped connections with a configurable exponential backoff strategy, ensuring your application remains connected and resilient.
  • Simple Subscription Management: An intuitive API allows you to subscribe and unsubscribe from data channels, with built-in support for wildcard subscriptions.
  • Built-in Protobuf Decoding: Raw binary messages are automatically decoded into structured, type-safe TypeScript objects, removing the need for manual parsing.
  • Type-Safe Handlers: Leverage TypeScript generics to ensure your event handlers receive data with the correct types, improving code quality and reducing runtime errors.
  • Resilient and Configurable: The client is highly configurable, allowing you to adjust connection timeouts, ping intervals, subscription limits, and reconnection behavior.

Getting Started: A Basic Subscription Script

Here is a complete script that demonstrates the fundamental workflow: connecting to the service, subscribing to BAGS launches, handling incoming data, and disconnecting gracefully.
stream-launches.ts

Subscription Methods

The SDK offers several ways to subscribe to channels, from generic to highly specific convenience methods.

Generic Methods

These methods provide maximum flexibility for subscribing to any channel.
  • subscribeTopic<T>(topic, subject, handler): The most common method. You provide the topic and subject separately.
  • subscribe<T>(channel, handler): Use this when you have the full channel string.

Convenience Methods

For common use cases, the SDK provides helper methods that build the channel string for you.
  • subscribeBagsLaunches(handler): Subscribes to new token launches on Bags.
streram-launches-convenient.tsx

Handling Client Lifecycle Events

To build a robust application, you should monitor the client’s connection state. The RestreamClient is an EventEmitter and provides several key events.

Advanced: Debugging Events

Some events are emitted by the ReStream client for observability or custom logic:
ReStream client doesn’t log anything by design, so make sure to subscribe to error events for observability, you can then tie that with your own logger.

Advanced: Custom Decoders

If Bags introduces a new topic without a built-in decoder in the SDK, or if you need to override existing decoding logic, you can register your own decoder function. The function receives a raw Buffer and should return the parsed data.