Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteorjs - Online users

Tags:

meteor

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?

like image 756
Pastor Bones Avatar asked Nov 05 '12 20:11

Pastor Bones


3 Answers

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.

like image 158
Andrew Mao Avatar answered Oct 17 '22 23:10

Andrew Mao


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);
like image 3
andreimpop Avatar answered Oct 17 '22 22:10

andreimpop


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.

like image 1
TheNastyOne Avatar answered Oct 18 '22 00:10

TheNastyOne