WebSocket client for O2 Exchange real-time data streams.

Use via O2Client.streamDepth, O2Client.streamOrders, etc. for the simplest interface, or create a standalone instance for advanced use cases.

import { O2WebSocket, TESTNET } from "@o2exchange/sdk";

const ws = new O2WebSocket({ config: TESTNET });
await ws.connect();
for await (const update of ws.streamDepth(marketId, "10")) {
console.log(update);
}
ws.disconnect();

Constructors

Methods

  • Connect to the WebSocket server.

    Returns Promise<void>

  • Disconnect from the WebSocket server.

    Signals all active generators to stop before closing the connection, so consumers are unblocked immediately even if the close handshake is slow.

    Returns void

  • Check if permanently terminated (max reconnect attempts exhausted or disconnected).

    Returns boolean

  • Stream WebSocket connection lifecycle events.

    Yields ConnectionEvent objects whenever the connection state changes. Use this to detect reconnects and re-sync state from the REST API — messages received during the disconnect window are lost.

    Returns AsyncGenerator<ConnectionEvent>

    for await (const event of ws.streamLifecycle()) {
    if (event.state === "reconnected") {
    const balances = await client.getBalances(account);
    // ... rebuild local state ...
    } else if (event.state === "closed") {
    break;
    }
    }
  • Subscribe to trade updates. Returns an AsyncGenerator yielding TradeUpdate messages.

    Parameters

    • marketId: string

    Returns AsyncGenerator<TradeUpdate>

  • Unsubscribe from depth updates for a market.

    Parameters

    • marketId: string

    Returns void

  • Unsubscribe from order updates.

    Returns void

  • Unsubscribe from trade updates for a market.

    Parameters

    • marketId: string

    Returns void