title: PHP WebSocket ZMQ - Chat Operation - Send data to specific user
body: <p>im working on a PHP project based on Symfony 2.2.11 and I installed the socketo related to the following tutorial <a href="http://socketo.me/docs/install" rel="nofollow">http://socketo.me/docs/install</a> to make my chat script working.</p>
  
  <p><strong>ServerCommand.php</strong> // Code of the command line that starts the WebSocket server</p>
  
  <pre><code>$oLoop = Factory::create();
  
      // Listen for the web server to make a ZeroMQ push after an ajax request
      $oContext = new Context($oLoop);
      $oPull = $oContext-&gt;getSocket(\ZMQ::SOCKET_PULL);
      // LET IT 127.0.0.1
      $oPull-&gt;bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
      $oPull-&gt;on('message', array($oChat, 'onMessage'));
  
      // Set up our WebSocket server for clients wanting real-time updates
      $oWebSock = new Server($oLoop);
      $oWebSock-&gt;listen(7979, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
      $webServer = new IoServer(
          new HttpServer(
              new WsServer(
                  new WampServer(
                      $oChat
                  )
              )
          ),
          $oWebSock
      );
  
      $oLoop-&gt;run();
  </code></pre>
  
  <p>After a message is being added to database : 
  <strong>MessagesController.php</strong></p>
  
  <pre><code>....
  // This is our new stuff
          $oContext = new \ZMQContext();
          $oSocket = $oContext-&gt;getSocket(\ZMQ::SOCKET_PUSH, 'PushMe');
          $oSocket-&gt;connect("tcp://mydomain:5555");
  
          $aData = array(
              'topic'         =&gt; 'message',
              'sUsername'     =&gt; $oUserCurrent-&gt;getUsername(),
              'sMessage'      =&gt; $sMessage
          );
  
          $oSocket-&gt;send(json_encode($aData));
  .....
  </code></pre>
  
  <p>The chat service : 
  <strong>Chat.php</strong></p>
  
  <pre><code>/**
   * A lookup of all the topics clients have subscribed to
   */
  public function onSubscribe(ConnectionInterface $conn, $topic) 
  {
      // When a visitor subscribes to a topic link the Topic object in a  lookup array
      $subject = $topic-&gt;getId();
      $ip = $conn-&gt;remoteAddress;
  
      if (!array_key_exists($subject, $this-&gt;subscribedTopics)) 
      {
          $this-&gt;subscribedTopics[$subject] = $topic;
      }
  
      $this-&gt;clients[] = $conn-&gt;resourceId;
  
      echo sprintf("New Connection: %s" . PHP_EOL, $conn-&gt;remoteAddress);
  
  }
  
  /**
   * @param string JSON'ified string we'll receive from ZeroMQ
   */
  public function onMessage($jData) 
  {
      $aData = json_decode($jData, true);
  
      var_dump($aData);
  
      if (!array_key_exists($aData['topic'], $this-&gt;subscribedTopics)) {
          return;
      }
  
      $topic = $this-&gt;subscribedTopics[$aData['topic']];
  
      // This sends out everything to multiple users, not what I want!!
  
      // re-send the data to all the clients subscribed to that category
      $topic-&gt;broadcast($aData);
  }
  </code></pre>
  
  <p>JS code that receives data : 
  <strong>messages.html.twig</strong> : </p>
  
  <pre><code>var conn = new ab.Session(
                      'ws://mydomain:7979' // The host (our Ratchet WebSocket server) to connect to
                    , function() {            // Once the connection has been established
                          conn.subscribe('message', function(topic, data) 
                          {
                              console.log(topic);
                              console.log(data);
                          });
                      }
                    , function() {            // When the connection is closed
                          console.warn('WebSocket connection closed');
                      }
                    , {                       // Additional parameters, we're ignoring the WAMP sub-protocol for older browsers
                          'skipSubprotocolCheck': true
                      }
                  );
  </code></pre>
  
  <p>So everytings working perfectly, when I send a new Message, it goes to DB then it lands on the page of the chat.</p>
  
  <p><strong>PROBLEM :</strong> 
  The data lands wherever the JS script is, and the result is that all users can get the same recorded message</p>
  
  <p><strong>ASKING :</strong>
  How can I make data lands in a <strong>specific user</strong> page ?</p>
  
  <p>Thank you</p>