From 7fa82e099af5699c91b0bb5dfcd1d6d40784b1ca Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Wed, 28 Sep 2022 09:55:48 +0100 Subject: [PATCH] expose ws store --- examples/ws-public.ts | 21 +++++++++++++++++++-- package.json | 2 +- src/websocket-client.ts | 5 +++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/examples/ws-public.ts b/examples/ws-public.ts index 8834d5a..cba4c71 100644 --- a/examples/ws-public.ts +++ b/examples/ws-public.ts @@ -60,6 +60,8 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src'; // Spot V3 // wsClient.subscribe('trade.BTCUSDT'); + // Or an array of topics + // wsClient.subscribe(['trade.BTCUSDT', 'trade.LTCUSDT']); // usdc options // wsClient.subscribe([ @@ -74,11 +76,26 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src'; // unified perps wsClient.subscribe('publicTrade.BTCUSDT'); + // For spot v1 (the old, deprecated client), request public connection first then send required topics on 'open' + // Not necessary for spot v3 + // wsClient.connectPublic(); + + // To unsubscribe from topics: // setTimeout(() => { // console.log('unsubscribing'); // wsClient.unsubscribe('trade.BTCUSDT'); // }, 5 * 1000); - // For spot, request public connection first then send required topics on 'open' - // wsClient.connectPublic(); + // Topics are tracked per websocket type + // Get a list of subscribed topics (e.g. for public v3 spot topics) + const publicSpotTopics = wsClient + .getWsStore() + .getTopics(WS_KEY_MAP.spotV3Public); + + console.log('public spot topics: ', publicSpotTopics); + + const privateSpotTopics = wsClient + .getWsStore() + .getTopics(WS_KEY_MAP.spotV3Private); + console.log('private spot topics: ', publicSpotTopics); })(); diff --git a/package.json b/package.json index acd1c4d..cc17ac8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bybit-api", - "version": "3.0.1", + "version": "3.0.2", "description": "Complete & robust node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & integration tests.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/websocket-client.ts b/src/websocket-client.ts index 3905a21..f3d78a8 100644 --- a/src/websocket-client.ts +++ b/src/websocket-client.ts @@ -174,6 +174,11 @@ export class WebsocketClient extends EventEmitter { } } + /** Get the WsStore that tracks websockets & topics */ + public getWsStore(): WsStore { + return this.wsStore; + } + public isTestnet(): boolean { return this.options.testnet === true; }