Skip to main content

WebSocket Overview

Spot WebSocket provides real-time pushes for market quotes, order book, K-lines, trade records, and private channels for order and balance updates.

Connection Endpoints​

TypeEndpoint
Public channelws://<host>:9004/ws
Private channelws://<host>:9004/ws (send auth after connection)

Client Request Format​

All client requests use the unified {op, args} structure.

Subscribe​

{
"op": "subscribe",
"args": ["ticker@BTC_USDT", "depth@BTC_USDT,20"]
}

Unsubscribe​

{
"op": "unsubscribe",
"args": ["ticker@BTC_USDT"]
}

Authentication (Private Channels)​

Login uses the same HmacSHA256 signing mechanism as the REST API. For details, see Common Module · WebSocket Interface.

Heartbeat​

{ "op": "ping" }

Server Response Format​

Success Response​

{
"op": "subscribe",
"success": true,
"args": ["ticker@BTC_USDT"]
}

Error Response​

{
"op": "subscribe",
"success": false,
"msg": "invalid channel format"
}

Authentication Response​

{ "op": "auth", "success": true }
{ "op": "auth", "success": false, "msg": "token is expired" }

Pong Response​

{ "op": "pong" }

Push Message Format​

All push messages use the following format:

{
"ch": "<channel>",
"d": { ... }
}
FieldDescription
chChannel name
dData payload

Channel Summary​

Channel TypeFormatExampleAccess
Tickerticker@{symbol}ticker@BTC_USDTPublic
Order book snapshotdepth@{symbol},{level}depth@BTC_USDT,20Public
Order book deltadepth_update@{symbol}depth_update@BTC_USDTPublic
K-linekline@{symbol},{interval}kline@BTC_USDT,1mPublic
Tradetrade@{symbol}trade@BTC_USDTPublic
Mark price (3s){symbol}@markPriceBTC_USDT@markPricePublic
Mark price (1s){symbol}@markPrice@1sBTC_USDT@markPrice@1sPublic
OrderorderorderPrivate
BalancebalancebalancePrivate

Error Messages​

Error MessageDescription
invalid message formatInvalid message format
invalid channel formatInvalid channel format
unknown operationUnknown operation
authentication requiredAuthentication required
invalid tokenInvalid token
max subscriptions reachedSubscription limit reached

Quick Start Example​

const ws = new WebSocket('ws://host:9004/ws');

ws.onopen = () => {
ws.send(JSON.stringify({
op: 'subscribe',
args: ['ticker@BTC_USDT', 'depth@BTC_USDT,20']
}));
};

ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.ch) {
console.log('Channel:', msg.ch, 'Data:', msg.d);
} else if (msg.op) {
console.log('Response:', msg);
}
};

// Heartbeat, recommended every 30 seconds
setInterval(() => ws.send(JSON.stringify({ op: 'ping' })), 30000);