tiagosiebler / binance

Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & browser support, integration tests, beautification & more.
MIT License
751 stars 266 forks source link

undefined "terminate" function while calling close on websocket #168

Open sergiocard opened 2 years ago

sergiocard commented 2 years ago

Hi all i'm trying following code installing this lib on react browser app.

import { WebsocketClient } from 'binance'
const wsClient = new WebsocketClient({
  beautify: true
})
wsClient.subscribeSpotAllMini24hrTickers()

The websocket is connected successfully and receive data on network but after some seconds i get this error:

websocket-client.js?069d:251 Uncaught TypeError: _b.terminate is not a function
    at WebsocketClient.close (websocket-client.js?069d:251)
    at eval (websocket-client.js?069d:234)

that correspond to the following source code:

    close(wsKey, willReconnect) {
        var _a, _b;
        this.logger.info('Closing connection', Object.assign(Object.assign({}, loggerCategory), { wsKey }));
        this.setWsState(wsKey, willReconnect ? READY_STATE_RECONNECTING : READY_STATE_CLOSING);
        this.clearTimers(wsKey);
        (_a = this.getWs(wsKey)) === null || _a === void 0 ? void 0 : _a.close();
        (_b = this.getWs(wsKey)) === null || _b === void 0 ? void 0 : _b.terminate();
    }

it seems the close function is called and terminate() method is not present into _b object

tiagosiebler commented 2 years ago

Thanks for the details - seems like the websocket object in browsers doesn't have that method. I wonder if it's not necessary in browser environments (where websocket lifecycles behave slightly differently)...

This is the line that's calling terminate() https://github.com/tiagosiebler/binance/blob/master/src/websocket-client.ts#L363

tiagosiebler commented 2 years ago

Also I wonder if it's the heartbeat mechanism thinking that the websocket dropped. I haven't yet added a way to turn off the heartbeat mechanism completely (but I plan in doing so), but in the meantime you can try to increase the heartbeat interval to a large number.

Set pingInterval: number to a high value - it's in milliseconds and controls how often the ws client sends a heartbeat to the server. You can also increase how much time you allow for the heartbeat to get a reply from the exchange server, which should reduce the chances of this happening: pongTimeout: number - this is also in milliseconds. https://github.com/tiagosiebler/binance/blob/master/src/websocket-client.ts#L48-L49

tiagosiebler commented 2 years ago

Will be fixed in the next release (2.0.18), available shortly. Please let me know once you've had a chance to test.

sergiocard commented 2 years ago

tested. Terminate undefined function error not present but after a few seconds, It tries to ping binance but an error occurs. You can view the used code and error log on following attached image

Schermata 2021-12-08 alle 15 21 04

tiagosiebler commented 2 years ago

Ah I see - I don't think the heartbeat mechanism will work at all in the browser, although perhaps it's also not necessary there. You can disable it as of the latest version by passing disableHeartbeat: true in the constructor of the websocket-client. That'll prevent the connector from trying to use these methods in the heartbeat mechanism, more intended for backend environments.