WebSocketClient setup
TheWebSocketClient connects to the Limitless Exchange WebSocket server for real-time market data, position updates, order lifecycle events, and transaction notifications.
Samples use top-level
await (ESM). In CommonJS wrap them in an async function.Constructor options
Public subscriptions (market prices) do not require authentication. Authenticated subscriptions (positions, order events, transactions) require
hmacCredentials — see Authentication to generate a token.Public subscriptions
Market prices
Subscribe to real-time price updates for one or more markets. No authentication required.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.Authenticated subscriptions
These subscriptions requirehmacCredentials passed to the constructor. The SDK signs the WebSocket handshake automatically.
Positions
Subscribe to real-time updates when your positions change:subscribe_positions requires marketSlugs and/or marketAddresses; a call without them is ignored by the server (no ack, no error). It has replace semantics like subscribe_market_prices.Order events
Subscribe to CLOB order lifecycle and settlement events for your account:orderEvent uses source as a discriminator:
source: 'OME'— off-chain order state changes: placement, update, cancellation, andEXECUTION, the terminal frame for FAK/FOK orders (statusFILLED, PARTIALLY_FILLED or KILLED); on this framepriceandremainingSizeare strings in raw 6-decimal units.source: 'SETTLEMENT'— on-chain settlement results for the taker order and each matched maker order.
clientOrderId is the id of the recipient’s own orderId. If only one side of a trade supplied a clientOrderId, only that side’s event includes it. Counterparty order ids inside takerOrderId or makerMatches[] are not resolved to client ids.
Transactions
Subscribe to transaction confirmations:Events
orderbookUpdate (CLOB markets)
Fired once per slug right after you subscribe, then whenever a CLOB market orderbook changes. Contains the full current orderbook.newPriceData (AMM markets)
Fired when AMM market prices update.The first
newPriceData after subscribe_market_prices is a snapshot whose updatedPrices is a single object (with collateralDecimals) rather than an array; the sample above normalises it with Array.isArray.oraclePriceData
Fired when the oracle backing a subscribed market publishes a new price (e.g. Chainlink, Pyth). Delivered on the samesubscribe_market_prices channel as orderbookUpdate and newPriceData, so no separate subscription is required.
OraclePriceData fields: marketAddress (nullable), marketSlug, timestamp (unix ms), value (number).
positions
Fired when any of your positions change (fill, cancel, resolution).tx
Fired on transaction events related to your account.Type definitions
Connection management
Disconnect handling
Listen for disconnections and clean up resources:When
autoReconnect is enabled the client reconnects on its own. WebSocketClient exposes no reconnect event; its connection events are connect, disconnect and error. On SDK 1.1.0 an automatic reconnect does not restore any subscription, public or authenticated, and authenticated channels are rejected with Invalid HMAC authentication because the original handshake signature is replayed. To survive a drop, listen for disconnect, call await ws.disconnect(); await ws.connect(); and re-issue your subscribe() calls. The SDK main branch re-signs the handshake and re-subscribes automatically; that fix is not yet published to npm.Graceful shutdown
Clean up the WebSocket connection on process exit:Debugging
WebSocketClient has no catch-all listener in 1.1.0 (there is no onAny). Log the events you care about one by one; system carries server-side messages: