title: socket.io v.4 How to get list of sockets already in room when joining
body: <p>I am using socket.io (v4.0.1) to build a chat app with react and node. I would like to get a list of sockets (users) who have already joined a room so that when a users joins a room I can send a notification to them to let them know if there are other people online already. There are <a href="https://stackoverflow.com/questions/18093638/socket-io-rooms-get-list-of-clients-in-specific-room">a lot of examples</a> how to do this with earlier versions of socket.io but none seem to work with version 4. Here is my server code for this event:</p>
  <pre class="lang-js prettyprint-override"><code>  // join a room (chat) with the chat id
    socket.on('join chat', ({ chat, userId }) =&gt; {
      socket.join(chat.id);
  
      // this line is not working
      const clients = Object.keys(io.sockets.adapter.rooms[chat.id].sockets);
      // does not work
      // var roster = io.sockets.clients('chatroom1');
  
  
      const { users } = chat;
  
      if (!chat.users) return console.log('users not defined');
  
      // for each user in the conversation, send a notification to their own room
      // telling them someone joined the chat
      // want to let the user who joined (userId) know who is already there
      chat.users.forEach(user =&gt; {
        if (user._id === userId) return;
        socket.in(user._id).emit('user joined', userId);
      });
  
      console.log(`User with id ${userId} joined chat with id ${chat.id}`);
    });
  </code></pre>