Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server cleanup after a client disconnects

Is there a way to detect when a client disconnects from a meteor server, either by refreshing or navigating away from the page, so that the server can attempt some cleanup?

like image 725
greggreg Avatar asked Apr 21 '12 10:04

greggreg


3 Answers

One technique is to implement a "keepalive" method that each client regularly calls. This assumes you've got a user_id held in each client's Session.

// server code: heartbeat method
Meteor.methods({
  keepalive: function (user_id) {
    if (!Connections.findOne(user_id))
      Connections.insert({user_id: user_id});

    Connections.update(user_id, {$set: {last_seen: (new Date()).getTime()}});
  }
});

// server code: clean up dead clients after 60 seconds
Meteor.setInterval(function () {
  var now = (new Date()).getTime();
  Connections.find({last_seen: {$lt: (now - 60 * 1000)}}).forEach(function (user) {
    // do something here for each idle user
  });
});

// client code: ping heartbeat every 5 seconds
Meteor.setInterval(function () {
  Meteor.call('keepalive', Session.get('user_id'));
}, 5000);
like image 167
debergalis Avatar answered Nov 17 '22 00:11

debergalis


I think better way is to catch socket close event in publish function.

Meteor.publish("your_collection", function() {
    this.session.socket.on("close", function() { /*do your thing*/});
}

UPDATE:

Newer version of meteor uses _session like this:

this._session.socket.on("close", function() { /*do your thing*/});
like image 38
poordeveloper Avatar answered Nov 17 '22 00:11

poordeveloper


I've implemented a Meteor smart package that tracks all connected sessions from different sessions and detects both session logout and disconnect events, without an expensive keepalive.

https://github.com/mizzao/meteor-user-status

To detect disconnect/logout events, you can just do the following:

UserStatus.on "connectionLogout", (info) ->
  console.log(info.userId + " with session " + info.connectionId + " logged out")

You can also use it reactively. Check it out!

EDIT: v0.3.0 of user-status now tracks users being idle as well!

like image 2
Andrew Mao Avatar answered Nov 16 '22 22:11

Andrew Mao