Gate JavaScript SDK example: ws-private-spot.ts

Gate websocket private spot JavaScript example for the Siebly Gate 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/Gate/Websocket/ws-private-spot.ts

What this example covers

  • Gate WebSocket stream JavaScript example.
  • Uses the Siebly Gate SDK package gateio-api instead of hand-written WebSocket plumbing.
  • Source path: Gate/Websocket/ws-private-spot.ts.
  • Imports SDK symbols including 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.
/* eslint-disable @typescript-eslint/no-unused-vars */import { LogParams, WebsocketClient, WsTopicRequest } from 'gateio-api'; // eslint-disable-next-line @typescript-eslint/no-unused-varsconst account = {  key: process.env.API_KEY || 'apiKeyHere',  secret: process.env.API_SECRET || 'apiSecretHere',}; // Define a custom logger object to handle logging at different levelsconst customLogger = {  // Trace level logging: used for detailed debugging information  trace: (...params: LogParams): void => {    // Uncomment the line below to enable trace logging    // console.log(new Date(), 'trace', ...params);  },  // Info level logging: used for general informational messages  info: (...params: LogParams): void => {    console.log(new Date(), 'info', ...params);  },  // Error level logging: used for error messages  error: (...params: LogParams): void => {    console.error(new Date(), 'error', ...params);  },}; async function start() {  const client = new WebsocketClient(    {      apiKey: account.key,      apiSecret: account.secret,    },    customLogger,  );   // console.log('auth with: ', account);  client.on('open', (data) => {    console.log('connected ', data?.wsKey);  });   // Data received  client.on('update', (data) => {    console.info('data received: ', JSON.stringify(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('server reply: ', JSON.stringify(data), '\n');  });   client.on('exception', (data) => {    console.error('exception: ', data);  });   client.on('authenticated', (data) => {    console.error('authenticated: ', data);  });   try {    // client.subscribe(topics, 'spotV4');     const topicWithoutParamsAsString = 'spot.balances';     // This has the same effect as above, it's just a different way of messaging which topic this is for    // const topicWithoutParamsAsObject: WsTopicRequest = {    //   topic: 'spot.balances',    // };     const userOrders: WsTopicRequest = {      topic: 'spot.orders',      payload: ['BTC_USDT', 'ETH_USDT', 'MATIC_USDT'],    };     const userTrades: WsTopicRequest = {      topic: 'spot.usertrades',      payload: ['BTC_USDT', 'ETH_USDT', 'MATIC_USDT'],    };     const userPriceOrders: WsTopicRequest = {      topic: 'spot.priceorders',      payload: ['!all'],    };     /**     * Either send one topic (with optional params) at a time     */    // client.subscribe(topicWithoutParamsAsObject, 'spotV4');     /**     * Or send multiple topics in a batch (grouped by ws connection (WsKey))     * You can also use strings for topics that don't have any parameters, even if you mix multiple requests into one function call:     */    client.subscribe(      [topicWithoutParamsAsString, userOrders, userTrades, userPriceOrders],      'spotV4',    );     /**     * You can also subscribe in separate function calls as you wish.     *     * Any duplicate requests should get filtered out (e.g. here we subscribed to "spot.balances" twice, but the WS client will filter this out)     */    client.subscribe(      [        'spot.balances',        'spot.margin_balances',        'spot.funding_balances',        'spot.cross_balances',      ],      'spotV4',    );  } 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.