5

Is there any way to get a list of all the namespaces registered on the socket?

For example, suppose some client connects to a namespace: `socket = io('/some-nsp');

They are now in a namespace automagically because the socket.io does not prevent creating random namespaces.

If I want to go through the list of all existing namespaces and disconnect those users, how could I get such a list.

I've tried io.nsps, but this just is a circular list of junk.

Is there an "official" way to get a list of all the namespaces registered on the socket? I don't want a list of clients. I just want a list of namespaces.

Justin Noel
  • 5,945
  • 10
  • 44
  • 59
  • http://stackoverflow.com/questions/6631501/how-to-list-rooms-on-socket-io-nodejs-server - is it applicable? I've been using Socket.io some time ago but AFAIR, rooms = namespaces. – eRIZ Aug 01 '14 at 13:45
  • 2
    Rooms and names spaces are very different creatures. See http://stackoverflow.com/questions/10930286/socket-io-rooms-or-namespacing – Justin Noel Aug 01 '14 at 13:50
  • 1
    What about `Object.keys(io.nsps)` ? – Oleg Aug 01 '14 at 18:35
  • @CuriousGuy : Excellent. That's what I needed. Why don't you answer the question so I can vote it. – Justin Noel Aug 01 '14 at 18:44

2 Answers2

10

You may try this:

Object.keys(io.nsps);
Oleg
  • 22,300
  • 9
  • 68
  • 84
  • 2
    This worked for me. However, I found out namespaces are not global, so if you initialize another io object, the nsps property will not have your namespaces. Just something I ran into, FYI – neveraskedforthis Apr 20 '18 at 22:38
4

In v3 nsps is a map not an object. Try this:

io._nsps.keys()