Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the list of connected users/client in Socket.io

I need to get a list of connected users or clients from socket.io for real time chat.

List of connected user image

I managed to display a list of client who connect to my route/API (localhost:3003/chat). Who ever access this route (authorize or not) will be displayed as shown in the picture. My problem is on initial access or if you try to refresh the browser the client will not see the currently connected users or history of client connections.

This is my sample code for socket.io for server side,

 module.exports.initializeSocketIO = (io) => {
 io.on('connection', (socket) => { 
    socket.on('connectedUser', (users) =>{
        socket.name = users;
        io.emit('connectedUser', users);
        console.log(users + ' has joined the chat.');
    });

    socket.on('disconnect', (user) => {
        io.emit('disconnect', user);
        console.log(socket.name + ' has left the chat.');
    });

    socket.on('chatMessage', (from, msg) => {
        io.emit('chatMessage', from, msg);
        console.log('Message From: ' + from + '\n -' + msg);
    });

    socket.on('showTypingUser', (user) => {
        io.emit('showTypingUser', user);
    });
});
};

This is my sample code for socket.io for client side

var socket = io(); 

socket.on('connectedUser', function(users){
   var name = $('#currentUser').val();
   var me = $('#user').val(name);
   socket.name = users;
   if(users != me){
      $('#connectedUser').append('<tr><td>' + '<b>' + users + '</b>' + ' has 
      joined the discussion. </td></tr>' );
   }
});


socket.on('chatMessage', function(from, msg){
   var me = $('#user').val();
   var color = (from == me) ? 'green' : '#009afd';
   var from = (from == me) ? 'Me' : from;
   var date = new Date();
   if(from == "Me"){
      $('#messages').append('<div class="speech-bubble">'  + '<b style="color:' + color + '">' + from + ':</b> ' + msg + ' <span class="pull-right" style="color:gray">' + date.toLocaleString() + '</span>' +'</div>');
  } else {
     $('#messages').append('<div class="speech-bubble-reply">' +  '<b 
 style="color:' + color + '">' + from + ':</b> ' + msg + ' <span class="pull-right" style="color:gray">' + date.toLocaleString() + '</span>' +'</div>');
  }
});


socket.on('showTypingUser', function(user){
   var name = $('#currentUser').val();
   var me = $('#user').val(name);
   if(user != me) {
      $('#notifyUser').text(user + ' is typing ...');
   } 
   setTimeout(function(){ $('#notifyUser').text(''); }, 10000);;
});


socket.on('disconnect', function(user){
   var name = $('#currentUser').val(); 
   var me = $('#user').val(name);
   if(socket.name != name){
      $('#connectedUser').append('<tr><td>' + '<b>' + socket.name  + '</b>' + ' has left the discussion. </td></tr>' );
  }
 });


$(document).ready(function(){
   var name = $('#currentUser').val();
  $('#welcomeMessage').append('<div class="welcome-chat">Welcome to Entrenami Chat</div>')
  socket.emit('connectedUser', name);
});

NOTE: I'm using express for my route and controller and EJS for my view. I'm a bit stuck here because I don't know where to look on how to solve my problem.

like image 536
Sherwin Ablaña Dapito Avatar asked Jun 26 '17 11:06

Sherwin Ablaña Dapito


People also ask

How do I know if Socket.IO client is connected?

You can check the socket. connected property: var socket = io. connect(); console.

How many users can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

How do I monitor Socket.IO traffic?

Use Monitor.io to observe connections and replay messages When you add it to your Node. js application, it provides a monitoring interface that you can access remotely by opening a Telnet connection to a particular port. It shows a list of active Socket.io client connections for your application.


1 Answers

io.engine.clientsCount return the total count of connected users you can send it to client when a user get connected like this:

io.on('connection', (socket) => {
   var total=io.engine.clientsCount;
   socket.emit('getCount',total)
});
like image 194
farhadamjady Avatar answered Oct 19 '22 00:10

farhadamjady