Binance TypeScript SDK example: ws-custom-parser.ts

Binance WebSocket Misc websocket custom parser example for the Siebly Binance SDK, with TypeScript source for exchange REST API and WebSocket integration, setup, and production SDK docs.

What This Example Covers

  • Binance WebSocket stream example in TypeScript.
  • Uses the Siebly Binance SDK package binance instead of hand-written WebSocket plumbing.
  • Source path: Binance/WebSockets/Misc/ws-custom-parser.ts.
  • Example category: WebSocket Misc.
  • Imports SDK symbols including WebsocketClient.

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.

Example Path

Binance/WebSockets/Misc/ws-custom-parser.ts

Source Link

Repository source: https://github.com/sieblyio/crypto-api-examples/blob/master/examples/Binance/WebSockets/Misc/ws-custom-parser.ts

Related SDK Docs

Example Source

// Optional, for 3rd scenario below:
// Import 3rd party library for parsing big number types in JSON
// import JSONbig from 'json-bigint';

import { WebsocketClient } from 'binance';

// Demonstrates using a custom JSON parser for incoming WS messages to preserve big integers

/**
 * ETHUSDT in futures can have unusually large orderId values, sent as numbers. See this thread for more details:
 * https://github.com/tiagosiebler/binance/issues/208
 *
 * If this is a problem for you, you can set a custom JSON parsing alternative using the customParseJSONFn hook injected into the WebsocketClient's constructor, as below:
 */
const ws = new WebsocketClient({
  // Default behaviour, if you don't include this:
  // customParseJSONFn: (rawEvent) => {
  //   return JSON.parse(rawEvent);
  // },
  // Or, pre-process the raw event using RegEx, before using the same workflow:
  customParseJSONFn: (rawEvent) => {
    return JSON.parse(
      rawEvent.replace(/"orderId":\s*(\d+)/g, '"orderId":"$1"'),
    );
  },
  // Or, use a 3rd party library such as json-bigint:
  // customParseJSONFn: (rawEvent) => {
  //   return JSONbig({ storeAsString: true }).parse(rawEvent);
  // },
});

ws.on('open', ({ wsKey }) => {
  console.log('ws connected', wsKey);
});

ws.on('message', (msg) => {
  console.log('msg:', msg);
});

// Subscribe to a couple of topics
// Note: '!ticker@arr' has been deprecated (2025-11-14). Using '!miniTicker@arr' instead.
ws.subscribe(['btcusdt@trade', '!miniTicker@arr'], 'main');