title: How to bind ZeroMQ socket with Ratchet web-socket library to make real time application for php application?
body: <p>I am just a beginner in this whole area involving websocket, Ratchet and ZeroMQ. </p>
  
  <p>To my basic understanding:</p>
  
  <p><code>websocket</code> is something that helps to create open connection between server and client.  </p>
  
  <p><code>Ratchet</code> is a PHP based library which uses PHP's core Socket functions to create a PHP socket framework allowing us to ease in PHP socket programming. </p>
  
  <p><code>ZeroMQ</code> is a socket library that helps non-ratchet application to (other PHP script) send the data over Ratchet Socket and web-socket.</p>
  
  <p>I am following the tutorial found in ratchet about 'hello world' and 'pusher' but both of them seems to be incomplete and only teaches how to work with console only. I have also found ratchet-example in github but it is not properly documented. I was looking for a complete example (with a dedicated html page and javascript) </p>
  
  <p>Below is the code I am working on: This is one of the method of the controller which I am making a Ajax request for. This method will create a new post (lets say). I want to update the list of post dynamically in multiple client's browser by broadcasting/pushing with the help of ZeroMq.</p>
  
  <p>A method in a controller:</p>
  
  <pre><code>public function create_new_post(){
      // ------
      // code to create a new post.
      // -------
  
      // After creating a post
      $response = [
          'new_post_title'    =&gt; $title,
          'post_id'           =&gt; $id
      ];
  
      $context = new ZMQContext();
      $socket = $context-&gt;getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
      $socket-&gt;connect("tcp://localhost:8000");
      $socket-&gt;send(json_encode($response));
  
  }
  </code></pre>
  
  <p>Pusher File:</p>
  
  <pre><code>use Ratchet\ConnectionInterface;
  use Ratchet\Wamp\WampServerInterface;
  
  class Pusher implements WampServerInterface{
  
       public function onPostEntry($data){
           // Data that were sent by ZeroMQ through create_new_post() method
           $entry_data = json_decode($data);      
  
           // AND AFTER THIS, I DONT HAVE CLUE OF WHAT TO DO NEXT !!             
  
       }
  }
  </code></pre>
  
  <p>Shell Script to run server:</p>
  
  <pre><code>require dirname(__DIR__) . '/vendor/autoload.php';
  
  $loop   = React\EventLoop\Factory::create();
  $pusher = new MyApp\Pusher;
  
  // Listen for the web server to make a ZeroMQ push after an ajax request
  $context = new React\ZMQ\Context($loop);
  $pull = $context-&gt;getSocket(ZMQ::SOCKET_PULL);
  $pull-&gt;bind('tcp://127.0.0.1:8000'); 
  $pull-&gt;on('message', array($pusher, 'onBidEntry'));
  
  // Set up our WebSocket server for clients wanting real-time updates
  $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();
  </code></pre>
  
  <p>Shell script only tells that it will serve in port 8080, however how would I mention my routes. Lets say I want the open connection in only the page 'mysite/allposts'. Also, what would be the script that I have to write in client side (a javascript file) and how to receive these new data by client side updating a particular DOM object. </p>