Gate JavaScript SDK example: ws-public.ts

Gate websocket public JavaScript example. Read the source code, review its SDK calls, and browse related exchange integration examples.

This example uses gateio-api, the JavaScript and TypeScript SDK for Gate. Install it with npm install gateio-api.

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-public.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-public.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-next-line @typescript-eslint/no-unused-varsimport { WebsocketClient, WsTopicRequest } from 'gateio-api'; // const customLogger = {//   // eslint-disable-next-line @typescript-eslint/no-unused-vars//   trace: (...params: LogParams): void => {//     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();   // Optional, inject a custom logger  // const client = new WebsocketClient({}, customLogger);   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 {    const tickersRequestWithParams: WsTopicRequest = {      topic: 'spot.tickers',      payload: ['BTC_USDT', 'ETH_USDT', 'MATIC_USDT'],    };     const rawTradesRequestWithParams: WsTopicRequest = {      topic: 'spot.trades',      payload: ['BTC_USDT', 'ETH_USDT', 'MATIC_USDT'],    };     // 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',    // };     /**     * Either send one topic (with optional params) at a time     */    // client.subscribe(tickersRequestWithParams, 'spotV4');     /**     * Or send multiple topics in a batch (grouped by ws connection (WsKey))     */    client.subscribe(      [tickersRequestWithParams, rawTradesRequestWithParams],      'spotV4',    );     // /**    //  * 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(    //   [tickersRequestWithParams, rawTradesRequestWithParams, topicWithoutParamsAsString],    //   '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.