title: Basic react http server and websocket server at the same time
body: <p>I managed to create a web service that listens on port 80 for http requests and handles ajax calls and also long polling.</p>
  
  <p>But I'm stuck at creating a similar websocke server in the same php file</p>
  
  <pre><code>&lt;?php
  require_once('vendor/autoload.php');
  
  use Ratchet\MessageComponentInterface;
  use Ratchet\ConnectionInterface;
  
  class Chat implements MessageComponentInterface {
      protected $clients;
  
      public function __construct() {
          $this-&gt;clients = new \SplObjectStorage;
      }
  
      public function onOpen(ConnectionInterface $conn) {
          // Store the new connection to send messages to later
          $this-&gt;clients-&gt;attach($conn);
          $conn-&gt;send('aa');
          echo "New connection! ({$conn-&gt;resourceId})\n";
      }
  
      public function onMessage(ConnectionInterface $from, $msg) {
          $numRecv = count($this-&gt;clients) - 1;
          echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
              , $from-&gt;resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
  
          foreach ($this-&gt;clients as $client) {
              if ($from !== $client) {
                  // The sender is not the receiver, send to each client connected
                  $client-&gt;send($msg);
              }
          }
      }
  
      public function onClose(ConnectionInterface $conn) {
          // The connection is closed, remove it, as we can no longer send it messages
          $this-&gt;clients-&gt;detach($conn);
  
          echo "Connection {$conn-&gt;resourceId} has disconnected\n";
      }
  
      public function onError(ConnectionInterface $conn, \Exception $e) {
          echo "An error has occurred: {$e-&gt;getMessage()}\n";
  
          $conn-&gt;close();
      }
  }
  
  
  $loop = React\EventLoop\Factory::create();
  
  $swsa=new Chat;
  $webSock = new React\Socket\Server($loop);
  $webSock-&gt;listen(8687, '127.0.0.1'); // Binding to 0.0.0.0 means remotes can connect
  $webServer = new Ratchet\Server\IoServer(
      new Ratchet\Http\HttpServer(
          new Ratchet\WebSocket\WsServer(
              $swsa
          )
      ),
      $webSock
  );
  
  $app = function ($request, $response) {
      //some code here to manage requests
      $response-&gt;writeHead(200,array('Content-Type'=&gt;'application/json'));
      $response-&gt;end(json_encode(array('ok'=&gt;'ok')));
  };
  $socket = new React\Socket\Server($loop);
  $http = new React\Http\Server($socket, $loop);
  $http-&gt;on('request', $app);
  $socket-&gt;listen(8485);
  
  $loop-&gt;run();
  </code></pre>
  
  <p>Code in browser:</p>
  
  <pre><code>var wsUri = "ws://127.0.0.1:8687/";
  websocket = new WebSocket(wsUri);
  </code></pre>
  
  <p>This triggers an error in the browser</p>
  
  <pre><code>Firefox can't establish a connection to the server at ws://127.0.0.1:8687/
  </code></pre>