TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tiagosiebler/gateio-api/llms.txt
Use this file to discover all available pages before exploring further.
WebsocketClient is the core class for all real-time streaming in the gateio-api SDK. It manages persistent WebSocket connections to Gate.com’s various server endpoints, handles authentication for private streams, automatically reconnects when a connection drops, and delivers market data and account events through a familiar Node.js EventEmitter interface. Whether you need live ticker prices, order book updates, or private order and balance notifications, the WebsocketClient is your single entry point.
The WsKey System
Each Gate.com WebSocket service runs on its own server URL. The SDK abstracts this with a WsKey — a short string constant that maps one-to-one to a unique WebSocket connection. When you callsubscribe() or unsubscribe(), you always pass a WsKey alongside the topic so the SDK knows which underlying connection to use.
All available WsKeys are exported from the WS_KEY_MAP constant:
| WsKey | Market | Live Endpoint |
|---|---|---|
spotV4 | Spot & Margin | wss://api.gateio.ws/ws/v4/ |
perpFuturesUSDTV4 | Perpetual Futures (USDT) | wss://fx-ws.gateio.ws/v4/ws/usdt |
perpFuturesBTCV4 | Perpetual Futures (BTC) | wss://fx-ws.gateio.ws/v4/ws/btc |
deliveryFuturesUSDTV4 | Delivery Futures (USDT) | wss://fx-ws.gateio.ws/v4/ws/delivery/usdt |
deliveryFuturesBTCV4 | Delivery Futures (BTC) | wss://fx-ws.gateio.ws/v4/ws/delivery/btc |
optionsV4 | Options | wss://op-ws.gateio.live/v4/ws |
announcementsV4 | Announcements | wss://api.gateio.ws/ws/v4/ann |
Event Model
WebsocketClient extends EventEmitter. All real-time data and lifecycle notifications arrive as named events. Attach handlers before calling subscribe():
| Event | When it fires | Payload |
|---|---|---|
update | A stream data message arrives | { data, wsKey } |
open | A connection is first established | { wsKey, event, wsUrl, ws } |
response | Server replies to a subscribe/unsubscribe/auth request | { wsKey, ...replyFields } |
close | A connection closes (expected or unexpected) | { wsKey, event } |
reconnected | Auto-reconnect succeeded | { wsKey, event, wsUrl, ws } |
reconnect | Auto-reconnect is about to be attempted | { wsKey, event } |
authenticated | The connection has been authenticated | { wsKey, event } |
exception | An internal SDK exception occurred | { wsKey, ...details } |
error | A raw WebSocket-level error occurred | error object |
Connection Lifecycle
TheWebsocketClient manages connections fully automatically:
- Auto-connect on subscribe — calling
ws.subscribe(topic, wsKey)establishes the WebSocket connection for that WsKey if one is not already open. You do not need to call aconnect()method first. - Heartbeat / ping-pong — the SDK sends ping frames on a regular interval and monitors pong replies. If a pong does not arrive within the timeout window, the connection is treated as dead and a reconnect is initiated.
- Auto-reconnect — when a connection closes unexpectedly the SDK waits for
reconnectTimeoutmilliseconds then reopens it and resubscribes to all previously-active topics automatically. - Subscription replay — because subscriptions are cached internally, every topic you subscribed to before a disconnect is resubscribed immediately after reconnect, with no extra code on your side.
subscribe() call, you can call connectAll():
Authentication
Private topics (orders, balances, positions, and so on) require an authenticated connection. Pass your API credentials in the constructor and the SDK authenticates the connection automatically before the first private subscription is sent:Configuration Options
All options are defined by theWSClientConfigurableOptions interface and passed to the constructor:
Data Streams
Subscribe to public market data and private account events using the WebsocketClient.
WebSocket API
Place and cancel orders in real time over a WebSocket connection.