title: PHP WebSockets with Ratchet - example doesn't work
body: <p>Here's some background first.</p>
  
  <ol>
  <li><p>My aim is to use Ratchet WebSockets to create two-way client-server communication.</p></li>
  <li><p>I have installed ratchet and accompanying software, as described <a href="http://socketo.me/docs/install" rel="nofollow noreferrer">here</a>.</p></li>
  <li><p>I have successfully created a Hello World application as described <a href="http://socketo.me/docs/hello-world" rel="nofollow noreferrer">here</a>.</p></li>
  <li><p>Now I am trying to create Push functionality using <a href="http://socketo.me/docs/push" rel="nofollow noreferrer">this</a> tutorial. I have copied the code, modifying it slightly (modifications noted in code comments below), installed the ZMQ library (latest version, added it to php.ini, show up in <code>php -m</code> - in short, it's installed correctly). But the WebSockets don't work.</p></li>
  </ol>
  
  <p>I will give my testing process with real live links to my domain below, so you can check it yourself.</p>
  
  <ol>
  <li><p>My push server is exactly the same as the one in their tutorial, with the IP changed to my server's IP. I run this through SSH and it seems to connect correctly.</p></li>
  <li><p>My Pusher class is in the MyApp namespace, same code and same relative location as in their tutorial.</p></li>
  <li><p>My post.php is slightly modified because there's no need to bother with MySQL queries:</p></li>
  </ol>
  
  
  
  <pre><code>$entryData = array(        //hard-coded content of $entryData for simplicity
      'cat'     =&gt; "macka"
    , 'title'   =&gt; "naslov"
    , 'article' =&gt; "tekst"
    , 'when'    =&gt; time()
  );
  
  // This is our new stuff
  $context = new ZMQContext();
  $socket = $context-&gt;getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
  $socket-&gt;connect("tcp://light-speed-games.com:5555");       //my domain, still using port 5555 as in their example
  
  $socket-&gt;send(json_encode($entryData));
  </code></pre>
  
  <p>This file is located <a href="http://light-speed-games.com/apps/post.php" rel="nofollow noreferrer">here</a>.</p>
  
  <ol start="4">
  <li>My client.php is the same as theirs, except I had to add a little fix for IE to work with <code>when.js</code>. My problem is browser-independent and the same as it was before the fix was added.</li>
  </ol>
  
  
  
  <pre><code>&lt;script&gt;
      window.define = function(factory) {    //my addition
          try{ delete window.define; } catch(e){ window.define = void 0; } // IE
          window.when = factory();
      };
      window.define.amd = {};
  &lt;/script&gt;
  &lt;script src="/apps/scripts/when.js"&gt;&lt;/script&gt; 
  &lt;script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      var conn = new ab.Session(
          'ws://light-speed-games.com:8080' // The host (our Ratchet WebSocket server) to connect to
        , function() {            // Once the connection has been established
              conn.subscribe('kittensCategory', function(topic, data) {
                  // This is where you would add the new article to the DOM (beyond the scope of this tutorial)
                  console.log('New article published to category "' + topic + '" : ' + data.title);
              });
          }
        , 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
          }
      );
  &lt;/script&gt;
  </code></pre>
  
  <p>This file is located <a href="http://light-speed-games.com/apps/client.php" rel="nofollow noreferrer">here</a>.</p>
  
  <p>In theory, what <em>should happen</em> is this (for example): I open <code>client.php</code> in Chrome with console switched on; then I open <code>post.php</code> in Firefox; Chrome's console should show the message 'New article published...' etc (from the <code>conn.subscribe</code> function in <code>client.php</code>). However, when I do this, nothing happens. The connection remains open (doesn't show the 'connection closed' error until I switch off <code>push-server.php</code> through SSH). The console remains empty.</p>
  
  <p>I think that's all the relevant info from the last couple of days, a large portion of which I've spent on trying to figure this out. However, I've been unable to even make sure if the problem is with the code or with some server configuration setting I may be unaware of. So, I come to you hoping someone will point me in the right direction.</p>
  
  <h2>Important edit</h2>
  
  <p>I'm pretty sure the problem is with the Autobahn.js method <code>conn.subscribe</code> not working properly. The connection is being established. When I change the code to:</p>
  
  <pre><code>function() {            // Once the connection has been established
              console.log('Connection established');
              conn.subscribe('kittensCategory', function(topic, data) {
                  // This is where you would add the new article to the DOM (beyond the scope of this tutorial)
                  console.log('New article published to category "' + topic + '" : ' + data.title);
              });
          }
  </code></pre>
  
  <p>Then <code>Connection established</code> is shown in the console correctly. So I believe we need to troubleshoot the subscribe method. If someone can explain to me how it works, and what exactly "topic" and "data" are supposed to be, it would be of great help. The Autobahn documentation uses a URL as an argument for this method (see <a href="http://autobahn.ws/js/tutorials/pubsub/" rel="nofollow noreferrer">here</a>).</p>