binance icon indicating copy to clipboard operation
binance copied to clipboard

Correct method to unsubscribe from WebSocket events?

Open TimUnderhay opened this issue 3 years ago • 2 comments

I'm wondering: what is the correct method for unsubscribing from a WS subscription, like say wsClient.subscribeSpotKline(...params)? Thanks in advance.

TimUnderhay avatar Sep 17 '21 08:09 TimUnderhay

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.

tiagosiebler avatar Sep 17 '21 09:09 tiagosiebler

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

mr-smit avatar Nov 24 '22 17:11 mr-smit