title: Understanding socket.io broadcasting of messages to specific users connected to a socket
body: <p>Using socket.io within a Node.js/Express setup, I'm trying to understand the functionality of when it comes to broadcasting messages to specific sockets/user-ids.</p>
  
  <p>For example: </p>
  
  <pre><code>io.on('connection', (socket) =&gt; {
    socket.join('user-1');
  });
  </code></pre>
  
  <p>then further down the code, I have the following code:</p>
  
  <pre><code>io.to('user-1').emit('user-1 has joined the room');
  </code></pre>
  
  <p>What I am trying to understand is the broadcasting of messages to specific users. </p>
  
  <p>Say I I had 5 different users accessing the website that has the above code and assuming that each of these users had their own unique user-ids, </p>
  
  <p>i.e. user-1, user-2, user-3 etc, which was passed in as part of the <code>socket.join('user-2')</code> for user-2, <code>socket.join('user-3')</code> for user-3 and so on.</p>
  
  <p>Now if I wanted to broadcast a message from a specific endpoint to a specific user, say user-3 ONLY, could I achieve this with just the following code:</p>
  
  <pre><code>io.to('user-3').emit('user-3 has joined the room');
  </code></pre>
  
  <p>or would I also need to associate each user connected with the socket#id?</p>
  
  <p>I'm hoping the io.to(....).emit(....) syntax can use the user-id alone but if not, what would be the best approach to accomplish the above as there may well be a lot of users connected.</p>