Websocket is a new HTML5 Protocol. It achieves full-duplex data transmission between the client and the server, allowing data to be transferred effectively in both directions. With just only one handshake, the connection between the client and the server is established. The server will then be able to push data to the client according to preset rules. Its advantages include:
The WebSocket request header for data transmission between client and server is approximately 2 bytes only
Either the client or server can initiate a data transmission
As there is no need to create and delete TCP connection repeatedly, it saves resources for both bandwidth and server
The returned data will be binary compressed except the heartbeat data (the user needs to decompress through Gzip algorithm)
Command Format
Heartbeat
Every once in a while, the server will send a PING message. The client needs to reply to the PONG message, otherwise the server will close the connection.
ping
{
"ping": 1535975085052
}
pong
{
"pong": 1535975085052
}
Subscription Full Depth
Payload
{
"channel":"market_btcusdt_depth_step0",
"ts":1506584998239,
"tick":{ //A maximum of 30 orders are returned
"asks":[ //asks
[10000.19,0.93],
[10001.21,0.2],
[10002.22,0.34]
],
"buys":[ //buy
[9999.53,0.93],
[9998.2,0.2],
[9997.19,0.21]
]
}
}
Subscription message structure
{
"event":"sub",
"params":{
"channel":"market_$symbol_depth_step0", // $symbol E.g. btcusdt
"cb_id":"1" // Business ID is not required
}
}
{
"event":"req",
"params":{
"channel":"market_$symbol_trade_ticker", // $symbol E.g. btcusdt
"cb_id":"1" // Business ID is not required
}
}
Request History Trade
Payload
{
"event_rep":"rep","channel":"market_$symbol_kline_5min",
"ts":1506584998239,//request time
"data":[ //up to 300
{
"id":1506602880,//kline start time
"amount":123.1221,
"vol":1212.12211,
"open":2233.22,//open price
"close":1221.11,//close price
"high":22322.22,//high price
"low":2321.22//low price
},
{
"id":1506602880,//kline start time
"amount":123.1221,
"vol":1212.12211,
"open":2233.22,//open price
"close":1221.11,//close price
"high":22322.22,//high price
"low":2321.22//low price
}
]
}
Subscription message structure
{
"event":"req",
"params":{
"channel":"market_$symbol_kline_[1min/5min/15min/30min/60min/1day/1week/1month]",
"cb_id":"1",
"endIdx":"1506602880", //Return pageSize data before endIdx Not required
"pageSize":100 // Not required
}
}
{
"event":"sub",
"params":{
"channel":"market_$symbol_ticker", // $symbol E.g. btcusdt
"cb_id":"1" // Business ID is not required
}
}
Subscription Market Tickers
Payload
{
"channel":"market_$symbol_kline_1min", //1min is for 1 minute
"ts":1506584998239,//request time
"tick":{
"id":1506602880,//kline start time
"vol":1212.12211,
"open":2233.22,//open price
"close":1221.11,//close price
"high":22322.22,//high price
"low":2321.22//low price
}
}
Subscription message structure
{
"event":"sub",
"params":{
"channel":"market_$symbol_kline_[1min/5min/15min/30min/60min/1day/1week/1month]", // $symbol E.g. btcusdt
"cb_id":"1" // Business ID is not required
}
}