My Real Time Chat App is based on Sails and SocketIO.It shows all users along with available online users .When a User logged in,a UserConnected socket is emitted and When a User is disconnected,a UserDisconnected socket is emitted , My Server side socketio code in config/sockets.js is
onConnect: function(session, socket) {
if (session.passport) {
if (session.passport.user && socket.id) {
sails.io.sockets.emit('UserConnected', {userID : session.passport.user}); }
}
}
//// By default: do nothing
//// This is a good place to broadcast a disconnect message, or any other custom socket.io logic
},
onDisconnect: function(session, socket) {
if (session.passport) {
if (session.passport.user && socket.id) {
sails.io.sockets.emit('UserDisconnected', {userID : session.passport.user});
}
}
//// By default: do nothing
//// This is a good place to broadcast a disconnect message, or any other custom socket.io logic
}
The Problem is that when a User refreshes the page ,it shows connected and disconnected to another User and one major problem is that if one User logged in two different browsers and if any one browser is closed is shows User disconnected to other Users but the User is actually available in another browser.
How would I modify/code So that these problems will be fixed?
Thanks in Advance
Here is the method I am using for a similar purpose.
It keeps track of how many browsers are logged in as a specific user. So you could use the following.
@InternalFX posted a good method; an alternative that you can use on Sails v0.10.x would be:
This uses Sails' built-in pubsub architecture to notify connected sockets whenever a particular user comes online or goes offline. It requires sockets to subscribe to the user instances they want to know about (using something like
socket.get('/user')
on the client), which is a good practice to get into. This way you can be notified only about the users you care about (e.g. if you had a "friends list").