You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+81-75Lines changed: 81 additions & 75 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -252,108 +252,114 @@ See [WebsocketClient](./src/WebsocketClient.ts) for further information and make
252
252
253
253
### Websocket API
254
254
255
-
The [WebsocketClient](./src/WebsocketClient.ts) supports this exchange's Websocket API. There are two ways to use the WS API, depending on individual preference:
255
+
Gate.com supports sending requests (commands) over an active WebSocket connection. This is called the WebSocket API.
256
+
257
+
The WebSocket API is available through two approaches, depending on individual preference:
258
+
259
+
#### Event Driven API
260
+
261
+
The WebSocket API is available in the [WebsocketClient](./src/WebsocketClient.ts) via the `sendWSAPIRequest(wsKey, channel, params)` method.
262
+
263
+
Each call to this method is wrapped in a promise, which you can async await for a response, or handle it in a raw event-driven design.
256
264
257
265
- event-driven:
258
-
- send requests via `client.sendWSAPIRequest(wsKey, channel, params).catch(e => console.error('WS API
259
-
exception for channel', channel, e))`, fire and forget, don't use await
266
+
- send requests via `client.sendWSAPIRequest(wsKey, channel, params).catch(e => console.error('WS API exception for channel', channel, e))`, fire and forget, don't use await
260
267
- handle async replies via event handlers on `client.on('exception', cb)` and `client.on('response', cb)`
261
268
- promise-driven:
262
269
- send requests via `const result = await client.sendWSAPIRequest(wsKey, channel, params)`, which returns a promise
263
270
- await each call
264
271
- use try/catch blocks to handle promise rejections
265
272
266
-
The below example demonstrates the promise-driven approach, which behaves similar to a REST API. For more detailed examples, refer to the [examples](./examples/) folder (e.g the [ws-private-spot-wsapi.ts](./examples/ws-private-spot-wsapi.ts) example).
273
+
#### REST-like API
274
+
275
+
The WebSocket API is also available in a promise-wrapped REST-like format. Either, as above, await any calls to `sendWSAPIRequest(...)`, or directly use the convenient WebsocketAPIClient. This class is very similar to existing REST API classes (such as the RestClient).
276
+
277
+
It provides one function per endpoint, feels like a REST API and will automatically route your request via an automatically persisted, authenticated and health-checked WebSocket API connection.
278
+
279
+
Below is an example showing how easy it is to use the WebSocket API without any concern for the complexity of managing WebSockets.
* To authenticate, send an empty request to "spot.login". The SDK will handle all the parameters.
326
-
*
327
-
* By default (reauthWSAPIOnReconnect: true), if we get reconnected later on (e.g. connection temporarily lost), we will try to re-authenticate the WS API automatically when the connection is restored.
0 commit comments