This content was muted by Programming & Dev moderators for not following community guidelines.

Binance WebSocket in Node.js: streaming futures mark price and trades with automatic reconnects

Words
285
Reading
2 min
2h
Listen to this post
~2 min

If you have ever opened a raw WebSocket to Binance from Node.js, you know the first version is easy and the second version is where the work starts: picking the right stream host for Spot or futures, answering pings, reconnecting after the 24 hour cut-off, and subscribing again to everything you had before the drop.

Here is a short way to get live USD-M futures data with all of that handled for you, using the open-source `binance` package from npm. No API key is needed for public streams.

## Install

```bash

npm install binance

```

## Stream mark price and trades

Save this as `stream.mjs` and run `node stream.mjs`:

```js

import { WebsocketClient } from 'binance';

const ws = new WebsocketClient({ beautify: true });

ws.on('formattedMessage', (msg) => {

if (msg.eventType === 'markPriceUpdate') {

console.log(`${msg.symbol} mark ${msg.markPrice}, funding ${msg.fundingRate}`);

} else if (msg.eventType === 'aggTrade') {

console.log(`${msg.symbol} trade ${msg.price} x ${msg.quantity}`);

}

});

ws.on('reconnecting', ({ wsKey }) => console.log('reconnecting', wsKey));

ws.on('reconnected', ({ wsKey }) => console.log('reconnected', wsKey));

ws.on('exception', console.error);

// USD-M futures streams, no API key needed

ws.subscribeMarkPrice('BTCUSDT', 'usdm');

ws.subscribeAggregateTrades('ETHUSDT', 'usdm');

```

Output looks like this (mark price updates every 3 seconds, trades as they happen):

```

BTCUSDT mark 53001.61969565, funding -0.00007122

ETHUSDT trade 1524.11 x 1.595

```

`beautify: true` turns Binance's one letter field names into readable ones (`markPrice`, `fundingRate`, `quantity`), so the handler reads like normal code. The client picks the correct futures stream route for each topic, keeps the connection alive with heartbeats, and if the socket drops it reconnects and resubscribes on its own. The `reconnecting` and `reconnected` events are there if you want to log or pause your logic while that happens.

## A quick REST check alongside it

The same package has a typed REST client for the Binance Futures API. Handy for a snapshot before the stream starts:

```js

import { USDMClient } from 'binance';

const client = new USDMClient();

const mark = await client.getMarkPrice({ symbol: 'BTCUSDT' });

const oi = await client.getOpenInterest({ symbol: 'BTCUSDT' });

console.log(mark.markPrice, mark.lastFundingRate, oi.openInterest);

```

## Where to go next

Private streams (orders, balances, positions) use the same `WebsocketClient` once you pass an API key and secret. The SDK docs have a full [Binance WebSocket guide](https://sieblyio.github.io/docs/binance/guides/websockets.html) covering Spot, futures, user data and the WebSocket API.

Both snippets above were run against live Binance futures streams on 3 December 2026 with Node.js 24 and binance 3.6.15.