BitMart JavaScript SDK example: ws-spot-public.ts

BitMart websocket spot public JavaScript example for the Siebly BitMart 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/Bitmart/Websocket/ws-spot-public.ts

What this example covers

  • BitMart WebSocket stream JavaScript example.
  • Uses the Siebly BitMart SDK package bitmart-api instead of hand-written WebSocket plumbing.
  • Source path: Bitmart/Websocket/ws-spot-public.ts.
  • Imports SDK symbols including WebsocketClient.
  • Calls SDK methods such as on(), tryWsSend(), subscribe().

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.
import { WebsocketClient } from 'bitmart-api'; async function start() {  const client = new WebsocketClient();   client.on('open', (data) => {    console.log('open: ', data?.wsKey);     // Some topics allow requests, here's an example for sending a request    // const wsKey = 'spotPublicV1';    // if (data?.wsKey === wsKey) {    //   const depthIncreaseDataRequest: WsSpotOperation = {    //     op: 'request',    //     args: ['spot/depth/increase100:BTC_USDT'],    //   };     //   client.tryWsSend(    //     'spotPublicV1',    //     JSON.stringify(depthIncreaseDataRequest),    //   );    // }  });   // Data received  client.on('update', (data) => {    console.info('data received: ', JSON.stringify(data));  });   // Something happened, attempting to reconnect  client.on('reconnect', (data) => {    console.log('reconnect: ', data);  });   // Reconnect successful  client.on('reconnected', (data) => {    console.log('reconnected: ', data);  });   // Connection closed. If unexpected, expect reconnect -> reconnected.  client.on('close', (data) => {    console.error('close: ', data);  });   // Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"  client.on('response', (data) => {    console.info('response: ', data);  });   client.on('exception', (data) => {    console.error('exception: ', data);  });   try {    /**     * Use the client subscribe(topic, market) pattern to subscribe to any websocket topic.     *     * You can subscribe to topics one at a time:     */     // Ticker Channel    // client.subscribe('spot/ticker:BTC_USDT', 'spot');     // KLine/Candles Channel    // client.subscribe('spot/kline1m:BTC_USDT', 'spot');     // Depth-All Channel    // client.subscribe('spot/depth5:BTC_USDT', 'spot');     // Depth-Increase Channel    // client.subscribe('spot/depth/increase100:BTC_USDT', 'spot');     // Trade Channel    // client.subscribe('spot/trade:BTC_USDT', 'spot');     /**     * Or have multiple topics in one array, in a single request:     */    client.subscribe(      [        'spot/ticker:BTC_USDT',        'spot/ticker:ETH_USDT',        'spot/ticker:XRP_USDT',        'spot/ticker:BMX_USDT',        'spot/ticker:SOL_USDT',      ],      'spot',    );  } catch (e) {    console.error('Req error: ', e);  }} start(); 

Subscribe on Substack

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