javascript - socket.io best way to track if a user is online -


i need update client side stuff if user has socket connection.

currently broadcasting when user connects "logged in"


client side (could server side , same result)

// announce user online socket.on('connect', function() {   socket.emit('im online'); }); 

server side

io.sockets.on('connection', function (socket) {      socket.on("im online", function (data) {         // announce online status of new user         socket.broadcast.emit('connected user',  {"name": socket.handshake.headers.user.username});     });  }); 

the problem is, load page let know "online" because loading of page triggers event, if idol , user reloads his/her page nothing triggered on end, there no indication online.

so thinking server side can capture array of users made connection , send array down new clients connecting , use compare users friend list see if username matches (usernames 100% unique in each case btw)

then when user disconnects can remove array , let client know.

this solution, seems highly ineffective , hoping there better solution.

you broadcast disconnect events well:

socket.on("disconnect", function (data) {     // announce offline status of new user     socket.broadcast.emit('disconnected user',  {"name": socket.handshake.headers.user.username}); }); 

th disconnect event automatically generated on server side when client disconnects.

on other hand have store on server side connected users, because otherwise newly connected users wouldn't notified connected others.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -