title: Send user ID from browser to websocket server while opening connection
body: <blockquote>
    <p>Before asking this question, I did my best by reading severel
    questions on SO (tagged Ratchet and dealing with similar issues but to
    no avail. I even asked a question which received no attention and I
    therefore deleted it to write another one (that hopefully is more
    clear).</p>
  </blockquote>
  
  <p>My final goal is to build a one-to-one private chat application using Ratchet. Everything is working fine except that I can't send message to a specific user.</p>
  
  <p>Every logged in user connects to the websocket server while accessing secured area of website:</p>
  
  <pre><code>$(document).ready(function() { 
  
      var conn = new WebSocket('ws://localhost:8080');
          conn.onopen = function(e) {
              console.log("Connection established!");
  
              // Here I need to send the logged in user_id to websocket server
              // and get it in onOpen method so that I can index my array 
              // of connections with user_id instead of
              //$connection-&gt;ResourceId, I explain more below
  
          };
  
          conn.onmessage = function(e) {
              console.log(e.data);
          };
  
  });
  </code></pre>
  
  <p>When a user writes a message in the chat box, the message is sent via AJAX to web server then pushed to Websocket using ZeroMQ. In the controller:</p>
  
  <pre><code>// Persistence of Message(message_id, sender_id, receiver_id, message_text)
                  .....
  
                  $context = new \ZMQContext();
                  $socket = $context-&gt;getSocket(\ZMQ::SOCKET_PUSH, 'my pusher');
                  $socket-&gt;connect("tcp://localhost:5555");
  
                  $pushData = array(
                         'receiver_id' =&gt; $receiver_id,
                         'sender_id'  =&gt; $user-&gt;getId(),
                         'message'  =&gt; $message-&gt;getMessageText(),
                      );
                  $socket-&gt;send(json_encode($pushData));
  </code></pre>
  
  <p>So at the end, my websocket server is able to know which is the id of receiver using the JSON. But how will he know which is the connection of that user? In other words, I need to store websocket connections in an array that is indexed by the user id.</p>
  
  <pre><code>&lt;?php
  namespace RealTime;
  
  use Ratchet\MessageComponentInterface;
  use Ratchet\ConnectionInterface;
  use Ratchet\Wamp\WampServerInterface;
  
  class Pusher implements WampServerInterface, MessageComponentInterface{
  
      private $clients;
  
      public function onOpen(ConnectionInterface $conn) {
  
          $this-&gt;clients[$conn-&gt;resourceId] = $conn;
          // I need here to get the user_id received from browser while opening connection
      }
  
      public function onMessageEntry($entry) {
          $entryData = json_decode($entry, true);
  
          //This is not what I need (It sends to all users in array)
          foreach ($this-&gt;clients as $key =&gt; $client) {
  
          $client-&gt;send($entryData['message']); 
          }
      }
      public function onMessage(ConnectionInterface $from, $msg) {
          echo $msg; 
      }
  }
  </code></pre>
  
  <p>And the websocket server:</p>
  
  <pre><code>  &lt;?php
          require dirname(__DIR__) . '/vendor/autoload.php';
          use RealTime\Pusher;
  
          $loop   = React\EventLoop\Factory::create();
          $pusher = new Pusher;
  
          $context = new React\ZMQ\Context($loop);
          $pull = $context-&gt;getSocket(ZMQ::SOCKET_PULL);
          $pull-&gt;bind('tcp://127.0.0.1:5555'); 
          $pull-&gt;on('message', array($pusher, 'onMessageEntry'));
  
  
          $webSock = new React\Socket\Server($loop);
          $webSock-&gt;listen(8080, '0.0.0.0'); 
          $webServer = new Ratchet\Server\IoServer(
              new Ratchet\Http\HttpServer(
                  new Ratchet\WebSocket\WsServer(
                      new Ratchet\Wamp\WampServer(
                          $pusher
                      )
                  )
              ),
              $webSock
          );
          $loop-&gt;run();
  
          ?&gt;
  </code></pre>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li><p>How to send the logged in <code>user_id</code> from client side while opening connection.I need to have the value in websocket server so that I can index my array of clients with it (<code>$client[user_id]=$conn</code> instead of <code>$client[recourceId]=$conn</code>). I tried the javascript function <code>send</code> but I don't know where to receive the sent data (even <code>onMessage</code> is not printing anything).</p></li>
  <li><p>Why the <code>onMessage</code> method is not executing even <code>MessageComponentInterface</code> implemented (Is it because I have <code>onMessageEntry</code> method + <code>$pull-&gt;on('message', array($pusher, 'onMessageEntry'));</code> line of code?</p></li>
  </ol>
  
  <p>Thank you. </p>