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!
click(function(){ socket. connect(); }); $('#disconnect_button'). click(function(){ socket. disconnect(); });
var socket = io. connect('http://localhost:3000/test'); socket. on('disconnect', function () { console. log('disconnect client event....'); }); socket.
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.
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];
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With