Overview
WebsocketClient is the primary class for subscribing to OKX WebSocket channels and for making WebSocket API (WS API) requests. It extends BaseWebsocketClient and emits typed events for connection lifecycle and market data updates.
Constructor
Optional configuration object. See options below.
Optional custom logger. Defaults to the built-in console logger.
Constructor options (WSClientConfigurableOptions)
Array of API credential objects, each with
apiKey, apiSecret, and apiPass. Providing credentials enables authentication for private channels and WS API calls.The API group to connect to:
'prod'— OKX global (wss://ws.okx.com)'EEA'— European Economic Area (wss://wspap.okx.com)'US'— United States (wss://wsaws.okx.com)
Set to
true to connect to OKX demo trading WebSocket endpoints.Disable the ping/pong heartbeat mechanism. Not recommended — OKX requires heartbeats to keep connections alive.
How often (ms) to check that the connection is alive by sending a ping.
How long (ms) to wait for a pong response before treating the connection as dead and reconnecting.
Delay (ms) before spawning a new connection after a disconnect.
Options forwarded to the underlying
RestClient used for time synchronisation.Additional options passed directly to the underlying
isomorphic-ws WebSocket constructor, such as protocols or agent.Override the WebSocket URL entirely.
Provide a custom HMAC signing function for authentication. Useful for using Node’s native
crypto.createHmac.Methods
Connection management
connectAll()
connectPublic(businessEndpoint?)
When
true, connects to the business WebSocket endpoint instead of the standard public one.connectPrivate(businessEndpoint?)
When
true, connects to the business WebSocket endpoint.connectWSAPI()
Subscriptions
subscribe(wsEvents, isPrivateTopic?)
A single channel subscription object or an array of them. Each object must include a
channel property, plus any channel-specific parameters (e.g. instId).Optional hint to force the library to treat a channel as private. The library auto-detects most private channels; only set this if a channel is not yet recognised.
unsubscribe(wsEvents, isPrivateTopic?)
The channel(s) to unsubscribe from. Must match the arguments used when subscribing.
Optional private topic hint, same as in
subscribe().WebSocket API
sendWSAPIRequest(wsKey, operation, params, requestFlags?)
WebsocketAPIClient.
The method ensures the connection is active and authenticated before sending, then returns a promise that resolves or rejects when the matching response arrives.
The WebSocket key identifying which connection to use. Use
getMarketWsKey('private') or getMarketWsKey('business') to obtain the correct key.The WS API operation name (e.g.
'order', 'cancel-order', 'batch-orders').Parameters for the operation. The shape is typed based on the operation.
Optional flags to control request behaviour, such as
expTime and authIsOptional.getMarketWsKey(type)
WsKey for the active market (prod / EEA / US) and the given connection type. Used when calling sendWSAPIRequest() directly.
'private' for order management and account data, or 'business' for spread trading and other business-line WS API operations.Utility
setTimeOffsetMs(offsetMs)
Events
Listen to events using theon() method:
| Event | Payload | Description |
|---|---|---|
open | { wsKey: WsKey } | A WebSocket connection has been opened. |
reconnect | { wsKey: WsKey } | The client is attempting to reconnect after a disconnect. |
reconnected | { wsKey: WsKey } | The client has successfully reconnected. |
authenticated | { wsKey: WsKey } | A private connection has been successfully authenticated. |
update | WsDataEvent & { wsKey: WsKey } | A data update was received on a subscribed channel. |
response | object & { wsKey: WsKey } | A response to a subscribe, unsubscribe, or WS API request was received. |
exception | object & { wsKey: WsKey } | An error event was received from the server, or an internal error occurred. |
close | { wsKey: WsKey } | A WebSocket connection has closed. |
Handling update events
All channel data arrives via the update event. Use the wsKey and arg.channel properties to route messages:

