user3776738
user3776738

Reputation: 254

How to remove io.socket namespace events?

How can I simply remove an io.socket namespace?

 delete io.nsps['/my_namespace'];

This doesn't prevent firering events from existing connected clients.

OK to point 1)

 nsp= io.of('/my_namespace');
 nsp.removeAllListeners("my_event"); //this doesnt' work

2.) Of course I could make an if statement for this,but I think this is not a clean solution.

3.)I don't know how to do this with a namespace without closing the complete socket(not just the namespace)) for the user.

My problem is,when I close a chat and delete all saving arrays and stuff, I get an error because events can still be fired(access on now undefined arrays,of course I could check if they exists but it was easier to just prevent events),like a send- or a disconnect- event.I don't want to prevent this on client-side,because of security issues.

Upvotes: 1

Views: 272

Answers (1)

jfriend00
jfriend00

Reputation: 707318

It's still not very clear what you're really trying to do. To stop getting incoming socket events, you have a number of options:

  1. Remove all your socket event handlers.
  2. Set a flag that your event handling code checks and if the flag is set, it skips its normal code execution.
  3. Disconnect all clients on that namespace and refuse new connections.

As always, if you tell us what problem you're really trying to solve rather than just asking about the solution you're trying to implement, we may be able to offer much better answers. See a description of the XY Problem for discussion about why it may be better to ask about your actual problem rather than asking about issues with your attempted solution.

Upvotes: 1

Related Questions