binance icon indicating copy to clipboard operation
binance copied to clipboard

undefined "terminate" function while calling close on websocket

Open sergiocard opened this issue 2 years ago • 5 comments

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

sergiocard avatar Nov 30 '21 16:11 sergiocard

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 avatar Dec 03 '21 15:12 tiagosiebler

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 avatar Dec 03 '21 15:12 tiagosiebler

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

tiagosiebler avatar Dec 08 '21 00:12 tiagosiebler

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

sergiocard avatar Dec 08 '21 14:12 sergiocard

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.

tiagosiebler avatar Dec 08 '21 23:12 tiagosiebler