Binance Misc JavaScript SDK example: ws-unsubscribe.ts

Binance Misc websocket unsubscribe JavaScript example for the Siebly Binance SDK, covering exchange REST API and WebSocket integration, setup, and production SDK docs.

We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.

examples/Binance/WebSockets/Misc/ws-unsubscribe.ts

What this example covers

  • Binance WebSocket stream JavaScript example.
  • Uses the Siebly Binance SDK package binance instead of hand-written WebSocket plumbing.
  • Source path: Binance/WebSockets/Misc/ws-unsubscribe.ts.
  • Example category: Misc.
  • Imports SDK symbols including WebsocketClient.
  • Calls SDK methods such as on(), subscribe(), unsubscribe().

How to use this example

  • Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
  • For WebSocket examples, keep reconnect, resubscribe, heartbeat, and event-handler behavior explicit in your service.
  • Open the repository source when you need the latest committed version: GitHub source file.
/* eslint-disable @typescript-eslint/no-unused-vars */import { WebsocketClient } from 'binance'; /** * * A simple demonstration on how to unsubscribe from one or more topics. * */(async () => {  const wsClient = new WebsocketClient();   // Raw unprocessed incoming data, e.g. if you have the beautifier disabled  wsClient.on('message', (data) => {    console.log('raw message received ', JSON.stringify(data));  });   wsClient.on('open', (data) => {    console.log('connection opened open:', data.wsKey);  });   wsClient.on('response', (data) => {    console.log('log response: ', data?.message || JSON.stringify(data));  });   wsClient.on('reconnecting', (data) => {    console.log('ws automatically reconnecting.... ', data?.wsKey);  });   wsClient.on('reconnected', (data) => {    console.log('ws has reconnected ', data?.wsKey);  });   try {    /**     * The Websocket Client will automatically manage connectivity and active topics/subscriptions for you.     *     * Simply call wsClient.subscribe(topic, wsKey) as many times as you want, with or without an array.     */     const wsTopicList = [      // Aggregate Trade Streams      // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#aggregate-trade-streams      'btcusdt@aggTrade',      // Kline/Candlestick Streams for UTC      // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#klinecandlestick-streams-for-utc      'btcusdt@kline_5m',      // Individual Symbol Mini Ticker Stream      // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-mini-ticker-stream      'btcusdt@miniTicker',      // Individual Symbol Ticker Streams      // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-ticker-streams      'btcusdt@ticker',      // Individual Symbol Rolling Window Statistics Streams      // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-rolling-window-statistics-streams      'btcusdt@ticker_1h',      // Average Price      // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#average-price      'btcusdt@avgPrice',    ];     console.log(      new Date(),      'Subscribing to the following topics: ',      wsTopicList,    );     /**     * Subscribe to each available type of spot market topic, the new way     */    await wsClient.subscribe(wsTopicList, 'main');     const unsubscribeFromList = [      'btcusdt@aggTrade',      'btcusdt@kline_5m',      'btcusdt@miniTicker',      'btcusdt@ticker',      'btcusdt@ticker_1h',    ];     // 5 seconds later, unsubscribe from almost all topics except avg price    setTimeout(() => {      console.log(        new Date(),        'Unsubscribing from the following topics: ',        unsubscribeFromList,      );      wsClient.unsubscribe(unsubscribeFromList, 'main');    }, 1000 * 5);  } catch (e) {    console.error('exception on subscribe attempt: ', e);  }})(); 

Subscribe on Substack

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