Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion sdks/binance/docs/guides/websockets.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Binance WebSocket streams in the SDK"
description: "Subscribe to Binance WebSocket streams in the SDK for live tickers, depth and trades. Reconnects are handled by the SDK. JavaScript examples."
description: "Subscribe to Binance WebSocket streams in the SDK for live Spot and USD-M futures tickers, klines, depth and trades. Reconnects and futures stream routes are handled by the SDK. JavaScript examples."
---

# Binance WebSocket streams in the SDK {#websocket-subscriptions}
Expand All @@ -13,6 +13,23 @@ Save this as `stream.ts` in a TypeScript project. It prints updates for 30 secon

<<< @/../examples/stream.ts

## USD-M futures streams

Pass the `usdm` key to subscribe to Binance Futures WebSocket streams. Binance serves USD-M market streams such as klines, mark price and liquidations on the `/market` route of `wss://fstream.binance.com`, and depth on the `/public` route. The SDK picks the route for each topic and opens the connections it needs:

```ts
import { WebsocketClient } from 'binance';

const ws = new WebsocketClient({});

ws.on('message', (message) => console.log(message));
ws.on('exception', (error) => console.error('WebSocket exception', error));

ws.subscribe(['btcusdt@markPrice', 'btcusdt@kline_1m', 'btcusdt@depth5'], 'usdm');
```

Use `coinm` for COIN-M futures. If a raw socket on `wss://fstream.binance.com/ws` connects but kline or markPrice updates never arrive, see [troubleshooting](/guides/troubleshooting).

## Optional: readable fields with the SDK beautifier

For descriptive field names and parsed numeric values, enable the SDK beautifier. In the example above, replace the client constructor and raw `message` listener with:
Expand Down