1

I have multiple heroku dynos and a chat app. When a user logs in, their status is set to "online" in MongoDB. However, if a server crashes, their status will still be set as online. How can I update the user status to be "offline" when a server crashes?

If I only had one dyno, this would be easy. I'd just update every user to be "offline" when the server starts. Unfortunately, this is not possible with multiple servers.

David L. Rodgers
  • 1,031
  • 1
  • 8
  • 18
  • are you tracking their activity on your site? – gmaniac Jan 09 '15 at 02:30
  • @gmaniac What do you mean? When they log on, I set the status to "online" and when the socket "disconnect" event is called, I set their status to "offline" – David L. Rodgers Jan 09 '15 at 02:32
  • so for instance if you are storing their messages then you can have a cron running or something running in the background that checks last activity and if it is not in the `x` time their status is changed to inactive or offline. – gmaniac Jan 09 '15 at 02:37
  • IF you are storing messages you don't have to add any extra parameters to your mongo doc, the `_id` has the timestamp – gmaniac Jan 09 '15 at 02:38
  • @gmaniac I guess that would work. Though an unfortunate side effect would be that the status is never 100% accurate, there would be a delay. – David L. Rodgers Jan 09 '15 at 02:43
  • I figured there would be a delay any way you looked it, like you said there are multiple servers so you won't be able to say everyone is offline and if only one server crashes when it comes back up you don't want to log everyone out. – gmaniac Jan 09 '15 at 02:49
  • Also I was assuming that each dyno was a different chat app but sharing the same db, is this wrong? – gmaniac Jan 09 '15 at 02:52
  • @gmaniac One app. Multiple servers to handle load. – David L. Rodgers Jan 09 '15 at 02:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68454/discussion-between-gmaniac-and-david-l-rodgers). – gmaniac Jan 09 '15 at 02:59

1 Answers1

2

As per our chat and comments.

The best option is to go with checking against last activity. So seeing when the last message was sent and if it happened within the last let's say 5 minutes they are online if there were no activity mark them as offline.

Like I mentioned in the comments, if you are not storing a date_created on the messages documents you will not have to change anything because _id stores the timestamp

ObjectId("507f191e810c19729de860ea").getTimestamp()

that returns this Date object

ISODate("2012-10-17T20:46:22Z")

This answer is another option (if you are wanting to keep them as online even if they are not sending messages):

If you would like to know they are still active even when they're not jumping from page to page, include a bit of javascript to ping your server every 60 seconds or so to let you know they are still alive. It'll work the same way as my original suggestion, but it will update your records without requiring them to be frantically browsing your site at least once every five minutes.

var stillAlive = setInterval(function () {
    /* XHR back to server
       Example uses jQuery */
    $.get("stillAlive.php");
}, 60000);
Community
  • 1
  • 1
gmaniac
  • 940
  • 1
  • 17
  • 33