-1

I was trying to remove all event listeners from a socket, to a specific event, and found this accepted answer on StackOveflow.

I followed the answer, and tried the following: socket.off('listenerName') to remove all listeners from the listenerName event, but this code gives me an error:

(node:27706) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type Function. Received type undefined

I am using the latest version (2.3.0) of socket.io. So it looks like, that the socket.off() method needs at least 2 parameters to work, and I have to explicitly tell it which function I want to remove. I tried searching socket.io's documentation to find the definition of this function, but all I found is that the socket inherits every method from the Emiter, including the off() method as well.

Does anyone know if there is a function in socket.io, with which I can remove all listeners from a specific event, without explicitly providing it a reference to the originally added function?

Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68
  • I don't know how you got from an answer citing `socket.off('event_name')` amd `socket.off('event_name', listener)` to your code of `socket.off('listenerName')`. Try reading more carefully. – user207421 Feb 08 '20 at 02:04
  • I think what you are saying, is just a naming problem, in the following line, what would you call `event_name`? `socket.on('disconnect', function() {});` ? because I would call `listenerName` the disconnect string. And by doing `socket.off('disconnect')` with socket.io 2.3.0, I get the above error. So if I am mistaken, and the `event_name` is not the same which I call `listenerName`, then pardon me, and I will correct my answer. but if not, maybe you should read more carefully, before downvoting and commenting. – Adam Baranyai Feb 09 '20 at 09:48

1 Answers1

0

I found the solution in another question.

I've managed to remove all the event listeners from the specific event with:

socket.removeAllListeners("news");
Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68