Skip to content

WebSocket streams

Overview

WebSocket is an HTML5 protocol that enables full-duplex communication so data can flow quickly in both directions. A single handshake establishes the connection; the server can push updates according to business rules. Benefits:

  • Small framing overhead when exchanging data (on the order of a few bytes).
  • Either side can initiate messages.
  • Avoids repeatedly opening and closing TCP connections, saving bandwidth and server resources.

Developers are strongly encouraged to use WebSockets for market data, depth, and related feeds.

Basics

  • Spot market WebSocket base: wss://openapi.bitbaby.com/spot/ws
  • Futures market WebSocket base: wss://openapi.bitbaby.com/futures/ws
  • Except for heartbeat payloads, outbound data may be gzip-compressed binary (clients must decompress with Gzip).

Parameter examples

eventchanneldescription
submarket_$symbol_depth_0.1Subscribe to depth (replace $symbol; check API for symbol format)
unsubmarket_$symbol_depth_0.1Unsubscribe depth
submarket_$symbol_dealsSubscribe to trades
unsubmarket_$symbol_dealsUnsubscribe trades
submarket_$symbolSubscribe to 24h ticker
unsubmarket_$symbolUnsubscribe 24h ticker
submarket_$symbol_kline_1minSubscribe to 1m k-lines

Send a heartbeat within 30 seconds:

// heartbeat within 30s
{
    "ping": "ping"
}

Full depth subscription

  • Subscribe payload example
java
{
    "event": "sub",
    "params": {
        "channel": "market_$symbol_depth_0.1" // $symbol e.g. spot btcusdt, futures e_btcusdt
    }
}
  • Response includes up to 30 bid/ask levels
java
{
    "channel": "market_btcusdt_depth_0.1",
    "ts": 1506584998239,
    "tick": {
        "asks": [ // asks
            [10000.19, 0.93],
            [10001.21, 0.2],
            [10002.22, 0.34]
        ],
        "buys": [ // bids
            [9999.53, 0.93],
            [9998.2, 0.2],
            [9997.19, 0.21]
        ]
    }
}

Trade stream

  • Subscribe payload example
java
{
    "event": "sub",
    "params": {
        "channel": "market_$symbol_deals" // $symbol e.g. spot btcusdt, futures e_btcusdt
    }
}
  • Payload
java
{
    "channel": "market_$symbol_deals",
    "ts": 1506584998239, // request time
    "tick": {
        "id": 12121,                // max trade id in data
        "ts": 1506584998239,        // max time in data
        "data": [
            {
                "side": "buy",      // buy / sell
                "price": 32.233,    // price
                "vol": 232,         // size
                "amount": 323,      // quote amount
                "ds": "2017-09-10 23:12:21"
            }
        ]
    }
}

K-line stream

  • Subscribe payload example
java
{
    "event": "sub",
    "params": {
        "channel": "market_$symbol_kline_[1min/5min/15min/30min/60min/1day/1week/1month]" // e.g. btcusdt
    }
}
  • Payload
java
{
    "channel": "market_$symbol_kline_1min", // 1min = 1-minute candle
    "ts": 1506584998239,        // request time
    "tick": {
        "id": 1506602880,       // candle open time
        "vol": 1212.12211,      // volume
        "open": 2233.22,        // open
        "close": 1221.11,       // close
        "high": 22322.22,       // high
        "low": 2321.22          // low
    }
}

24h ticker

  • Subscribe payload example
java
{
    "event": "sub",
    "params": {
        "channel": "market_$symbol" // $symbol e.g. spot btcusdt, futures e_btcusdt
    }
}
  • Payload
java
{
    "channel": "market_$symbol",
    "ts": 1506584998239,        // request time
    "tick": {
        "amount": 123.1221,     // quote volume
        "vol": 1212.12211,      // base volume
        "open": 2233.22,        // open
        "close": 1221.11,       // close / last
        "high": 22322.22,       // high
        "low": 2321.22,         // low
        "rose": -0.2922         // change ratio
    }
}