WebsocketClient includes a built-in heartbeat and reconnection system. When a connection drops the client tears it down, respawns it, re-authenticates if needed, and resubscribes all active topics — without any intervention required from your application code.
Heartbeat mechanism
The client sends periodicping frames to the server and expects a pong response within a configurable timeout window. This detects stale connections that have silently dropped — a common issue with long-lived TCP connections traversing NAT or load-balancers.
| Option | Description |
|---|---|
pingInterval | How often to send a ping frame |
pongTimeout | How long to wait for a pong reply before declaring the connection dead |
reconnectTimeout | Delay before opening a new connection after a drop is detected |
disableHeartbeat | Set true to disable the ping/pong mechanism entirely. Not recommended. |
Reconnection lifecycle
When a connection drop is detected (pong timeout, socket error, or a server-initiated close) the following sequence runs automatically:Emit 'reconnect'
The
reconnect event fires, notifying your application that reconnection is starting.Teardown
The existing socket is cleaned up. Any in-flight requests tied to this connection are rejected.
Wait for reconnectTimeout
The client waits for
reconnectTimeout milliseconds before attempting to reconnect, avoiding hammering the server.Re-authenticate (private connections)
If the connection is a private or business endpoint, a fresh signed login request is sent automatically. A new HMAC signature is generated for each auth attempt.
Resubscribe
All topics that were active before the drop are resubscribed in batch. Topics removed via
unsubscribe() are not resubscribed.Server-initiated reconnects
OKX may send anotice event with code 64008 when a server is about to restart for an upgrade. The client handles this automatically:
- The notice is detected inside the message parser.
executeReconnectableClose()is queued immediately.- The same teardown → respawn → re-auth → resubscribe flow runs as for a dropped connection.
- The notice event is also emitted on
updateso your application can log it.
The wsKey concept
The client manages multiple independent WebSocket connections, each identified by aWsKey string. Different connections are used for different endpoint types and markets.
WsKey based on the channel name and the configured market. The wsKey field is included in every emitted event so you can identify the source connection in your handlers.
WebSocket endpoints by market and type
| Market | Type | Live URL |
|---|---|---|
Global (prod) | public | wss://ws.okx.com:8443/ws/v5/public |
Global (prod) | private | wss://ws.okx.com:8443/ws/v5/private |
Global (prod) | business | wss://ws.okx.com:8443/ws/v5/business |
| EEA | public | wss://wseea.okx.com:8443/ws/v5/public |
| EEA | private | wss://wseea.okx.com:8443/ws/v5/private |
| US | public | wss://wsus.okx.com:8443/ws/v5/public |
| US | private | wss://wsus.okx.com:8443/ws/v5/private |
wspap / wseeapap / wsuspap host variants. Set demoTrading: true in the constructor and the correct URLs are selected automatically.
Automatic vs manual reconnection
In most cases you should rely on automatic reconnection and simply handle thereconnected event to reset any derived state:
-
Authentication fails repeatedly — close the connection to stop the loop:
-
You want to change subscriptions after reconnection — use
subscribe()andunsubscribe()in thereconnectedhandler.

