Skip to content

Instantly share code, notes, and snippets.

@svasva
Created February 14, 2013 10:38
Show Gist options
  • Save svasva/4951913 to your computer and use it in GitHub Desktop.
Save svasva/4951913 to your computer and use it in GitHub Desktop.
Meteor - users online tracker
common =
keepalive: ->
(update = -> Meteor.call('keepalive') if Meteor.userId())()
Meteor.clearInterval(Meteor.keepalive) if Meteor.keepalive?
Meteor.keepalive = Meteor.setInterval(update, config.keepalive * 1000)
Meteor.methods
keepalive: (params) ->
return false unless @userId
Meteor.keepalive ?= {}
Meteor.clearTimeout Meteor.keepalive[@userId] if Meteor.keepalive[@userId]
(setOnline = (online) ->
if (user = Meteor.user()) and not user.profile.online is online
Meteor.users.update user._id, $set: {'profile.online': true}
)(true)
Meteor.keepalive[@userId] = Meteor.setTimeout (=>
delete Meteor.keepalive[@userId]
setOnline(false)
), (config.keepalive + 10) * 1000
return true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment