Use this file to discover all available pages before exploring further.
The WebsocketClient makes it straightforward to receive live data from Gate.com — from ticker prices and order books to your own private order fills and balance changes. You subscribe to one or more topics on a specific WsKey (the connection identifier), attach event handlers, and the SDK delivers a continuous stream of updates. All connection management, authentication, and reconnection logic runs in the background without any extra code on your part.
subscribe(requests, wsKey) accepts a single topic or an array of topics, plus the WsKey that identifies which WebSocket connection to use. The connection is opened automatically if it is not already active.Topics can be passed as plain strings (for topics that require no parameters) or as WsTopicRequest objects containing a topic and an optional payload array:
// Single string topic — no parametersws.subscribe('spot.balances', 'spotV4');// Single object topic — with parametersws.subscribe({ topic: 'spot.tickers', payload: ['BTC_USDT'] }, 'spotV4');// Array of mixed strings and objects in one callws.subscribe( [ 'spot.balances', { topic: 'spot.tickers', payload: ['BTC_USDT', 'ETH_USDT'] }, { topic: 'spot.trades', payload: ['BTC_USDT'] }, ], 'spotV4',);
unsubscribe(requests, wsKey) mirrors subscribe() exactly. The SDK removes the topics from its internal cache so they are not resubscribed after a reconnect:
Private topics stream your own account activity such as order fills, balance changes, and price-triggered orders. The client authenticates the connection automatically when it detects a private topic — no explicit login call is needed.
Attach handlers using ws.on(event, callback). All handlers receive an object that always includes the wsKey that fired the event.
import { WebsocketClient } from 'gateio-api';const ws = new WebsocketClient({ apiKey: process.env.API_KEY, apiSecret: process.env.API_SECRET,});// Primary data handler — fires for every incoming stream messagews.on('update', (data) => { console.info('data received:', JSON.stringify(data));});// Fires when a WebSocket connection is first openedws.on('open', ({ wsKey }) => { console.log('connection open:', wsKey);});// Fires when the server sends a reply to subscribe/unsubscribe/auth requestsws.on('response', (data) => { console.info('server reply:', JSON.stringify(data));});// Fires when a connection closes — if unexpected, a reconnect will followws.on('close', ({ wsKey }) => { console.error('connection closed:', wsKey);});// Fires after a successful automatic reconnectws.on('reconnected', ({ wsKey }) => { console.log('reconnected:', wsKey);});// Fires when an internal SDK exception occurs (e.g. authentication failure)ws.on('exception', (data) => { console.error('exception:', data);});// Fires on raw WebSocket-level errorsws.on('error', (err) => { console.error('ws error:', err);});