> For the complete documentation index, see [llms.txt](https://openapi-docs.nagaexchange.co.id/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openapi-docs.nagaexchange.co.id/websocket-api/websocket.md).

# WebSocket

{% hint style="warning" %}
⚠️ This page documents public market-data push channels only. No authenticated/user-data WebSocket stream (e.g. private order or balance updates) is currently available. Do not assume one exists.
{% endhint %}

### General

`WebSocket` is a communication protocol that provides full-duplex data transmission between client and server, meaning data can flow in both directions at the same time. Only one handshake is needed to establish the connection; after that, the server can push data to the client according to preset rules. Its advantages include:

* The `WebSocket` request header for data transmission between client and server is only about 2 bytes
* Either the client or the server can initiate a data transmission
* Because the connection doesn't need to be repeatedly created and torn down, this approach saves both bandwidth and server resources

**We strongly recommend using the WebSocket API to retrieve market data and order book depth.**

### WS Information

* URL: <wss://wspool.nagaexchange.co.id/kline-api/ws>
* All returned data is Gzip-compressed in binary form, except for heartbeat messages. Decompress the data with Gzip before parsing.

### Command Format

| event   | channel                                                                | description                       |
| ------- | ---------------------------------------------------------------------- | --------------------------------- |
| `sub`   | `market_$symbol_depth_step0`                                           | Subscribe to depth                |
| `unsub` | `market_$symbol_depth_step0`                                           | Unsubscribe from depth            |
| `sub`   | `market_$symbol_trade_ticker`                                          | Subscribe to real-time trades     |
| `unsub` | `market_$symbol_trade_ticker`                                          | Unsubscribe from real-time trades |
| `req`   | `market_$symbol_trade_ticker`                                          | Request historical trade records  |
| `sub`   | `market_$symbol_ticker`                                                | Subscribe to 24h market data      |
| `unsub` | `market_$symbol_ticker`                                                | Unsubscribe from 24h market data  |
| `sub`   | `market_$symbol_kline_[1min/5min/15min/30min/60min/1day/1week/1month]` | Subscribe to k-line information   |
| `req`   | `market_$symbol_kline_[1min/5min/15min/30min/60min/1day/1week/1month]` | Request historical k-line records |

### Heartbeat

Every so often, the server sends a `PING` message. On receiving it, the client must send back a **new** `PONG` message — not simply re-send or acknowledge the `PING`. If the server doesn't receive a `PONG` in response, it will close the connection.

* `PING` — sent by the server:

```java
{
    "ping": 1535975085052
}
```

* `PONG` — sent by the client, in response to a `PING`:

```java
{
    "pong": 1535975085052
}
```

### Subscription Full Depth

* Subscription message structure

```java
{
    "event":"sub",
    "params":{
        "channel":"market_$symbol_depth_step0", // $symbol E.g. btcusdt
        "cb_id":"1" // Business ID; optional
    }
}
```

* Payload

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

### Subscription Real-Time Trade

* Subscription message structure

```java
{
    "event":"sub",
    "params":{
        "channel":"market_$symbol_trade_ticker", // $symbol E.g. btcusdt
        "cb_id":"1" // Business ID; optional
    }
}
```

* Payload

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

### Subscription Kline Market

* Subscription message structure

```java
{
    "event":"sub",
    "params":{
        "channel":"market_$symbol_kline_[1min/5min/15min/30min/60min/1day/1week/1month]", // $symbol E.g. btcusdt 
        "cb_id":"1" // Business ID; optional
    }
}
```

* Payload

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

### Subscription Market Tickers

* Subscription message structure

```java
{
    "event":"sub",
    "params":{
        "channel":"market_$symbol_ticker", // $symbol E.g. btcusdt 
        "cb_id":"1" // Business ID; optional
    }
}
```

* Payload

```java
{
    "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 //price change; + means increase, - means decrease
    }
}
```

### Request Kline History Data

* Request message structure

```java
{
    "event":"req",
    "params":{
        "channel":"market_$symbol_kline_[1min/5min/15min/30min/60min/1day/1week/1month]",
        "cb_id":"1",
        "endIdx":"1506602880", //Returns pageSize records before this point; optional
        "pageSize":100 // optional
    }
}
```

* Payload

```java
{
    "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

* Request message structure

```java
{
    "event":"req",
    "params":{
        "channel":"market_$symbol_trade_ticker", // $symbol E.g. btcusdt 
        "cb_id":"1" // Business ID; optional
    }
}
```

* Payload

```java
{
    "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 vol
            "amount":323//trade amount
        },
        {
            "side":"buy",//buy,sell
            "price":32.233,//trade price
            "vol":232,//trade vol
            "amount":323//trade amount
        }
    ]
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://openapi-docs.nagaexchange.co.id/websocket-api/websocket.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
