I am building a website with a node.js server.
How would i track logged-in users (clients) and store their id's on the node.js server?
Try this:
var http=require('http');
var connected_users={};
var server=http.createServer(function(req,res){
    res.end('hi');
});
server.on('connection',function(socket){
    socket.__fd=socket.fd;
    connected_users[socket.__fd]=socket.remoteAddress;
    console.log(connected_users);
    socket.on('close',function(){
        delete connected_users[socket.__fd];
        console.log(connected_users);
    }); 
});
server.listen(8080);
It prints out to console the array of connected users every time someone connects/disconnects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With