title: Get the list of connected users/client in Socket.io
body: <p>I need to get a list of connected users or clients from socket.io for real time chat.</p>
  
  <p><a href="https://i.stack.imgur.com/tAG4N.png" rel="noreferrer">List of connected user image</a></p>
  
  <p>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.</p>
  
  <p>This is my sample code for socket.io for server side,</p>
  
  <pre><code> module.exports.initializeSocketIO = (io) =&gt; {
   io.on('connection', (socket) =&gt; { 
      socket.on('connectedUser', (users) =&gt;{
          socket.name = users;
          io.emit('connectedUser', users);
          console.log(users + ' has joined the chat.');
      });
  
      socket.on('disconnect', (user) =&gt; {
          io.emit('disconnect', user);
          console.log(socket.name + ' has left the chat.');
      });
  
      socket.on('chatMessage', (from, msg) =&gt; {
          io.emit('chatMessage', from, msg);
          console.log('Message From: ' + from + '\n -' + msg);
      });
  
      socket.on('showTypingUser', (user) =&gt; {
          io.emit('showTypingUser', user);
      });
  });
  };
  </code></pre>
  
  <p>This is my sample code for socket.io for client side</p>
  
  <pre><code>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('&lt;tr&gt;&lt;td&gt;' + '&lt;b&gt;' + users + '&lt;/b&gt;' + ' has 
        joined the discussion. &lt;/td&gt;&lt;/tr&gt;' );
     }
  });
  
  
  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('&lt;div class="speech-bubble"&gt;'  + '&lt;b style="color:' + color + '"&gt;' + from + ':&lt;/b&gt; ' + msg + ' &lt;span class="pull-right" style="color:gray"&gt;' + date.toLocaleString() + '&lt;/span&gt;' +'&lt;/div&gt;');
    } else {
       $('#messages').append('&lt;div class="speech-bubble-reply"&gt;' +  '&lt;b 
   style="color:' + color + '"&gt;' + from + ':&lt;/b&gt; ' + msg + ' &lt;span class="pull-right" style="color:gray"&gt;' + date.toLocaleString() + '&lt;/span&gt;' +'&lt;/div&gt;');
    }
  });
  
  
  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('&lt;tr&gt;&lt;td&gt;' + '&lt;b&gt;' + socket.name  + '&lt;/b&gt;' + ' has left the discussion. &lt;/td&gt;&lt;/tr&gt;' );
    }
   });
  
  
  $(document).ready(function(){
     var name = $('#currentUser').val();
    $('#welcomeMessage').append('&lt;div class="welcome-chat"&gt;Welcome to Entrenami Chat&lt;/div&gt;')
    socket.emit('connectedUser', name);
  });
  </code></pre>
  
  <p><strong>NOTE</strong>: 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.</p>