Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

monitor connected clients using node.js

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?

like image 733
chris Avatar asked Aug 10 '11 07:08

chris


1 Answers

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

like image 52
stewe Avatar answered Sep 28 '22 08:09

stewe