How do you stop listening to a socket.io channel?
If you want to remove only a specific event you can try socket.removeListener('you_event');
The off
function can also be used.
socket.off('news'); // stops listening to the "news" event
socket.off('news', myFunction); // useful if you have multiple listeners for the same event
socket.off(); // stops listening to all events
Sources
According to the Socket.io Client Documentation "the socket actually inherits every method of the Emitter class, like hasListeners
, once
or off
(to remove an event listener)."
Emitter documentation:
- Pass
event
andfn
to remove a listener.- Pass
event
to remove all listeners on that event.- Pass nothing to remove all listeners on all events.
Try, socket.removeAllListeners("news");