Simple question, maybe simple answer: how do I know on the server that a certain client has disconnected? Basic use case: the serve would need to know if a player has dropped the connection.
Asked
Active
Viewed 2,814 times
5
-
1http://stackoverflow.com/questions/10257958/server-cleanup-after-a-client-disconnects – Vic Goldfeld Sep 11 '12 at 13:30
-
just added an alternative way of tracking user connection status on this thread http://stackoverflow.com/questions/10257958/server-cleanup-after-a-client-disconnects – Lloyd Sep 12 '12 at 09:19
5 Answers
4
In the publish function, you can watch socket close event as follows.
this.session.socket.on "close", -> # do your thing

poordeveloper
- 2,272
- 1
- 23
- 36
4
Meteor.publish("yourPublishFunction", function()
{
var id = this._session.userId;
this._session.socket.on("close", Meteor.bindEnvironment(function()
{
console.log(id); // called once the user disconnects
}, function(e){console.log(e)}));
return YourCollection.find({});
});

Flavien Volken
- 19,196
- 12
- 100
- 133
-
-
… probably useless in most of the cases, I can imagine I was using it in my original code and just forgot to remove it for the SO post. – Flavien Volken Jan 13 '14 at 18:34
3
I've created a pretty comprehensive package to keep track of all logged-in sessions from every user, as well as their IP addresses and activity:
To watch for disconnects, you can just do the following, which catches both logouts and browser closes:
UserStatus.on "sessionLogout", (userId, sessionId) ->
console.log(userId + " with session " + sessionId + " logged out")
You can also check out the code and do something similar for yourself.

Andrew Mao
- 35,740
- 23
- 143
- 224
1
Maybe (in the server code)
Meteor.default_server.sessions.length
or
Meteor.default_server.stream_server.open_sockets.length

snez
- 2,400
- 23
- 20
0
you can do one thing make a event on server and call it from browser with ajax which call after some small time interval settimeinterval using with session values into header and if server din`t get request from user it means he dropped connection

Gyan Chandra Srivastava
- 1,370
- 9
- 29