HTX Spot JavaScript SDK example: privateWs.ts

HTX Spot private websocket JavaScript example for the Siebly HTX 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/HTX/Spot/WebSockets/privateWs.ts

What this example covers

  • HTX WebSocket stream JavaScript example.
  • Uses the Siebly HTX SDK package @siebly/htx-api instead of hand-written WebSocket plumbing.
  • Source path: HTX/Spot/WebSockets/privateWs.ts.
  • Example category: Spot.
  • Imports SDK symbols including DefaultLogger, WebsocketClient, WS_KEY_MAP.
  • Calls SDK methods such as 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.
/* eslint-disable @typescript-eslint/no-unused-vars */import {  DefaultLogger,  LogParams,  WebsocketClient,  WS_KEY_MAP,} from '@siebly/htx-api'; function isRecord(value: unknown): value is Record<string, unknown> {  return typeof value === 'object' && value !== null;} function isMutedTrace(params: LogParams): boolean {  const [message, details] = params;   if (message === 'Received PING event') {    return true;  }   if (message === 'onWsMessage().emit(message)') {    return true;  }   if (    typeof message === 'string' &&    message.startsWith('getFinalEmittable()->pre()')  ) {    return true;  }   if (    message === 'Sending upstream ws message: ' &&    isRecord(details) &&    typeof details.wsMessage === 'string'  ) {    // return false;    return !details.wsMessage.includes('"pong"');  }   return false;} const customLogger: DefaultLogger = {  trace: (...params: LogParams): void => {    // if (isMutedTrace(params)) {    //   return;    // }    // console.log('trace', params);  },  info: (...params: LogParams): void => {    console.log('info', ...params);  },  error: (...params: LogParams): void => {    console.error('error', ...params);  },}; async function start() {  const account = {    key: process.env.API_KEY || '',    secret: process.env.API_SECRET || '',  };   const client = new WebsocketClient(    {      apiKey: account.key,      apiSecret: account.secret,    },    customLogger,  );   client    .on('open', (data) => console.log('open:', data.wsKey))    .on('authenticated', (data) => console.log('authenticated:', data.wsKey))    .on('message', (data) => console.info('message:', JSON.stringify(data)))    .on('response', (data) => console.info('response:', JSON.stringify(data)))    .on('reconnecting', (data) => console.log('reconnecting:', data.wsKey))    .on('reconnected', (data) => console.log('reconnected:', data.wsKey))    .on('close', (data) => console.log('close:', data.wsKey))    .on('exception', (data) => console.error('exception:', data));   /**   * Spot private websocket: wss://api-aws.huobi.pro/ws/v2 by default.   *   * The SDK signs and sends the auth request automatically before subscribing.   */   client.subscribe(    [      // not specifying "mode", Only update when account balance changed;      'accounts.update',      // Specify "mode" as 0, Only update when account balance changed;      'accounts.update#0',      // Specify "mode" as 1, Update when either account balance changed or available balance changed.      'accounts.update#1',      // Specify "mode" as 2, Whenever account balance or available balance changed, it will be updated together.      'accounts.update#2',      // Subscribe to order updates for all symbols (wildcard allowed)      'orders#*',      // Subscribe to order updates for btcusdt only      'orders#btcusdt',      // Subscribe to trade clearing for all symbols (wildcard allowed) (mode is optional)      'trade.clearing#*',      // Subscribe to trade clearing for btcusdt only (mode is optional)      'trade.clearing#btcusdt',      // Subscribe to trade clearing for all symbols, mode 0 (trade event only (default))      'trade.clearing#*#0',      // Subscribe to trade clearing for all symbols, mode 1 (trade & cancellation events)      'trade.clearing#*#1',    ],    WS_KEY_MAP.spotPrivateV2,  );} start(); 

Subscribe on Substack

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