HTX Spot JavaScript SDK example: publicWs.ts

HTX Spot public 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/publicWs.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/publicWs.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 false;    // 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 client = new WebsocketClient({}, customLogger);   client    .on('open', (data) => console.log('open:', 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 market websocket: wss://api-aws.huobi.pro/ws by default.   */   client.subscribe(    [      'market.btcusdt.kline.1min',      'market.btcusdt.kline.5min',      'market.btcusdt.ticker',      'market.btcusdt.depth.step0',      'market.btcusdt.trade.detail',      'market.btcusdt.bbo',    ],    WS_KEY_MAP.spotPublic,  );   /**   * Spot feed websocket: wss://api-aws.huobi.pro/feed by default.   * Use this for feed-specific/high-frequency topics such as BBO and MBP.   *   * E.g. https://www.htx.com/en-us/opend/newApiPages/?id=7ec5362b-7773-11ed-9966-0242ac110003   */  client.subscribe(    ['market.btcusdt.mbp.5', 'market.btcusdt.trade.detail'],    WS_KEY_MAP.spotFeed,  );} start(); 

Subscribe on Substack

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