Skip to main content

WebSocket Overview

Futures WebSocket provides real-time pushes for market quotes, order book, K-lines including the 1-second interval, trades, mark price, and private channel updates for orders, balances, and positions.

Connection Endpoints

TypeEndpoint
Public channelws://<host>:9021/ws
Private channelws://<host>:9021/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",
"kline@BTC_USDT,1s",
"BTC_USDT@markPrice@1s"
]
}

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 TypeFormatExampleAccessFutures Specific
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,1sPublicYes, supports 1s interval
Tradetrade@{symbol}trade@BTC_USDTPublic-
Mark price (3s){symbol}@markPriceBTC_USDT@markPricePublicYes
Mark price (1s){symbol}@markPrice@1sBTC_USDT@markPrice@1sPublicYes
OrderorderorderPrivate-
BalancebalancebalancePrivate-
PositionpositionpositionPrivateYes

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:9021/ws');

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

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);
}
};

setInterval(() => ws.send(JSON.stringify({ op: 'ping' })), 30000);