binance
binance copied to clipboard
Correct method to unsubscribe from WebSocket events?
I'm wondering: what is the correct method for unsubscribing from a WS subscription, like say wsClient.subscribeSpotKline(...params)
? Thanks in advance.
This is admittedly not intuitive at the moment. The subscribe method returns a websocket object. You should be able to request closure using that ws object: wsClient.closeWs(ws)
:
https://github.com/tiagosiebler/binance/blob/master/src/websocket-client.ts#L366-L372
Would be good to add this to the readme or samples.
How to unsubscribe:
// subscribe
let ws_usdm = wsClient.subscribeSymbolBookTicker('BTCUSDT', 'usdm');
// unsubscribe
wsClient.closeWs(ws_usdm);
you can store ws variables in array and use array to closeWs in loop