Removing someone from a user hash on navigating away from a page / page close
Problem
I'm building a Node.js / Socket.io project.
I have a Hash of Users based on their websocket id. When a user makes the following request i'd like to add them to a group of users viewing that page.
app.get('/board/:id', function(req, res){}
I'd like to keep something like
Browsing = { Username : id, username : id, ... }
However i'm unsure how to remove a user lets say if they navigate off the page. Is there some kind of function that gets called upon page leave?
Partial Solution:
The following seems to do the trick on Chrome: $(window).unload(function(){
var username = $('#username').text();
var pid = currentProject;
var data = {
username: username,
id : pid
}
socket.emit('leaving-page', data);
})
Solution
... Is there some kind of function that gets called upon page leave? ...
Yes, but it is not reliable.
The way the people keep track of who is online and who isn't, is usually like this:
- Add the time when the user last refreshed/visited a page
- set a limit to you consider them offline
Discussion
View additional discussion.