Sorry first for the misunderstooding title, but couldn't make it better. So here is my problem: I'm using NodeJS and socket.IO. I got a lobby system for something like a game. You create root, the other people joins etc. I want when the owner of the room close the connection the room should delete itself. I did it like this
db.game.findOne({ _id: ObjectId(roomId) }, function(err, item)
{
if ( err || !item || item.started )
return;
if ( item.createdBy == currentUser )
{
db.game.remove({ _id: ObjectId(roomId) }, function(err, item) {
io.sockets.in(roomId).emit('force_interupt');
});
}
and it's working, but I have issue. If the person just press REFRESH on the browser, the webserver ( php, nginx, mongod ) is getting the contents of the game first -> showing it and after this the game got deleted by NodeJS, because the connection is lost. Any ideas how I can avoid this?
What's your expectation about the life cycle of the room?
If you want to close the room when the socket.io connection close, you already got what you want.
If you want to close the room when the http session expire, you should hook the room up to the session, which means you need to save room info in the http session data, and remove the room when the session expired.
If you want to close the room when the socket.io connection close, unless the connection close is caused by page reload or temporary network issue, you can do this: