How to create online users list using webrtc and nodejs on the server end

I am using webrtc to make a audio, video and chat application where I need keep all the users in a user list in the serverside. Need help how to get this done.

Also, how can I remove users from the list when they logout from the system.

Need help to implement this.

 webRTC.rtc.on('connect', function(rtc) {
 //Client connected
 });

 webRTC.rtc.on('send answer', function(rtc) {
 //answer sent
 });

 webRTC.rtc.on('disconnect', function(rtc) {
 //Client disconnect
     //console.log(webRTC);
 });

 webRTC.rtc.on('chat_msg', function(data, socket) {
     var roomList = webRTC.rtc.rooms[data.room] || [];

     for (var i = 0; i < roomList.length; i++) {
         var socketId = roomList[i];

         if (socketId !== socket.id) {
             var soc = webRTC.rtc.getSocket(socketId);

             if (soc) {
                 soc.send(JSON.stringify({
                     "eventName": "receive_chat_msg",
                     "data": {
                     "messages": data.messages,
                     "id": data.id,
                     "from": data.from,
                     "status": data.status,
                     "email": data.email
                     }
                 }), function(error) {
                     if (error) {
                     console.log(error);
                 }
                 });
             }
         }
     }
 });

As I was using webrtc.io module, so below are the methods that helped me to create the userlist and maintain the presence.

webRTC.rtc.on('join_room', function(data, socket) {
// Will get info who joined along with his socket id
}

And

webRTC.rtc.on('room_leave', function(room, socketid) {
// Will get info who left the room
}

Node.js code:

var users = {};
io.sockets.on('connection', function (socket) {
    socket.emit('connect', true);

    socket.on('message', function (data) {
        socket.broadcast.emit('message', data);
    });

    socket.on('new-user', function (username) {
        users[username] = username;
    });

    socket.on('check-presence', function (username) {
        var isUserPresent = !! users[username];
        socket.emit('presence', isUserPresent);
    });

    socket.on('remove-user', function (username) {
        var user = users[username];
        if (user) delete users[username];
    });
});

This may also work (node.js):

var users = {};
io.sockets.on('connection', function (socket) {
    var UserName;

    socket.emit('connect', true);
    socket.on('message', function (data) {
        socket.broadcast.emit('message', data);
    });

    socket.on('new-user', function (username) {
        users[username] = username;
        UserName = username;
    });

    socket.on('check-presence', function (username) {
        var isUserPresent = !! users[username];
        socket.emit('presence', isUserPresent);
    });

    // removing user on "disconnect"
    socket.on('disconnect', function () {
        var user = users[UserName];
        if (user) delete users[UserName];
    });
});

For 1st case; client-side code:

var socket = io.connect();

socket.on('connect', function () {
    socket.emit('new-user', 'username');
});

function removeUser() {
    socket.emit('remove-user', 'username');
}

window.onbeforeunload = function () {
    removeUser();
};

// if someone pressed "F5" key to refresh the page
window.onkeyup = function (e) {
    if (e.keyCode == 116)
        removeUser();
};

// if someone leaves via <a href>
var anchors = document.querySelectorAll('a'),
    length = anchors.length;
for (var i = 0; i < length; i++) {
    var a = anchors[i];
    if (a.href.indexOf('#') !== 0 && a.getAttribute('target') != '_blank')
        a.onclick = function () {
            removeUser();
        };
}

For 2nd case; client side code:

var socket = io.connect();

socket.on('connect', function () {
    socket.emit('new-user', 'username');
});

You can check presence too:

socket.on('presence', isUserPresent) {
    // boolean: user is present or not
});

socket.emit('check-presence', 'username');