dome12b
dome12b

Reputation: 633

Catch user disconnect on server side

Is there a possibility to catch an inactive user-session on server side? I build a solution with two intervalls, but that makes my page updating every 20 seconds, because the client is pushing his online state every 20 seconds. I know the Meteor.logout([callback]) function, but I have to detect weather a client is closing the browser window without logging out, too. I found the meteor client user sessions demo, but this has not the functions I need. Is there any solution yet?

Upvotes: 2

Views: 1284

Answers (2)

Andrew Mao
Andrew Mao

Reputation: 36940

I've figured out a way to catch disconnects using the websocket. This doesn't use polling to keep the state as online, either. Basically, you make a arbitrary publication and subscribe to it on the client side:

Meteor.publish "statusWatcher", ->
  id = @_session.userId
  @_session.socket.on "close", Meteor.bindEnvironment( ->
    Meteor.users.update id,
      $set: {'profile.online': false}

As always, this is subject to Meteor API changes, but you can install this as a smart package, and the community can keep it up to date:

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

Upvotes: 2

Rahul
Rahul

Reputation: 12231

Check out the meteor-profile-online package (for which you'll need to install meteorite):

Profile.online is a meteor smart package to provide a convenient way to expose accounts online status.

It adds the profile.online property to the user object, which is toggled by a keepalive that you can configure.

It uses the same approaches you mentioned in your question (polling), but that's really the only way to do it. At least this package integrates it nicely into Meteor with the reactive Meteor.keepalive.

Upvotes: 2

Related Questions