Skip to main content

WebSocketClient setup

The WebSocketClient 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.
Subscriptions replace previous ones. To listen to both CLOB (by slug) and AMM (by address) markets, include both marketSlugs and marketAddresses in a single subscribe_market_prices call. To stop receiving a market, re-send subscribe_market_prices with the remaining set. The only server-side unsubscribe is unsubscribe_market_lifecycle, sent through subscribe(); ws.unsubscribe() emits a generic unsubscribe event the server does not handle, so it rejects after its 5 second ack timeout.
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 require hmacCredentials passed to the constructor. The SDK signs the WebSocket handshake automatically.
The server re-validates the handshake signature on every authenticated subscribe call and accepts lmts-timestamp values at most 30 seconds old. Issue subscribe_positions, subscribe_order_events and subscribe_transactions immediately after connect(); to subscribe later, call await ws.disconnect(); await ws.connect(); first so the handshake is re-signed.

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, and EXECUTION, the terminal frame for FAK/FOK orders (status FILLED, PARTIALLY_FILLED or KILLED); on this frame price and remainingSize are strings in raw 6-decimal units.
  • source: 'SETTLEMENT' — on-chain settlement results for the taker order and each matched maker order.
For settlement events, 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 same subscribe_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:
Use raw event logging during development to discover event shapes and debug subscription issues. Remove it before deploying to production.

Full example

A complete script that subscribes to market prices and positions: