binance
binance copied to clipboard
undefined "terminate" function while calling close on websocket
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
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
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
Will be fixed in the next release (2.0.18), available shortly. Please let me know once you've had a chance to test.
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
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.