Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sergiocard opened this issue Nov 30, 2021 · 5 comments
Open

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

sergiocard opened this issue Nov 30, 2021 · 5 comments

Comments

@sergiocard
Copy link

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants