tiagosiebler / ftx-api

Node.js connector for the FTX.com & FTX.us APIs and WebSockets, with TypeScript & browser support.
https://www.npmjs.com/package/ftx-api
MIT License
123 stars 47 forks source link

Is it possible to completely disconnect? #76

Closed kenchambers closed 1 year ago

kenchambers commented 2 years ago

I noticed that after I instantiate (the websocket) client, and then try to ws.close I am still connected. Is it possible to terminate connection completely? I am asking because I am opening WS inside of a background job and I am trying to avoid memory leaks:

pseudo code repro:


class FtxUS {
  constructor({ apiKey, apiSecret, market = null }) {
    this.market = market;
    this.apiKey = apiKey;
    this.apiSecret = apiSecret;
    this.client = this.initializeRestClient();
  }
  async initializeWsClient() {
    let wsClient = new WebsocketClient({
      key: this.apiKey,
      secret: this.apiSecret,
      wsUrl: 'wss://ftx.us/ws/',
    });
    return await wsClient;
  }

  testCurrentCoinPriceStream() {
    this.turnOnStream(['BAT/USD']);

    setTimeout(async () => {
      await this.turnOffStream(['BAT/USD']);
    }, 10000);
  }

  turnOnStream = async (pairs = []) => {
    this.ws = await this.initializeWsClient();

    let channelArray = [];
    pairs.forEach(pair => {
      channelArray.push({ channel: 'ticker', market: pair });
    });

    try {
      this.ws.on('response', msg => {});
      this.ws.on('update', msg => {
        this.parseWebSockets(msg);
      });
      // this.ws.on('error', msg => console.log('err: ', msg));
      this.ws.subscribe(channelArray);
    } catch (err) {
      goodError(err);
    }
  };

  async turnOffStream(pairs = []) {
    let channelArray = [];
    pairs.forEach(pair => {
      channelArray.push({ channel: 'ticker', market: pair });
    });
    await this.ws.unsubscribe(channelArray);
    await this.ws.close('update');
    await this.ws.close('ticker');
    await this.ws.close('response');
  }
}

as you can see, connection is not closing:

Screen Shot 2022-05-12 at 4 18 56 AM
tiagosiebler commented 2 years ago

Small tip, you can also set the client to use the US domain via the domain property: https://github.com/tiagosiebler/ftx-api/blob/8990f20b6bcb2968c77cf80c394a5b6db489bd8c/src/util/requestUtils.ts#L51

I'm surprised the wsclient.close() didn't do the trick, will need to investigate that. Thanks for reporting it.

Would a teardown or teardownAll method on the ws client be helpful, specifically to close and clean up everything?

kenchambers commented 2 years ago

absolutely!

thanks for response.

Or maybe a flag that you can pass into the client that would disable reconnect. since it is by default set ?

tiagosiebler commented 2 years ago

absolutely!

thanks for response.

Or maybe a flag that you can pass into the client that would disable reconnect. since it is by default set ?

Or both might be nice in this case. Would also avoid the confusion of managing all the close(closewhat) calls

kenchambers commented 2 years ago

Another library I saw has this:

turnOffStream() {
    let endpoints = this.binanceWs.websockets.subscriptions();
    for (let endpoint in endpoints) {
      this.binanceWs.websockets.terminate(endpoint);
    }
  }

as well as the ability to pass a disable for reconnect:

const binanceWs = new Binance({
      APIKEY: this.apiKey,
      APISECRET: this.apiSecret,
      reconnect: false,
    });