Skip to main content
WebSocket
Online Support avatar
Written by Online Support
Updated over a week ago

WebSocket

General

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

We strongly recommend developers to use WebSocket API to retrieve market data and order book depth.

Ws information

Command Format

event

channel

description

sub

market_$symbol_depth_step0

Subscribe depth

unsub

market_$symbol_depth_step0

Unsubscribe depth

sub

market_$symbol_trade_ticker

Subscribe to real-time trade

unsub

market_$symbol_trade_ticker

Unsubscribe real-time trade

sub

market_$symbol_ticker

Subscribe to 24h market data

unsub

market_$symbol_ticker

Unsubscribe 24h market data

sub

market_$symbol_kline_1min

Subscribe to 1min k-line information

req

market_$symbol_kline_1month

Request 1 month historical bar record

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

1

{

2

"ping": 1535975085052

3

}

Copied!

  • pong

1

{

2

"pong": 1535975085052

3

}

Copied!

Subscription Full Depth

  • Subscription message structure

1

{

2

"event":"sub",

3

"params":{

4

"channel":"market_$symbol_depth_step0", // $symbol E.g. btcusdt

5

"cb_id":"1" // Business ID is not required

6

}

7

}

Copied!

  • Payload

1

{

2

"channel":"market_btcusdt_depth_step0",

3

"ts":1506584998239,

4

"tick":{ //A maximum of 30 orders are returned

5

"asks":[ //asks

6

[10000.19,0.93],

7

[10001.21,0.2],

8

[10002.22,0.34]

9

],

10

"buys":[ //buy

11

[9999.53,0.93],

12

[9998.2,0.2],

13

[9997.19,0.21]

14

]

15

}

16

}

Copied!

Subscription Real Time Trade

  • Subscription message structure

1

{

2

"event":"sub",

3

"params":{

4

"channel":"market_$symbol_trade_ticker", // $symbol E.g. btcusdt

5

"cb_id":"1" // Business ID is not required

6

}

7

}

Copied!

  • Payload

1

{

2

"channel":"market_$symbol_trade_ticker",

3

"ts":1506584998239,//request time

4

"tick":{

5

"data":[

6

{

7

"side":"buy",//buy,sell

8

"price":32.233,

9

"vol":232,

10

"amount":323,

11

"ds":'2017-09-10 23:12:21'

12

}

13

]

14

}

15

}

Copied!

Subscription Kline Market

  • Subscription message structure

1

{

2

"event":"sub",

3

"params":{

4

"channel":"market_$symbol_kline_[1min/5min/15min/30min/60min/1day/1week/1month]", // $symbol E.g. btcusdt

5

"cb_id":"1" // Business ID is not required

6

}

7

}

Copied!

  • Payload

1

{

2

"channel":"market_$symbol_kline_1min", //1min is for 1 minute

3

"ts":1506584998239,//request time

4

"tick":{

5

"id":1506602880,//kline start time

6

"vol":1212.12211,

7

"open":2233.22,//open price

8

"close":1221.11,//close price

9

"high":22322.22,//high price

10

"low":2321.22//low price

11

}

12

}

Copied!

Subscription Market Tickers

  • Subscription message structure

1

{

2

"event":"sub",

3

"params":{

4

"channel":"market_$symbol_ticker", // $symbol E.g. btcusdt

5

"cb_id":"1" // Business ID is not required

6

}

7

}

Copied!

  • Payload

1

{

2

"channel":"market_$symbol_ticker",

3

"ts":1506584998239,//request time

4

"tick":{

5

"amount":123.1221,

6

"vol":1212.12211,

7

"open":2233.22,//open price

8

"close":1221.11,//close price

9

"high":22322.22,//high price

10

"low":2321.22,//low price

11

"rose":-0.2922,//increase

12

}

13

}

Copied!

Request Kline History Data

  • Subscription message structure

1

{

2

"event":"req",

3

"params":{

4

"channel":"market_$symbol_kline_[1min/5min/15min/30min/60min/1day/1week/1month]",

5

"cb_id":"1",

6

"endIdx":"1506602880", //Return pageSize data before endIdx Not required

7

"pageSize":100 // Not required

8

}

9

}

Copied!

  • Payload

1

{

2

"event_rep":"rep","channel":"market_$symbol_kline_5min",

3

"ts":1506584998239,//request time

4

"data":[ //up to 300

5

{

6

"id":1506602880,//kline start time

7

"amount":123.1221,

8

"vol":1212.12211,

9

"open":2233.22,//open price

10

"close":1221.11,//close price

11

"high":22322.22,//high price

12

"low":2321.22//low price

13

},

14

{

15

"id":1506602880,//kline start time

16

"amount":123.1221,

17

"vol":1212.12211,

18

"open":2233.22,//open price

19

"close":1221.11,//close price

20

"high":22322.22,//high price

21

"low":2321.22//low price

22

}

23

]

24

}

Copied!

Request History Trade

  • Subscription message structure

1

{

2

"event":"req",

3

"params":{

4

"channel":"market_$symbol_trade_ticker", // $symbol E.g. btcusdt

5

"cb_id":"1" // Business ID is not required

6

}

7

}

Copied!

  • Payload

1

{

2

"event_rep":"rep","channel":"market_$symbol_trade_ticker",

3

"ts":1506584998239,"status":"ok",

4

"data":[

5

{

6

"side":"buy",//buy,sell

7

"price":32.233,//trade price

8

"vol":232,//trade vol

9

"amount":323//trade amount

10

},

11

{

12

"side":"buy",//buy,sell

13

"price":32.233,//trade price

14

"vol":232,//trade vol

15

"amount":323//trade amount

16

}

17

]

18

}

Did this answer your question?