Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO: How do I remove a namespace

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?

like image 922
Fredrik Avatar asked Oct 16 '14 09:10

Fredrik


People also ask

How many rooms can Socket.IO have?

socket.io rooms are a lightweight data structure. They are simply an array of connections that are associated with that room. You can have as many as you want (within normal memory usage limits). There is no heavyweight thing that makes a room expensive in terms of resources.

How do I disable a Socket.IO server?

close([callback])​ Closes the socket.io server. The callback argument is optional and will be called when all connections are closed.

Does Socket.IO need a database?

You don't need to store this data in database. It can be stored in memory as arrays of objects or map of objects.


2 Answers

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
like image 128
mitsos1os Avatar answered Sep 23 '22 07:09

mitsos1os


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..

like image 24
DerM Avatar answered Sep 23 '22 07:09

DerM