watchBinanceKlines
Stream live bar updates for one Binance symbol and interval. This is the low-level stream in Binance’s own vocabulary — most consumers call CandleSource.watchCandles (or the useCandleStream hook) and never touch it; it is exported for anyone assembling their own source.
import { watchBinanceKlines } from "@symmio/trading-core";
const unwatch = watchBinanceKlines({
wsUrl: "wss://fstream.binance.com/market/stream",
symbol: "BTCUSDT",
interval: "1m",
webSocketConstructor: WebSocket,
onCandle: (candle, { closed }) => console.log(candle.close, closed),
});Parameters
wsUrlstringCombined-stream WebSocket endpoint (…/stream). For futures this must be the /market/ route — see the routing
note on createBinanceCandleSource.
symbolstringBinance symbol, upper-case (e.g. "BTCUSDT").
intervalstringBinance interval string (e.g. "1m"), not an SDK resolution. Translate with toBinanceInterval.
webSocketConstructorWebSocketConstructorWebSocket implementation to dial with.
onCandle(candle: Candle, meta: CandleUpdateMeta) => voidCalled for each update to the bar, and once more with closed: true when it closes.
onReset() => voidoptionalCalled after a reconnect, when bars may have been missed. Refetch history rather than splicing a live bar onto a stale series.
onStatusChange(status: SocketStatus) => voidoptionalCalled whenever the connection status changes.
onError(error: SymmError) => voidoptionalCalled on a transport or parse error (BINANCE_KLINES_SOCKET_ERROR); does not stop the subscription.
Returns
UnwatchGuarantees
- One connection per call. Deliberate at this scale — a chart holds exactly one subscription — and it keeps the reconnect and unsubscribe paths trivially correct.
- Reconnects re-subscribe themselves. Binance drops idle connections after 24 hours; the reconnecting socket re-dials, re-sends the SUBSCRIBE frame, and fires
onResetso the consumer refetches the bars missed in the gap. - Frames are matched on symbol and interval. Matching on symbol alone lets two charts on different intervals feed each other’s series.
- Both combined-stream (
{ stream, data }) and raw frame shapes are parsed, so the same code works against either endpoint form.
Related
createBinanceCandleSource— the full source this stream powers.useCandleStream— the React hook overwatchCandles.Candle— the bar shape delivered toonCandle.