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
WSClientConfigurableOptions
Optional configuration object. See options below.
DefaultLogger
Optional custom logger. Defaults to the built-in console logger.
Constructor options (WSClientConfigurableOptions)
APICredentials[]
Array of API credential objects, each with
apiKey, apiSecret, and apiPass. Providing credentials enables authentication for private channels and WS API calls.string
default:"prod"
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)
boolean
default:"false"
Set to
true to connect to OKX demo trading WebSocket endpoints.boolean
default:"false"
Disable the ping/pong heartbeat mechanism. Not recommended — OKX requires heartbeats to keep connections alive.
number
How often (ms) to check that the connection is alive by sending a ping.
number
How long (ms) to wait for a pong response before treating the connection as dead and reconnecting.
number
Delay (ms) before spawning a new connection after a disconnect.
RestClientOptions
Options forwarded to the underlying
RestClient used for time synchronisation.object
Additional options passed directly to the underlying
isomorphic-ws WebSocket constructor, such as protocols or agent.string
Override the WebSocket URL entirely.
(message: string, secret: string) => Promise<string>
Provide a custom HMAC signing function for authentication. Useful for using Node’s native
crypto.createHmac.Methods
Connection management
connectAll()
connectPublic(businessEndpoint?)
boolean
When
true, connects to the business WebSocket endpoint instead of the standard public one.connectPrivate(businessEndpoint?)
boolean
When
true, connects to the business WebSocket endpoint.connectWSAPI()
Subscriptions
subscribe(wsEvents, isPrivateTopic?)
WsChannelSubUnSubRequestArg | WsChannelSubUnSubRequestArg[]
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).boolean
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?)
WsChannelSubUnSubRequestArg | WsChannelSubUnSubRequestArg[]
The channel(s) to unsubscribe from. Must match the arguments used when subscribing.
boolean
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.
WsKey
The WebSocket key identifying which connection to use. Use
getMarketWsKey('private') or getMarketWsKey('business') to obtain the correct key.string
The WS API operation name (e.g.
'order', 'cancel-order', 'batch-orders').object
Parameters for the operation. The shape is typed based on the operation.
WSAPIRequestFlags
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.
string
'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:
Handling update events
All channel data arrives via the update event. Use the wsKey and arg.channel properties to route messages:

