# WebSocket API v1

## Endpoint

* Testnet
  * Spot: `wss://testnet-gw.sodex.dev/ws/spot`
  * Perps: `wss://testnet-gw.sodex.dev/ws/perps`
* Mainnet
  * Spot: `wss://mainnet-gw.sodex.dev/ws/spot`
  * Perps: `wss://mainnet-gw.sodex.dev/ws/perps`

## Rate limits

For the full REST and WebSocket limit table, see [API Rate Limits](https://sodex.com/documentation/api/api-rate-limits).

## Connection

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 **`60`** seconds.

To keep the connection stable:

1. Set a timer of `N` seconds whenever a response message is received, where `N` is less than `60`.
2. If the timer is triggered, which means that no new message is received within `N` seconds, send the `{"op":"ping"}` as request.
3. Expect a `{"op":"pong"}` as a response. If the response message is not received within `N` seconds, please raise an error or reconnect.

## Data Streams

To subscribe to specific data feeds, you need to send a subscription message. The subscription message format is as follows:

```json
{
  "op": "subscribe",
  "params": { ... }
}
```

The subscription object contains the details of the specific feed you want to subscribe to.

To unsubscribe from a specific data feed on the Sodex WebSocket API, you need to send an unsubscribe message with the following format:

```json
{
  "op": "unsubscribe",
  "params": { ... }
}
```

The `params` object should match the original subscription message that was sent when subscribing to the feed. This allows the server to identify the specific feed you want to unsubscribe from. By sending this unsubscribe message, you inform the server to stop sending further updates for the specified feed.

Please note that unsubscribing from a specific feed does not affect other subscriptions you may have active at that time. To unsubscribe from multiple feeds, you can send multiple unsubscribe messages, each with the appropriate subscription details.

User-specific streams do not require subscription authorization. Any client may subscribe to another user's data, and API key authentication is not required for these subscriptions.

### Data type definitions

see [Schema](https://sodex.com/documentation/api/rest-v1/schema)

### Available Data Streams

* [Ticker Stream](https://sodex.com/documentation/api/websocket-v1/ticker)
* [All Tickers Stream](https://sodex.com/documentation/api/websocket-v1/all-tickers)
* [Mini Ticker Stream](https://sodex.com/documentation/api/websocket-v1/mini-ticker)
* [All Mini Tickers Stream](https://sodex.com/documentation/api/websocket-v1/all-mini-tickers)
* [Book Ticker Stream](https://sodex.com/documentation/api/websocket-v1/book-ticker)
* [All Book Tickers Stream](https://sodex.com/documentation/api/websocket-v1/all-book-tickers)
* [Mark Price Stream](https://sodex.com/documentation/api/websocket-v1/mark-price) (perps only)
* [All Mark Prices Stream](https://sodex.com/documentation/api/websocket-v1/all-mark-prices) (perps only)
* [L2 Book Stream](https://sodex.com/documentation/api/websocket-v1/l2book)
* [L4 Book Stream](https://sodex.com/documentation/api/websocket-v1/l4book)
* [Candles (OHLC) Stream](https://sodex.com/documentation/api/websocket-v1/candles)
* [Market Trades Stream](https://sodex.com/documentation/api/websocket-v1/market-trade)
* [Account Frontend State Stream](https://sodex.com/documentation/api/websocket-v1/account-frontend-state)
* [Account Updates Stream](https://sodex.com/documentation/api/websocket-v1/account-updates)
* [Account Order Updates Stream](https://sodex.com/documentation/api/websocket-v1/account-order-updates)
* [Account Trades Stream](https://sodex.com/documentation/api/websocket-v1/account-trades)
* [Account Events Stream](https://sodex.com/documentation/api/websocket-v1/account-events)
