Socket.IO: How do I remove a namespace
Asked Answered
L

3

10

I need to be able to construct and destruct socket.io namespaces on-the-fly. It is easily to find information how to create a namespace, but I find nothing about how I remove/disconnect the namespace to release its memory.

Say I got the following code already running:

var nsp = io.of('/my-namespace');
nsp.on('connection', function(socket){
  console.log('someone connected'):
});
nsp.emit('hi', 'everyone!');

How do I disconnect/remove the socket.io namespace created above?

Laterite answered 16/10, 2014 at 9:16 Comment(0)
P
8

The io.of method just creates an array element:

Server.prototype.of = function(name, fn){
  if (String(name)[0] !== '/') name = '/' + name;

  if (!this.nsps[name]) {
    debug('initializing namespace %s', name);
    var nsp = new Namespace(this, name);
    this.nsps[name] = nsp;
  }
  if (fn) this.nsps[name].on('connect', fn);
  return this.nsps[name];
};

So I assume you could just delete it from the array in socket io. I tested it pretty quick and it seems to work. Sockets that are already connected, keep connected.

delete io.nsps['/my-namespace'];

Connecting to /my-namespace then falls back to the default namespace. I don't know if this is a good solution, but maybe you can play with this a little..

Pine answered 16/10, 2014 at 10:1 Comment(1)
Check the other answer, this one does not work properly.Rwanda
E
40

As some have commented below, this is only applicable to Socket.IO version <3

Actually, by just deleting the namespace from the server nsps array you will not free any memory and sockets will still remain connected since there are still pointers to the Namespace in memory, so it will not be Garbage Collected... If what you want is to completely empty the resource you should

  1. disconnect all the sockets from the specific namespece
  2. delete all event listeners since it is an EventEmitter extented class
  3. remove it from the nsps array in the server

For example

const MyNamespace = io.of('/my-namespace'); // Get Namespace
const connectedNameSpaceSockets = Object.keys(MyNamespace.connected); // Get Object with Connected SocketIds as properties
connectedNameSpaceSockets.forEach(socketId => {
    MyNamespace.connected[socketId].disconnect(); // Disconnect Each socket
});
MyNamespace.removeAllListeners(); // Remove all Listeners for the event emitter
delete io.nsps['/my-namespace']; // Remove from the server namespaces
Exorcism answered 8/4, 2016 at 12:42 Comment(5)
It works only with one problem, connectedNameSpaceSockets[socketId].disconnect(); should be MyNamespace.connected[socketId].disconnect() instead.Guillermoguilloche
@YiKai you are right... That was the result of writing a fast reply... :D I am curious how nobody noticed all this time... I will update the reply... ThanksExorcism
io.nsps is undefined ,I am using v3 can you please helpRickeyricki
This answer seems to be no longer correct from v3 onwards.Farfetched
@Rickeyricki Unfortunately I haven't worked with v3. So probably Julian is right. I will also add a mention in the reply that is only for version <3Exorcism
P
8

The io.of method just creates an array element:

Server.prototype.of = function(name, fn){
  if (String(name)[0] !== '/') name = '/' + name;

  if (!this.nsps[name]) {
    debug('initializing namespace %s', name);
    var nsp = new Namespace(this, name);
    this.nsps[name] = nsp;
  }
  if (fn) this.nsps[name].on('connect', fn);
  return this.nsps[name];
};

So I assume you could just delete it from the array in socket io. I tested it pretty quick and it seems to work. Sockets that are already connected, keep connected.

delete io.nsps['/my-namespace'];

Connecting to /my-namespace then falls back to the default namespace. I don't know if this is a good solution, but maybe you can play with this a little..

Pine answered 16/10, 2014 at 10:1 Comment(1)
Check the other answer, this one does not work properly.Rwanda
R
3

With Socket.IO v3 and above, you can use this:

// disconnect all sockets from this namespace
io.of('/the-namespace').local.disconnectSockets();

// remove the namespace
io._nsps.delete('/my-namespace');
Repel answered 4/4, 2022 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.