title: Sending message to a specific connected users using webSocket?
body: <p>I wrote a code for broadcasting a message to <em>all</em> users:</p>
  <pre><code>// websocket and http servers
  var webSocketServer = require('websocket').server;
  
  ...
  ...
  var clients = [ ];
  
  var server = http.createServer(function(request, response) {
      // Not important for us. We're writing WebSocket server, not HTTP server
  });
  server.listen(webSocketsServerPort, function() {
    ...
  });
  
  var wsServer = new webSocketServer({
      // WebSocket server is tied to a HTTP server. 
      httpServer: server
  });
  
  // This callback function is called every time someone
  // tries to connect to the WebSocket server
  wsServer.on('request', function(request) {
  ...
  var connection = request.accept(null, request.origin); 
  var index = clients.push(connection) - 1;
  ...
  </code></pre>
  <p>Please notice:</p>
  <ul>
  <li>I don't have any user reference but only a connection .</li>
  <li>All users connection are stored in an <code>array</code>.</li>
  </ul>
  <p><strong>Goal</strong>: Let's say that the Node.js server wants to send a message to a specific client (John). How would the NodeJs server know which connection John has? The Node.js server doesn't even know John. all it sees is the connections.</p>
  <p>So, I believe that now, I shouldn't store users only by their connection, instead, I need to store an object, which will contain the <code>userId</code> and the <code>connection</code> object.</p>
  <p><strong>Idea:</strong></p>
  <ul>
  <li><p>When the page finishes loading (DOM ready) - establish a connection to the Node.js server.</p>
  </li>
  <li><p>When the Node.js server accept a connection - generate a unique string and send it to the client browser. Store the user connection and the unique string in an object. e.g. <code>{UserID:&quot;6&quot;, value: {connectionObject}}</code></p>
  </li>
  <li><p>At client side, when this message arrives - store it in a hidden field or cookie. (for future requests to the NodeJs server )</p>
  </li>
  </ul>
  <hr />
  <p>When the server wants to send a message to John:</p>
  <ul>
  <li><p>Find john's UserID in the dictionary and send a message by the corresponding connection.</p>
  </li>
  <li><p>please notice there is no asp.net server code invloced here (in the message mechanism). only NodeJs .*</p>
  </li>
  </ul>
  <p><strong>Question:</strong></p>
  <p>Is this the right way to go?</p>