Skip to content

Commit

Permalink
improve tracking of multiple connections
Browse files Browse the repository at this point in the history
  • Loading branch information
mizzao committed Jun 23, 2013
1 parent e5f953f commit 8ef35ba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion smart.json
Expand Up @@ -3,7 +3,7 @@
"description": "User connection and status tracking for meteor",
"homepage": "https://github.com/mizzao/meteor-user-status",
"author": "Andrew Mao (http://www.andrewmao.net)",
"version": "0.1.1",
"version": "0.1.2",
"git": "https://github.com/mizzao/meteor-user-status.git",
"packages": {}
}
16 changes: 2 additions & 14 deletions user_status_client.coffee
@@ -1,15 +1,3 @@

# Hook into original logout function
Meteor.__logout__ = Meteor.logout
Meteor.logout = (callback) ->
Meteor.users.update Meteor.userId(),
$set: {'profile.online': false}
Meteor.__logout__(callback)

# Add online status when logged in or resuming
Deps.autorun ->
return unless Meteor.userId()
Meteor.users.update Meteor.userId(),
$set: {'profile.online': true}

Meteor.subscribe "statusWatcher"
Meteor.userId() # Re-subscribe whenever logging in our out
Meteor.subscribe "statusWatcher"
38 changes: 26 additions & 12 deletions user_status_server.coffee
@@ -1,30 +1,44 @@
this.UserSockets = new Meteor.Collection(null)

# Publication is guaranteed to be called once per connection
# trick as referenced in http://stackoverflow.com/q/10257958/586086
# pub/sub trick as referenced in http://stackoverflow.com/q/10257958/586086
Meteor.publish "statusWatcher", ->
id = @_session.userId
return unless id?
return unless @_session.socket?
sockId = @_session.socket.id

# Add socket to open connections
Meteor.bindEnvironment ->
UserSockets.insert
userId: id
# Untrack connection on logout
unless id?
# TODO: this could be replaced with a findAndModify once it's supported on Collections
existing = UserSockets.findOne
sockId: sockId
, (e) ->
Meteor._debug "Exception from connection add callback:", e
return unless existing? # Probably new session

id = existing.userId
UserSockets.remove
sockId: sockId

if UserSockets.find(userId: id).count() is 0
Meteor.users.update id,
$set: {'profile.online': false}
return

# Add socket to open connections
UserSockets.insert
userId: id
sockId: sockId
Meteor.users.update id,
$set: {'profile.online': true}

# Remove socket on close
@_session.socket.on "close", Meteor.bindEnvironment( ->
@_session.socket.on "close", Meteor.bindEnvironment ->

This comment has been minimized.

Copy link
@mizzao

mizzao Jun 10, 2014

Author Collaborator

This needs to be replaced with the new http://docs.meteor.com/#meteor_onconnection in order to take advantage of the DDP heartbeat timeout.

UserSockets.remove
userId: id
sockId: sockId

if UserSockets.find(userId: id).count() is 0
Meteor.users.update id,
$set: {'profile.online': false}

, (e) ->
Meteor._debug "Exception from connection close callback:", e
)

return

0 comments on commit 8ef35ba

Please sign in to comment.