Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  68] [ 7]  / answers: 1 / hits: 28097  / 12 Years ago, thu, march 29, 2012, 12:00:00

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!


More From » node.js

 Answers
62

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];
});
});

[#86547] Wednesday, March 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carlymykalac

Total Points: 740
Total Questions: 91
Total Answers: 91

Location: Sudan
Member since Thu, May 7, 2020
4 Years ago
;