Blog
Binance market dataWebSockets

Consuming Binance miniTicker Updates in Node.js

Maintain a bounded Binance miniTicker price table without deleting unchanged symbols, confusing rolling statistics with daily bars, or retaining prices after reconnect.

Siebly.io2 min readMarkdown

Keep One Entry per Watched Symbol

Binance's all-market miniTicker stream sends the symbols whose ticker statistics changed. Replacing the application's entire price map with each incoming array would therefore delete symbols that simply did not appear in that update.

The stream contract describes a rolling 24-hour window. Its statistics do not represent the UTC calendar day or a sequence of individual trades.

This program watches two symbols through the all-market stream. It requires the binance package and stops after twenty seconds:

mini-ticker.mjs

JavaScript
import { performance } from 'node:perf_hooks';
import { WebsocketClient } from 'binance';

const watched = ['BTCUSDT', 'ETHUSDT'];
const latest = new Map();
const client = new WebsocketClient();
let stopped = false;

client.on('message', (message) => {
  if (stopped) return;
  for (const ticker of Array.isArray(message) ? message : [message]) {
    if (!ticker || ticker.e !== '24hrMiniTicker' || !watched.includes(ticker.s)) continue;
    if (
      typeof ticker.c !== 'string' ||
      !Number.isFinite(Number(ticker.c)) ||
      Number(ticker.c) <= 0 ||
      !Number.isSafeInteger(ticker.E)
    )
      continue;
    latest.set(ticker.s, {
      price: ticker.c,
      exchangeTime: ticker.E,
      receivedAt: performance.now(),
    });
  }
});
client.on('reconnecting', () => latest.clear());
client.on('close', () => latest.clear());
client.on('exception', () => {
  console.error('miniTicker connection error');
  process.exitCode = 1;
  stop();
});
const reporter = setInterval(() => {
  console.table(
    watched.map((symbol) => {
      const ticker = latest.get(symbol);
      if (!ticker) return { symbol, status: 'waiting' };
      const ageMs = Math.round(performance.now() - ticker.receivedAt);
      return { symbol, price: ticker.price, ageMs, status: ageMs > 5_000 ? 'old' : 'recent' };
    }),
  );
}, 1_000);
const deadline = setTimeout(stop, 20_000);
function stop() {
  if (stopped) return;
  stopped = true;
  clearInterval(reporter);
  clearTimeout(deadline);
  client.closeAll();
}
process.once('SIGINT', stop);
await client.subscribe('!miniTicker@arr', 'main');

The map is bounded by the watched symbols. Each valid ticker replaces only its own entry. An empty array changes nothing. The message handler keeps prices as decimal strings and does no database or rendering work.

Interpret Age Carefully

ageMs measures time since the program received a valid ticker for that symbol. The five-second label is a display policy for this example. A quiet symbol can remain absent while the connection is healthy, so the label is not proof of a transport failure.

The ticker's event time and local monotonic receipt time answer different questions. Neither a recent ticker nor a changed last price establishes the completeness of a trade history.

Reset After an Interruption

The program clears cached prices on a reconnect attempt or close and waits for new observations. It does not mark an old price as current merely because the socket reopens. A display may show waiting until a watched symbol appears again.

For a small symbol set, individual @miniTicker subscriptions avoid receiving the entire all-market array. The Binance tutorial covers subscription helpers. A recorder requiring every trade needs a trade stream and history recovery, rather than a miniTicker table.

Related articles

Continue from here

Related Siebly resources

All articles

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.