# WebSocket

**General**

WebSocket is a modern HTML5 protocol that enables full-duplex data transmission between the client and the server, allowing data to be transferred effectively in both directions. With a single handshake, the connection is established, allowing the server to push data to the client according to preset rules. Its advantages include:

* The WebSocket request header for data transmission between the client and server is approximately 2 bytes only.
* Either the client or server can initiate data transmission.
* No need to repeatedly create and delete TCP connections, saving resources for both bandwidth and server.

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

**WebSocket Information**

**URL:** `wss://ws.fokawa.com/kline-api/ws`

The returned data will be binary compressed (users need to decompress it using the Gzip algorithm).

**Heartbeat**

The server actively pushes ping messages every 10 seconds. The client should reply with a pong message to maintain the connection. The server does not strictly verify the client's pong replies. To ensure the link's validity, the client should immediately reply in Pong format upon receiving a ping message from the server.

* **Ping message format sent by the server:** `{"ping": timestamp (seconds)}`
* **Pong message format replied by the client:** `{"pong": timestamp (seconds)}`
* **Example:** `{"pong": 1694416595}`

**Command Format**

* **Subscribe Depth:**
  * `sub market_$symbol_depth_step0`
* **Unsubscribe Depth:**
  * `unsub market_$symbol_depth_step0`
* **Subscribe to Real-Time Trade:**
  * `sub market_$symbol_trade_ticker`
* **Unsubscribe Real-Time Trade:**
  * `unsub market_$symbol_trade_ticker`
* **Subscribe to 24h Market Data:**
  * `sub market_$symbol_ticker`
* **Unsubscribe 24h Market Data:**
  * `unsub market_$symbol_ticker`
* **Subscribe to 1min K-line Information:**
  * `sub market_$symbol_kline_1min`
* **Request 1 Month Historical Bar Record:**
  * `reg market_$symbol_kline_1month`

**Heartbeat Example**

* **Ping:**

  ```json
  {
    "ping": 1535975085052
  }
  ```
* **Pong:**

  ```json
  {
    "pong": 1535975085052
  }
  ```

**Subscription Messages**

* **Full Depth Subscription Message:**

  ```json
  {
    "event": "sub",
    "params": {
        "channel": "market_$symbol_depth_step0", // $symbol E.g. btcusdt
        "cb_id": "1" // Business ID is not required
    }
  }
  ```
* **Full Depth Payload:**

  ```json
  {
    "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": [ // Buys
            [9999.53, 0.93],
            [9998.2, 0.2],
            [9997.19, 0.21]
        ]
    }
  }
  ```
* **Real-Time Trade Subscription Message:**

  ```json
  {
    "event": "sub",
    "params": {
        "channel": "market_$symbol_trade_ticker", // $symbol E.g. btcusdt
        "cb_id": "1" // Business ID is not required
    }
  }
  ```
* **Real-Time Trade Payload:**

  ```json
  {
    "channel": "market_$symbol_trade_ticker",
    "ts": 1506584998239, // Request time
    "tick": {
        "data": [
            {
                "side": "buy", // buy, sell
                "price": 32.233,
                "vol": 232,
                "amount": 323,
                "ds": "2017-09-10 23:12:21"
            }
        ]
    }
  }
  ```
* **Kline Market Subscription Message:**

  ```json
  {
    "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
    }
  }
  ```
* **Kline Market Payload:**

  ```json
  {
    "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
    }
  }
  ```
* **Market Tickers Subscription Message:**

  ```json
  {
    "event": "sub",
    "params": {
        "channel": "market_$symbol_ticker", // $symbol E.g. btcusdt
        "cb_id": "1" // Business ID is not required
    }
  }
  ```
* **Market Tickers Payload:**

  ```json
  {
    "channel": "market_$symbol_ticker",
    "ts": 1506584998239, // Request time
    "tick": {
        "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
        "rose": -0.2922 // Increase
    }
  }
  ```

**Request Kline History Data**

* **Subscription Message Structure:**

  ```json
  {
    "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
    }
  }
  ```
* **Payload:**

  ```json
  {
    "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
        }
    ]
  }
  ```

**Request History Trade**

* **Subscription Message Structure:**

  ```json
  {
    "event": "req",
    "params": {
        "channel": "market_$symbol_trade_ticker", // $symbol E.g. btcusdt
        "cb_id": "1" // Business ID is not required
    }
  }
  ```
* **Payload:**

  ```json
  {
    "event_rep": "rep",
    "channel": "market_$symbol_trade_ticker",
    "ts": 1506584998239,
    "status": "ok",
    "data": [
        {
            "side": "buy", // buy, sell
            "price": 32.233, // Trade price
            "vol": 232, // Trade volume
            "amount": 323 // Trade amount
        },
        {
            "side": "buy", // buy, sell
            "price": 32.233, // Trade price
            "vol": 232, // Trade volume
            "amount": 323 // Trade amount
        }
    ]
  }
  ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://apidocs.fokawa.com/openapi-basic-information/websocket.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
