Skip to main content

Overview

The WebSocketClient provides real-time streaming of orderbook updates and price data over a persistent WebSocket connection. It supports automatic reconnection and event-driven message handling via decorators.

Setup

Event Handlers

Register handlers for specific events using the @ws_client.on() decorator:

Available Events

Subscribing to Markets

After connecting, subscribe to specific markets to receive updates:
The subscribe() method takes two arguments:
You can subscribe to multiple markets at once by passing a list of slugs in marketSlugs.

Authenticated Subscriptions

The subscribe_positions and subscribe_order_events channels are per-account and require authentication. Pass hmac_credentials to the config — the SDK signs the WebSocket handshake automatically (no manual headers needed). This is the same flow as the TypeScript SDK; see Authentication to generate a token.

Positions

Subscribe to real-time updates when your positions change:

Order Events

Subscribe to your CLOB order lifecycle and settlement events. This channel is per-account and takes no payload:
Authenticated subscribe() calls raise ValueError if no hmac_credentials is set. Auth failures surface on the exception event. See the WebSocket Events reference for the full orderEvent payload shapes (source OME vs SETTLEMENT, and type values).

Auto-Reconnect

When auto_reconnect is enabled (the default), the client automatically reconnects after a disconnection:
  1. The connection drops (network issue, server restart, etc.)
  2. The client waits reconnect_delay seconds
  3. A new connection is established
  4. The connect event fires again, so your subscription logic re-executes
Place your subscribe() calls inside the connect handler to ensure subscriptions are restored after every reconnection.

Complete Example

await ws_client.connect() returns once the connection is established — it does not block. Keep the event loop alive afterwards (e.g. await asyncio.Event().wait()), otherwise the program exits before any events arrive. Register your @ws_client.on(...) handlers and subscribe() inside the connect handler before calling connect().