Skip to main content

Overview

The WebSocketClient 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 using On() 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)

Each authenticated Subscribe re-validates the handshake HMAC headers against a 30-second window measured from Connect. Subscribe to positions, order events and transactions immediately after Connect; a later Subscribe fails with Invalid HMAC authentication even though REST calls succeed. Auto-reconnect re-signs, so restored subscriptions are unaffected. If you must subscribe late, call Disconnect() then Connect() first.

SubscriptionOptions

Unsubscribing

The server has no generic unsubscribe event, so ws.Unsubscribe cannot succeed (it returns unsubscribe failed: ack timeout after 5s). Use the channel-specific mechanisms instead:
To stop everything, call ws.Disconnect().

Auto-reconnect

When WithAutoReconnect(true) is set (the default), the client automatically reconnects after a disconnection using exponential backoff with jitter (capped at 60 seconds):
  1. The connection drops (network issue, server restart, etc.)
  2. The client waits with exponential backoff
  3. The client establishes a new connection
  4. 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().

Complete example

The ws.Disconnect() call cleans up the connection and clears all subscriptions. Always defer it after a successful Connect().