Skip to main content
The WebsocketClient extends Node.js EventEmitter. All activity — incoming data, connection lifecycle, and errors — is surfaced through named events. Attach listeners with .on() before calling subscribe().

Event summary

update

Fires for every incoming data message from a subscribed channel.
Payload type:
  • arg.channel — the channel name that produced this update (e.g. 'tickers', 'trades', 'account').
  • data — the array of records in this update, shaped according to the channel.
  • wsKey — identifies the underlying connection (e.g. 'prodPublic', 'prodPrivate').
Routing updates by channel:

response

Fires when the server acknowledges a subscribe, unsubscribe, or login request. Also fires for WS API operation results.
Payload type:
A successful subscription confirmation looks like:

open

Fires each time a WebSocket connection is first established (including after reconnections).
Payload:

reconnect

Fires when the client detects a dropped connection and is about to attempt reconnection. The connection is not yet restored at this point.
Payload:

reconnected

Fires after a reconnection attempt succeeds and the connection is re-established. At this point the client has also re-authenticated (if required) and resubscribed all active topics.
Payload:

exception

Fires when an error message is received from the server, when authentication fails, or when a WS API operation returns a non-zero error code.
Payload type:
Common error codes:

authenticated

Fires after a private connection successfully authenticates. Useful for performing actions that depend on auth being complete.

TypeScript types

The full set of event types is expressed via WSClientEventMap:
The WebsocketClient.on() method is fully typed. TypeScript will infer the correct payload shape based on the event name:

Complete handler example

This is the canonical event listener setup from the public channel example:

Best practices

  • Register all listeners before subscribing. Subscribing may open the connection immediately, and events can arrive before you attach handlers.
  • Route in update by data.arg.channel. A single client can hold many subscriptions; switching on the channel name keeps your handler readable.
  • Always handle exception. Unhandled exceptions are logged but not thrown. Ignoring auth errors will result in a reconnect loop with invalid credentials.
  • Use response for confirmation, not update. The response event confirms that a subscription or auth request was accepted. Channel data only flows on update.
  • Use reconnected to verify state. After a reconnection all subscriptions are automatically restored, but if your application maintains derived state (e.g. an in-memory order book) you may need to reset it using the snapshot that arrives after resubscription.