Monitoring connected clients using node.js

I am creating a site with a node.js server.

How will I track registered users (clients) and store their identifier on the node.js server?

+5
source share
3 answers

Try the following:

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 for the console an array of connected users every time someone connects / disconnects

+5
source

You can use plug-ins or NPM express modules (express is an extension of the connection). Connect (and therefore the expression) supports cookie-based sessions. Express is a very lightweight and popular structure.

, "" . , MongoDB, Riak SQL (postgreSQL mySQL), riak, ( , , , , - noSQL).

0

, middleware app.use(express.cookieParser()) app.use(express.session({secret: 'some secret'}). , . . express guide. ?

0

All Articles