Overview
TheWebSocketClient provides real-time streaming of orderbook updates, prices, order events, transactions, and market lifecycle data over a persistent WebSocket connection. It supports automatic reconnection with exponential backoff and event-driven message handling. The WebSocket client is standalone and does not require an HttpClient.
Setup
Connecting
Connection states
Check the current state with
ws.State() or ws.IsConnected().
Event handlers
Register handlers usingOn() for persistent handlers or Once() for one-time handlers. Both return a handler ID that you can use with Off() to unregister. The raw handler receives the message as json.RawMessage.
Typed event handlers
The SDK provides convenience methods that automatically deserialize events into typed structs:Available events
Position updates are delivered as a raw
positions event — register with ws.On("positions", func(data json.RawMessage) { ... }).
Subscribing to channels
After connecting, subscribe to a channel to receive its events. CLOB orderbook updates are delivered through the market-price subscription:The server answers every
subscribe_market_prices call, first or repeat, with one orderbookUpdate per CLOB slug carrying the full current book, including books with empty bids and asks. You do not need a REST call to seed local state. See Initial snapshot.Public channels (no authentication required)
Authenticated channels (require HMAC credentials or a legacy API key)
SubscriptionOptions
Unsubscribing
The server has no generic unsubscribe event, sows.Unsubscribe cannot succeed (it returns unsubscribe failed: ack timeout after 5s). Use the channel-specific mechanisms instead:
ws.Disconnect().
Auto-reconnect
WhenWithAutoReconnect(true) is set (the default), the client automatically reconnects after a disconnection using exponential backoff with jitter (capped at 60 seconds):
- The connection drops (network issue, server restart, etc.)
- The client waits with exponential backoff
- The client establishes a new connection
- The client re-signs the handshake and re-emits every subscription it recorded
Subscriptions are restored automatically after a reconnect. Subscribe to all market slugs in one
Subscribe call: the server replaces your market rooms on every subscribe_market_prices, so separate calls only keep the last set. You can observe reconnects through ws.State().