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 class manages persistent WebSocket connections to Gate.io’s market data and private account streams. It handles authentication, heartbeat ping/pong cycles, automatic reconnection with topic re-subscription, and routing of both public and private topic subscriptions to the correct underlying connection. It also exposes a sendWSAPIRequest() method for executing REST-like trading commands entirely over WebSocket.
Installation and Import
Constructor
Configuration object for the WebSocket client. All fields are optional.
Your Gate.io API key. Required to subscribe to private topics or use the WS API.
Your Gate.io API secret. Required to subscribe to private topics or use the WS API.
Default:
false. When true, connects to Gate.io’s testnet WebSocket endpoints instead of production.Milliseconds added to the current timestamp when generating a private WebSocket request signature. Effectively shifts the signature timestamp forward to account for clock skew. Default:
0.How often (in milliseconds) the SDK sends a ping frame to each active connection to keep it alive. The exact format of the ping varies per Gate.io market (e.g.
spot.ping, futures.ping).How long (in milliseconds) to wait for a pong reply before considering the connection dead and triggering a reconnect.
Delay in milliseconds before attempting to re-establish a dropped connection.
Default:
true. When true, the SDK automatically re-authenticates any WS API connection that was previously authenticated after a reconnect. Up to 5 retry attempts are made. Emit events authenticated (success) or exception (type: wsapi.auth, if all retries fail) signal the outcome. Set to false to manage re-authentication manually.Override the WebSocket URL for all connections. Useful for proxying through a custom gateway.
Low-level request options passed to the underlying HTTP agent used for the WebSocket upgrade handshake.
Low-level options forwarded to the underlying
WebSocket constructor. Accepts protocols, agent, and any fields from WebSocket.ClientOptions or ClientRequestArgs.Provide a custom HMAC signing function. Useful for leveraging Node.js’s faster
createHmac rather than the Web Crypto API. See the examples folder for a demonstration.A custom logger instance. If omitted, the SDK uses its built-in logger which writes to the console.
WsKey Values
Each Gate.io WebSocket product is identified by aWsKey string. Pass the correct key when calling subscribe(), unsubscribe(), or sendWSAPIRequest():
| WsKey | Market |
|---|---|
'spotV4' | Spot (public + private) |
'perpFuturesUSDTV4' | USDT-settled perpetual futures |
'perpFuturesBTCV4' | BTC-settled perpetual futures |
'deliveryFuturesUSDTV4' | USDT-settled delivery futures |
'deliveryFuturesBTCV4' | BTC-settled delivery futures |
'optionsV4' | Options |
'announcementsV4' | System announcements |
WS_KEY_MAP to access these values without hardcoding strings:
Public Methods
connectAll()
subscribe() call to trigger lazy connection. Returns an array of promises that each resolve when the corresponding connection is ready.
connectWSAPI(wsKey, skipAuth?)
subscribe(topics, wsKey)
WsTopicRequest object, or an array of either. The SDK automatically:
- Opens the connection if not already active
- Authenticates if the topic is private and credentials are configured
- Resubscribes automatically after any reconnection
unsubscribe(topics, wsKey)
sendWSAPIRequest(wsKey, channel, params?, requestFlags?)
Promise that resolves with the server’s reply or rejects if an error response is received or the connection drops before the reply arrives.
The SDK automatically:
- Ensures the connection is open (
assertIsConnected) - Ensures the connection is authenticated (
assertIsAuthenticated) unlessrequestFlags.authIsOptionalistrue - Signs the request payload with your API secret
- Stores a deferred promise keyed to the request ID and resolves it when the matching reply arrives
Event Emitter Interface
WebsocketClient extends Node.js’s EventEmitter. Listen for events using the standard .on() method.
update
Emitted for every market data push or private account data push received from the server. This is the primary event for consuming real-time data.
open
Emitted when a WebSocket connection is successfully established for the first time. If a connection drops and is re-established, expect the reconnected event instead.
reconnect
Emitted when the SDK begins attempting to reconnect a dropped connection. This fires before the new connection is established; listen for reconnected to confirm the reconnection succeeded.
close
Emitted when a connection is closed.
response
Emitted when the server sends a reply to a subscription or unsubscription request. Also emitted for successful WS API command responses.
authenticated
Emitted when a private WebSocket connection (or WS API connection) has been successfully authenticated.
exception
Emitted when the SDK encounters an internal error, such as a failed WS API request (non-200 status), a failed re-authentication attempt, or a message parsing error.
error
Emitted for raw WebSocket-level errors from the underlying ws library.