websocket — WebSocket client

The O2WebSocket class provides a low-level WebSocket client with automatic reconnection and subscription management. For most use cases, use the streaming methods on O2Client instead, which manage the WebSocket connection automatically.

See also

The O2 Exchange WebSocket protocol is documented at https://docs.o2.app. The high-level streaming methods are documented in O2Client — High-level client.

O2WebSocket

class o2_sdk.websocket.O2WebSocket(config)[source]

Async WebSocket client for O2 Exchange real-time data.

Features:

  • Auto-reconnect with exponential backoff (1s to 60s).

  • Subscription persistence — subscriptions are re-sent on reconnect.

  • Per-channel queues — messages are dispatched to typed queues by action type.

Parameters:

config (NetworkConfig) – Network configuration with WebSocket URL.

async connect()[source]

Connect to the WebSocket endpoint.

Returns:

The connected client (for chaining).

Return type:

O2WebSocket

async disconnect()[source]

Disconnect from the WebSocket and signal all subscription iterators to stop.

Subscription methods

Each subscription method returns an AsyncIterator that yields typed update objects. The first message may be a full snapshot (for depth subscriptions), followed by incremental updates.

async O2WebSocket.stream_depth(market_id, wire_precision='10')[source]

Subscribe to order book depth updates.

Parameters:
  • market_id (str) – The market ID (hex string).

  • wire_precision (str) – Wire-format precision value (default "10", corresponding to user-facing precision level 1).

Returns:

Async iterator of depth updates.

Return type:

AsyncIterator[DepthUpdate]

async O2WebSocket.stream_orders(identities)[source]

Subscribe to order updates for the given identities.

Parameters:

identities (list[dict]) – List of identity dicts (e.g., [{"ContractId": "0x..."}]).

Returns:

Async iterator of order updates.

Return type:

AsyncIterator[OrderUpdate]

async O2WebSocket.stream_trades(market_id)[source]

Subscribe to trade updates for a market.

Parameters:

market_id (str) – The market ID (hex string).

Returns:

Async iterator of trade updates.

Return type:

AsyncIterator[TradeUpdate]

async O2WebSocket.stream_balances(identities)[source]

Subscribe to balance updates for the given identities.

Parameters:

identities (list[dict]) – List of identity dicts.

Returns:

Async iterator of balance updates.

Return type:

AsyncIterator[BalanceUpdate]

async O2WebSocket.stream_nonce(identities)[source]

Subscribe to nonce updates for the given identities.

Parameters:

identities (list[dict]) – List of identity dicts.

Returns:

Async iterator of nonce updates.

Return type:

AsyncIterator[NonceUpdate]

Unsubscribe methods

async O2WebSocket.unsubscribe_depth(market_id)[source]

Unsubscribe from depth updates for a market.

async O2WebSocket.unsubscribe_orders()[source]

Unsubscribe from all order updates.

async O2WebSocket.unsubscribe_trades(market_id)[source]

Unsubscribe from trade updates for a market.

async O2WebSocket.unsubscribe_balances(identities)[source]

Unsubscribe from balance updates.

async O2WebSocket.unsubscribe_nonce(identities)[source]

Unsubscribe from nonce updates.