3

Ideally, I'd like to find simple, lightweight code that allows all the web clients connected to my site to maintain real-time status of who else is currently online.

I know ejabberd does this, but it also does a lot of other things, and I'd prefer a small code footprint so I can customize and understand its performance characteristics.

I like the non-blocking aspect of node.js, and was wondering if there's an open source project that does all this logic.

I'd also like to see a JavaScript implementation of maintaining this model on the client side.

jseims
  • 31
  • 1
  • 2

2 Answers2

5

For real time status, use socket.io. Every time someone connects add them to the connected users list. For accurate stats you will need to keep track of user sessions. See http://www.danielbaulig.de/socket-ioexpress/

var onlineUsers = {};
var online = 0;

io.sockets.on('connection', function (socket) {
  onlineUsers[socket.handshake.sessionID] = socket.handshake.session;
  online = Object.keys(onlineUsers).length;
  socket.broadcast.emit('online', online);

  socket.on('disconnect', function () {
    delete onlineUsers[socket.handshake.sessionID];
    online--;
    socket.broadcast.emit('online', online);
  });
});
fent
  • 17,861
  • 15
  • 87
  • 91
  • Note this does not scale horizontally with multiple server processes. In this example the `onlineUsers` object is a property of the server's global object, and multiple servers will not share that data. Implementing real-time presence across a multi-node environment is much more difficult. – Yetanotherjosh May 03 '18 at 22:21
1

For anyone reading this in the future. I started with the answer written by fent, but I needed some modifications since things have changed in the intervening 6 years since his answer was published.

I used a Map rather than an Object for storing the session information. this means that the sessionID is no longer required.

const onlineUsers = new Map();

io.on('connection', function (socket) {
    console.log('connection initiated');

    socket.on('userInfo', userInfo => {
        console.log('userInfo', userInfo);

        onlineUsers.set(socket, userInfo);

        io.emit('onlineUsers', Array.from(onlineUsers.values()));
    });

    socket.on('disconnect', function () {
        console.log(onlineUsers.get(socket));

        onlineUsers.delete(socket);

        io.emit('onlineUsers', Array.from(onlineUsers.values()));
    });
});
Potter
  • 633
  • 7
  • 13