BitMart JavaScript SDK example: ws-custom-logger.ts

BitMart websocket custom logger 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-custom-logger.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-custom-logger.ts.
  • Imports SDK symbols including DefaultLogger, WebsocketClient.
  • Calls SDK methods such as on(), 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 { DefaultLogger, LogParams, WebsocketClient } from 'bitmart-api'; /** Optional, implement a custom logger */const customLogger: typeof DefaultLogger = {  trace: (...params: LogParams): void => {    console.log('silly', ...params);  },  info: (...params: LogParams): void => {    console.info(params);  },  error: (...params: LogParams): void => {    console.error(params);  },}; async function start() {  const client = new WebsocketClient(undefined, customLogger);   client.on('open', (data) => {    console.log('open: ', data?.wsKey);  });   // Data received  client.on('update', (data) => {    console.info('update: ', 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);  });   client.on('authenticated', (data) => {    console.error('authenticated: ', data);  });   try {    client.subscribe('spot/ticker:BTC_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.