Is there a way to get a list of all currently connected users? I have checked many of the chatroom tutorials and none of them provide this methodology. Is it even possible and if so how would one go about implementing it correctly the Meteor way?
I've managed to figure out a way to do this without using inefficient keep-alives. Basically, user logins or reconnects set the profile.online
to true, and logouts or disconnects set it to false. You can add it as a Meteorite smart package and try it out here:
https://github.com/mizzao/meteor-user-status
Fixes or improvements are appreciated.
We accomplished this by setting an online property when a user logs in, and then periodically pinging (every 10 seconds) and setting any inactive users to offline. Not ideal, but it works. Would love to see this feature in Meteor. Here is the pinging function.
Meteor.setInterval(function () {
var now = (new Date()).getTime();
OnlineUsers.find({'active':true,'lastActivity': {$lt: (now - 30 * 1000)}}).forEach(function (onlineActivity) {
if(onlineActivity && onlineActivity.userId) {
OnlineUsers.update({userId:onlineActivity.userId},{$set: {'active': false}});
Meteor.users.update(onlineActivity.userId,{$set: {'profile.online': false}});
}
});
}, 10000);
Old question, but for anyone looking into this there is now a meteor package that monitors client to server connections. It's called meteor user status and can be found on github.
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