title: Send message to a specific client socket (from NodeJS server to Java client)
body: <p>I have a Java client and a NodeJS server that communicate via sockets. I want to have more clients connected at once using a Windows &quot;cmd&quot; for each one. My program should simulate some login/logout scenarios. What I want to achieve is:</p>
  <ul>
  <li>ClientA and ClientB both connect to Server</li>
  <li>ClientA logs in (using a command like &quot;login username password&quot;)</li>
  <li>ClientB also logs in, but using the same username and password as ClientA</li>
  <li>ClientA should be logged out by force, while ClientB should log in successfully</li>
  </ul>
  <p>I use a Java thread for each of the clients. My problem is that I don't understand how my NodeJS can send a message for each one of them to announce that they were logged in/logged out. The only one that receives the message is the last one who requested something from the server (i.e. the one who wants to log in).</p>
  <p><code>connection</code> is the current client-server connection. For every connection that has a successful login, I store the username in <code>currentUsername</code> key. I store all these successful logins in <code>connectionArray</code>. When a new client wants to log in, I go through all the connections to see if somebody is already logged in with that username:</p>
  <p><strong>Node server</strong></p>
  <pre><code>var net = require('net')
  
  var connectionArray = []
  
  var server = net.createServer(function (connection) {
  
      connection.on('data', function (data) {
  
          let commandArray = data.toString().split(&quot; &quot;)
  
          if (commandArray[0] == &quot;login&quot;) {
  
              for (let connectionElement of connectionArray) {
                  if (connectionElement.currentUsername == requestedUsername) {
                      connectionElement.write(`We will log you out!\n`)
                      connection.write(`We will log you in!\n`)
                  }
              }
  
              /* This Promise result is responsible for storing the connections
                 I didn't yet treat the removing case of a logout */
              loginService.check(someParameters).then((response) =&gt; {
                  connectionArray.push(connection)
              }, (rejected) =&gt; {
                  connection.write(rejected.message)
              })
          }
      })
  })
  </code></pre>
  <p><strong>Main.java</strong></p>
  <pre><code>public class Main {
  
      public static void main(String[] args) throws IOException {
          ClientThread clientThread = new ClientThread();
          clientThread.start();
      }
  }
  </code></pre>
  <p><strong>Login case inside ClientThread.java</strong></p>
  <pre><code>    case &quot;login&quot;:
      try {
          if (splitCommand[1] != null &amp;&amp; splitCommand[2] != null) {
              out.write(commandLine);
              out.flush();
  
              String line = reader.readLine();
              System.out.println(line + &quot;\n&quot;);
          }
      } catch (Exception e) {
          System.out.println(&quot;Please enter your username and password!\n&quot;);
      }
      break;
  </code></pre>
  <p>The result will be that clientB will receive &quot;We will log you in!&quot;, but clientA will not receive its message. What's the correct way to do this? Thanks a lot!</p>