Skip to main content
Version: V2

WebSocket API

Overview

WebSocket is a new protocol in HTML5 that enables full-duplex communication between clients and servers, allowing rapid bidirectional data transmission. Through a simple handshake, a connection can be established between client and server, enabling the server to actively push information to the client based on business rules. Its advantages include:

  • Small header size (~2 bytes) during data transmission between client and server
  • Both client and server can actively send data
  • Eliminates the need for repeated TCP connection setup/teardown, conserving bandwidth and server resources
  • Strongly recommended for developers to obtain market data, order book depth, and other information
DomainWebSocket APIRecommended Use
Public Channelwss://ws-spot.weex.com/v2/ws/publicPrimary domain, public channels
Private Channelwss://ws-spot.weex.com/v2/ws/privatePrimary domain, private channels

Connection

Connection Specifications:

  • Connection limit: 300 connection requests/IP/5 minutes, maximum 100 concurrent connections per IP
  • Subscription limit: 240 operations/hour/connection, maximum 100 channels per connection
  • Private channel requirement: Private channel connections require header authentication

To maintain stable and effective connections, we recommend:

After successful WebSocket connection establishment, the server will periodically send Ping messages to the client in the format: {"event":"ping","time":"1693208170000"}, where "time" represents the server's timestamp. Upon receiving this message, the client should respond with a Pong message: {"event":"pong","time":"1693208170000"}. The server will actively terminate connections that fail to respond more than 5 times.

Header Authentication for Private Channels

ACCESS-KEY: Unique identifier for API user authentication (requires application)

ACCESS-PASSPHRASE: Password for the API Key

ACCESS-TIMESTAMP: Unix Epoch timestamp in milliseconds (expires after 30 seconds, must match signature timestamp)

ACCESS-SIGN: Signature string generated as follows:

The message (string to be signed) consists of: timestamp + requestPath

Example timestamp (in milliseconds): const timestamp = '' + Date.now()

Where requestPath is /v2/ws/private

Signature Generation Process

  1. Encrypt the message string using HMAC SHA256 with the secret key:
    • Signature = hmac_sha256(secretkey, Message)
  2. Encode the Signature using Base64:
    • Signature = base64.encode(Signature)

Subscription

Subscription Specification:

{
"event": "subscribe",
"channel": "channel_name"
}

Unsubscription

Unsubscription Specification:

{
"event": "unsubscribe",
"channel": "channel_name"
}