Binance TypeScript SDK example: ws-demo-usdm.ts

Binance WebSocket Demo websocket demo usdm 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/Demo/ws-demo-usdm.ts.
  • Example category: WebSocket Demo.
  • Imports SDK symbols including WebsocketClient.
  • Calls SDK methods such as on(), subscribeKlines(), subscribeUsdFuturesUserDataStream().

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/Demo/ws-demo-usdm.ts

Source Link

Repository source: https://github.com/sieblyio/crypto-api-examples/blob/master/examples/Binance/WebSockets/Demo/ws-demo-usdm.ts

Related SDK Docs

Example Source

import { WebsocketClient } from 'binance';

const key = process.env.API_KEY_COM || 'APIKEY';
const secret = process.env.API_SECRET_COM || 'APISECRET';

async function start() {
  const wsClient = new WebsocketClient({
    api_key: key,
    api_secret: secret,
    beautify: true,
    /**
     * Demo trading uses real market data with simulated trading.
     * Perfect for testing strategies without risk.
     */
    demoTrading: true,
  });

  wsClient.on('formattedMessage', (data) => {
    console.log('Demo WS data: ', JSON.stringify(data, null, 2));
  });

  wsClient.on('open', (data) => {
    console.log('Demo WS connection opened:', data.wsKey);
  });

  wsClient.on('response', (data) => {
    console.log('Demo WS response: ', JSON.stringify(data, null, 2));
  });

  wsClient.on('reconnected', (data) => {
    console.log('Demo WS reconnected ', data?.wsKey);
  });

  wsClient.on('exception', (data) => {
    console.error('Demo WS error', data);
  });

  // Subscribe to USDM futures market streams on demo trading
  wsClient.subscribeKlines('BTCUSDT', '1m', 'usdm');
  wsClient.subscribeUsdFuturesUserDataStream();
}

start();