Overview
The SDK provides WebSocket support for real-time quote streaming. The WebSocket client handles connection management, automatic reconnection with exponential backoff, and ping/pong keep-alive. There are two ways to use WebSocket:- High-level: Use
streamQuotes/streamQuotesByTickerconvenience methods onMakerClient - Low-level: Use
WSClientdirectly with event-based message handling
Enabling WebSocket
Before streaming quotes, enable the WebSocket connection:enableWebSocket method returns a WSClient instance.
High-Level API
streamQuotesByTicker / stream_quotes_by_ticker
The simplest way to stream quotes. Automatically discovers routes and starts streaming.
streamQuotes / stream_quotes
Stream quotes for a specific route (asset IDs and layers).
streamQuotesForAllRoutes / stream_quotes_for_all_routes
Stream quotes for all available routes between two tickers simultaneously.
getAvailableRoutes / get_available_routes
Discover available routes for a pair before streaming.
Low-Level WSClient API
For full control, use theWSClient directly.
Connection
Events
Subscribe to events usingon / off:
| Event | Data | Description |
|---|---|---|
connected | — | Connection established |
disconnected | — | Connection closed |
reconnecting | attempt: number | Attempting reconnection |
quoteResponse / quote_response | QuoteResponse | New quote received |
connectionEstablished / connection_established | connection data | Server confirmed connection |
pong | PongResponse | Pong received |
error | Error | An error occurred |
maxReconnectExceeded / max_reconnect_exceeded | — | Max reconnection attempts reached |
Requesting Quotes
Ping / Keep-Alive
The WSClient automatically pings the server at the configured interval. You can also ping manually:Configuration
The WSClient accepts configuration options:| Option | Default | Description |
|---|---|---|
maxReconnectAttempts / max_reconnect_attempts | 5 | Maximum reconnection attempts |
reconnectDelay / reconnect_delay | 1000ms / 1.0s | Base delay between reconnections (exponential backoff) |
pingInterval / ping_interval | 30000ms / 30.0s | Interval between keep-alive pings |
delay * 2^attempt.
WebSocket Protocol
The WebSocket uses a JSON message protocol:Message Types
| Action | Direction | Description |
|---|---|---|
quote_request | Client -> Server | Request a price quote |
quote_response | Server -> Client | Quote update |
ping | Client -> Server | Keep-alive ping |
pong | Server -> Client | Keep-alive response |
connection_established | Server -> Client | Connection confirmed |
error | Server -> Client | Error message |
QuoteResponse Fields
| Field | Type | Description |
|---|---|---|
from_asset | string | Source asset ID |
to_asset | string | Destination asset ID |
from_amount | number | Source amount |
to_amount | number | Destination amount |
price | number | Exchange rate |
rfq_id | string | Request-for-quote ID (use to create orders) |
price_precision | number | Price decimal precision |
timestamp | number | Server timestamp |
expires_at | number | Quote expiry timestamp |
fee | Fee | Fee breakdown |
Best Practices
Handle disconnections gracefully
Handle disconnections gracefully
Subscribe to
disconnected and reconnecting events. The WSClient automatically reconnects with exponential backoff, but you should handle the case where max attempts are exceeded.Use the high-level API when possible
Use the high-level API when possible
streamQuotesByTicker handles route discovery, connection management, and quote routing for you. Only use the low-level WSClient when you need custom control.Unsubscribe when done
Unsubscribe when done
Always call the unsubscribe function returned by
streamQuotes / streamQuotesByTicker when you no longer need quotes. This prevents memory leaks and unnecessary network traffic.Use rfq_id from quotes to create orders
Use rfq_id from quotes to create orders
The
rfq_id in each quote response is what you pass to createSwapOrder. Use the most recent quote to ensure the rate is still valid.