Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Remove objects on disconnect

Remove objects on disconnect


I'm using Nodejs and 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.


Problem courtesy of: dzm


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;

  socket.on('disconnect', function() {
    delete clients[];
Solution courtesy of: Mohamed Mansour


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Remove objects on disconnect


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription