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?
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);
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*/});
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!
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