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.
gateio-api instead of hand-written WebSocket plumbing.Gate/Websocket/ws-public.ts.WebsocketClient.on(), subscribe().// 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(); We use essential cookies and optional analytics. Read the Privacy Policy.
Essential cookies stay on. Toggle analytics if you want to share anonymous usage insights. You can revisit this anytime via Cookie Settings in the footer.