Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove objects on disconnect socket.io

I'm using Nodejs and Socket.io. When the client connects, new JavaScript objects are created.

Do these objects just linger forever? Should they be deleted or removed when the client disconnects? Is it even possible to remove an object? I know delete won't work...

Thanks - I guess this is more of a general question and any suggestions would be really helpful.

Thanks!

like image 690
dzm Avatar asked Mar 29 '12 02:03

dzm


People also ask

How do I remove a socket.io connection?

click(function(){ socket. connect(); }); $('#disconnect_button'). click(function(){ socket. disconnect(); });

How do I remove socket.io from client side?

var socket = io. connect('http://localhost:3000/test'); socket. on('disconnect', function () { console. log('disconnect client event....'); }); socket.

How many rooms can socket.io handle?

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.


1 Answers

If you don't cleanup, then yes, they will stay there forever since I assume you are making them global.

You should cleanup once a user disconnects by binding to the disconnect event listener:

var clients = {}
sockets.on('connection', function(socket) {
  clients[socket.id] = socket;

  socket.on('disconnect', function() {
    delete clients[socket.id];
  });
});
like image 141
Mohamed Mansour Avatar answered Oct 10 '22 15:10

Mohamed Mansour