diff --git a/src/websocket-client.ts b/src/websocket-client.ts index 55fbf37..4e145a2 100644 --- a/src/websocket-client.ts +++ b/src/websocket-client.ts @@ -109,11 +109,78 @@ export class WebsocketClient extends EventEmitter { } /** - * Only used if we fetch exchange time before attempting auth. - * Disabled by default. + * Subscribe to topics & track/persist them. They will be automatically resubscribed to if the connection drops/reconnects. + * @param wsTopics topic or list of topics + * @param isPrivateTopic optional - the library will try to detect private topics, you can use this to mark a topic as private (if the topic isn't recognised yet) + */ + public subscribe(wsTopics: WsTopic[] | WsTopic, isPrivateTopic?: boolean) { + const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics]; + + topics.forEach((topic) => + this.wsStore.addTopic( + getWsKeyForTopic(this.options.market, topic, isPrivateTopic), + topic + ) + ); + + // attempt to send subscription topic per websocket + this.wsStore.getKeys().forEach((wsKey: WsKey) => { + // if connected, send subscription request + if ( + this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED) + ) { + return this.requestSubscribeTopics(wsKey, [ + ...this.wsStore.getTopics(wsKey), + ]); + } + + // start connection process if it hasn't yet begun. Topics are automatically subscribed to on-connect + if ( + !this.wsStore.isConnectionState( + wsKey, + WsConnectionStateEnum.CONNECTING + ) && + !this.wsStore.isConnectionState( + wsKey, + WsConnectionStateEnum.RECONNECTING + ) + ) { + return this.connect(wsKey); + } + }); + } + + /** + * Unsubscribe from topics & remove them from memory. They won't be re-subscribed to if the connection reconnects. + * @param wsTopics topic or list of topics + * @param isPrivateTopic optional - the library will try to detect private topics, you can use this to mark a topic as private (if the topic isn't recognised yet) + */ + public unsubscribe(wsTopics: WsTopic[] | WsTopic, isPrivateTopic?: boolean) { + const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics]; + topics.forEach((topic) => + this.wsStore.deleteTopic( + getWsKeyForTopic(this.options.market, topic, isPrivateTopic), + topic + ) + ); + + this.wsStore.getKeys().forEach((wsKey: WsKey) => { + // unsubscribe request only necessary if active connection exists + if ( + this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED) + ) { + this.requestUnsubscribeTopics(wsKey, [ + ...this.wsStore.getTopics(wsKey), + ]); + } + }); + } + + /** + * @private Only used if we fetch exchange time before attempting auth. Disabled by default. * I've removed this for ftx and it's working great, tempted to remove this here */ - prepareRESTClient(): void { + private prepareRESTClient(): void { switch (this.options.market) { case 'inverse': { this.restClient = new InverseClient( @@ -509,7 +576,7 @@ export class WebsocketClient extends EventEmitter { } /** - * Send WS message to subscribe to topics. + * @private Use the `subscribe(topics)` method to subscribe to topics. Send WS message to subscribe to topics. */ private requestSubscribeTopics(wsKey: WsKey, topics: string[]) { if (!topics.length) { @@ -544,7 +611,7 @@ export class WebsocketClient extends EventEmitter { } /** - * Send WS message to unsubscribe from topics. + * @private Use the `unsubscribe(topics)` method to unsubscribe from topics. Send WS message to unsubscribe from topics. */ private requestUnsubscribeTopics(wsKey: WsKey, topics: string[]) { if (!topics.length) { @@ -814,74 +881,6 @@ export class WebsocketClient extends EventEmitter { ); } - /** - * Add topic/topics to WS subscription list - * @param wsTopics topic or list of topics - * @param isPrivateTopic optional - the library will try to detect private topics, you can use this to mark a topic as private (if the topic isn't recognised yet) - */ - public subscribe(wsTopics: WsTopic[] | WsTopic, isPrivateTopic?: boolean) { - const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics]; - - topics.forEach((topic) => - this.wsStore.addTopic( - getWsKeyForTopic(this.options.market, topic, isPrivateTopic), - topic - ) - ); - - // attempt to send subscription topic per websocket - this.wsStore.getKeys().forEach((wsKey: WsKey) => { - // if connected, send subscription request - if ( - this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED) - ) { - return this.requestSubscribeTopics(wsKey, [ - ...this.wsStore.getTopics(wsKey), - ]); - } - - // start connection process if it hasn't yet begun. Topics are automatically subscribed to on-connect - if ( - !this.wsStore.isConnectionState( - wsKey, - WsConnectionStateEnum.CONNECTING - ) && - !this.wsStore.isConnectionState( - wsKey, - WsConnectionStateEnum.RECONNECTING - ) - ) { - return this.connect(wsKey); - } - }); - } - - /** - * Remove topic/topics from WS subscription list - * @param wsTopics topic or list of topics - * @param isPrivateTopic optional - the library will try to detect private topics, you can use this to mark a topic as private (if the topic isn't recognised yet) - */ - public unsubscribe(wsTopics: WsTopic[] | WsTopic, isPrivateTopic?: boolean) { - const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics]; - topics.forEach((topic) => - this.wsStore.deleteTopic( - getWsKeyForTopic(this.options.market, topic, isPrivateTopic), - topic - ) - ); - - this.wsStore.getKeys().forEach((wsKey: WsKey) => { - // unsubscribe request only necessary if active connection exists - if ( - this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED) - ) { - this.requestUnsubscribeTopics(wsKey, [ - ...this.wsStore.getTopics(wsKey), - ]); - } - }); - } - /** @deprecated use "market: 'spotv3" client */ public subscribePublicSpotTrades(symbol: string, binary?: boolean) { if (this.options.market !== 'spot') {