Websocket Introduction#
WebSocket is a new HTML5 protocol that achieves full-duplex data transmission between the client and server, allowing data to be transferred effectively in both directions. A connection between the client and server can be established with just one handshake. The server will then be able to push data to the client according to preset rules. Its advantages include:
- The WebSocket request header size for data transmission between client and server is only 2 bytes.
- Either the client or server can initiate data transmission.
- There's no need to repeatedly create and delete TCP connections, saving resources on bandwidth and server.
Connect#
Connection limit: 3 requests per second (based on API KEY)
When subscribing to a private channel, use the address of the private service
Request limit
The total number of 'subscribe'/'unsubscribe'/'login' requests per connection is limited to 480 times per hour. If there’s a network problem, the system will automatically disable the connection. The connection will break automatically if the subscription is not established or data has not been pushed for more than 30 seconds. To keep the connection stable:
- Set a timer of N seconds whenever a response message is received, where N is less than 30.
- If the timer is triggered, which means that no new message is received within N seconds, send the String 'ping'.
- Expect a 'pong' as a response. If the response message is not received within N seconds, please raise an error or reconnect.
Connection count limit
The limit will be set at 30 WebSocket connections per specific WebSocket channel per API KEY. Each WebSocket connection is identified by the unique connId. The WebSocket channels subject to this limitation are as follows:
- Price channel
- Candlesticks channel
- Trades channel
If users subscribe to the same channel through the same WebSocket connection through multiple arguments, it will be counted once only. If users subscribe to the listed channels (such as price and trade) using either the same or different connections, it will not affect the counting, as these are considered as two different channels. The system calculates the number of WebSocket connections per channel. The platform will send the number of active connections to clients through the channel-conn-count event message to new channel subscriptions. Connection count update
{
"event":"channel-conn-count",
"channel":"prices",
"connCount": "2",
"connId":"abcd1234"
}
When the limit is breached, generally the latest connection that sends the subscription request will be rejected. Client will receive the usual subscription acknowledgement followed by the channel-conn-count-error from the connection that the subscription has been terminated. In exceptional circumstances, the platform may unsubscribe existing connections. Connection limit error
{
"event": "channel-conn-count-error",
"channel": "prices",
"connCount": "20",
"connId":"a4d3ae55"
}
Notification#
WebSocket has introduced a new message type (event = notice). Client will receive the information in the following scenarios:
- Websocket disconnect for service upgrade 30 seconds prior to the upgrade of the WebSocket service, the notification message will be sent to users indicating that the connection will soon be disconnected. Users are encouraged to establish a new connection to prevent any disruptions caused by disconnection.
Response Example
{
"event": "notice",
"code": "64008",
"msg": "The connection will soon be closed for a service upgrade. Please reconnect.",
"connId": "a4d3ae55"
}