I'm working on an application where I'd like to show the user the number of users currently online on the site.
For users that are signed in, I have an array of logged in users with their user id. And I'm taking the count to show the number of logged in users. But here is a problem here, I push the user id of a user into the array when the user logs in, and remove it when the user logs off. But if the user doesn't logout and closes the tab, it stays there. But of course, I could do something like, have a table of users signed in, have a last seen column in it and remove the entries if the user was inactive for the last 5 minutes or so. But I wanted to know if there was a better logic to do this.
I tried:
io.on( "connection", function( socket )
{
console.log( "A user connected" );
console.log(io.engine.clientsCount);
console.log(Object.keys(io.sockets.connected).length);
});
But if one user has 10 tabs of my app open, io.engine.clientsCount
and Object.keys(io.sockets.connected).length
return 10. So what can I do to get the unique number of users online, regardless of how many tabs they have open.
EDIT: I'm using MySQL.