> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.6mm.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.6mm.com/_mcp/server.

# Connection, Subscription & Heartbeat

Use WebSocket streams for market data and private user-state updates that must stay current without polling.

## Connection URL

```text
wss://ws.6mm.com/ws
```

Private streams require authentication context such as a listenKey or access token as described in the private channel documentation.

## Subscribe

```json
{ "id": "1772007814666", "op": "subscribe", "args": ["market.depth.BTCUSDT"] }
```

Successful subscription acknowledgement:

```json
{
  "id": "1772007814666",
  "event": "subscribe",
  "success": true,
  "data": ["market.depth.BTCUSDT"]
}
```

## Unsubscribe

```json
{ "id": "1772007814667", "op": "unsubscribe", "args": ["market.depth.BTCUSDT"] }
```

## Application ping

```json
{ "id": "1772007814668", "op": "ping", "args": [] }
```

## Message envelope

Public pushes use a consistent envelope:

```json
{
  "topic": "market.depth.BTCUSDT",
  "event": "data",
  "ts": 1772007815000,
  "data": {}
}
```

## Operational recommendations

* Generate a unique client-side `id` for every subscribe, unsubscribe, and ping request.
* Treat reconnect as a new session and resubscribe to required topics.
* Validate that the returned `topic` matches the channel your service expects.
* Keep message processing idempotent where private order or account events are involved.
* Log connection state changes, subscription acknowledgements, and abnormal disconnects.

## Reconnect workflow

1. Close or discard the previous socket and create a new connection.
2. Re-authenticate private streams with a valid listenKey or access token.
3. Resubscribe to every required topic and wait for successful acknowledgements.
4. Replace local order-book state with a new snapshot before applying incremental updates.
5. Reconcile open orders, positions, and balances through REST if private events may have been missed.

Use bounded exponential backoff with jitter for reconnect attempts. A continuously reconnecting client should alert operators instead of retrying indefinitely without visibility.

## Heartbeat and stale connections

* Track the most recent inbound message and successful ping acknowledgement.
* Use the heartbeat interval and idle threshold approved for the production environment.
* Treat a connection as stale when the configured threshold is exceeded, even if the TCP socket still appears open.
* Do not reuse subscriptions from an old socket; resubscribe explicitly after every reconnect.

## Related documentation

* [Public Market Channels](/developer-api/websocket/public-market-channels): topics, order-book continuity, trades, candles, and tickers.
* [Private User Channel](/developer-api/websocket/private-user-channel): listenKey lifecycle and authenticated events.
* [Integration Recommendations](/developer-api/integration-recommendations): retries, idempotency, monitoring, and launch testing.