title: Websockets using angular and Nodejs: How to send data to specific client
body: <p>I am trying to use <code>websocket.io</code> with <code>nodejs</code> and <code>angular</code>. I am trying to build a one-to-one chat application. Here is snippets of my code:</p>
  
  <p><strong>server.js</strong></p>
  
  <pre><code> app.io.on('connection', function(socket){
  
          console.info(`Client connected [id=${socket.id}]`);
          socket.on('disconnect', function(){
              console.info(`Client gone [id=${socket.id}]`);
          });
        });
  </code></pre>
  
  <p><strong>sendmessage.js</strong></p>
  
  <pre><code>router.post("/auth/sendMessage", function(req, res) {
      //rest api to handle send message
  
      //mongodb insert
      app.io.emit('new-message', { message: message});
  }
  </code></pre>
  
  <p><strong>Angular Client</strong></p>
  
  <pre><code>this.socket.on("new-message", function(data) {
              if (
                  data.message.conversationId === self.conversationId &amp;&amp;
                  data.message.sender === "customer"
              ) {
                  self.messages.push(data.message);
                  setTimeout(function() {
                      self.messagesDiv.nativeElement.scrollTop =
                          self.messagesDiv.nativeElement.scrollHeight;
                  }, 100);
              }
          });
  </code></pre>
  
  <p>Now, the problem that I am facing is <code>new-message</code> will send message to all the listeners. Although, I am handling the logic on the client side, to only show the message if it is sent by specific <code>conversationId</code>, this should be handled at the server end only due to security issues.</p>
  
  <p>So, I want to be able to send to specific <code>users</code> only. I think this can somehow be done using <code>socket.id</code> which would be unique for each connection. But how can I pass this <code>socket.id</code> from client to server, so that server knows on which client it needs to send the data?</p>