Search code examples
sslratchetreactphp

Using Ratchet WsServer in combination with React SecureServer (WSS / SSL)


We are trying to implement websockets over secure connection (WSS) and have cboden/ratchet implemented in our project. All Works fine over normal connection. Looking around in the docs and the Github project pages, it seems te new React SecureServer class is not yet implemented in Ratchet.

In the issues we found a post about it, and it won't be supported untill later versions, however, we need it now :D At the issue report they suggest using the new React SecureServer class in combination with Ratchet classes.. We tried and got a fatal error:

Catchable fatal error: Argument 1 passed to React\Socket\SecureServer::__construct() must be an instance of React\Socket\Server, instance of Ratchet\Http\HttpServer given.

Part where it goes wrong:

$webServer = new Ratchet\Server\IoServer(
    new React\Socket\SecureServer( // Using React class here as suggested
        new Ratchet\Http\HttpServer(
            new Ratchet\WebSocket\WsServer(
                new Ratchet\Wamp\WampServer(
                    $pusher
                )
            )
        )
    ),
    $webSock, 
    array(
        'local_cert' => 'xxxx.pem',
        'allow_self_signed' => true,
        'verify_peer' => false
    )
);

Are we doing something stupid? :p


Solution

  • The secure server is supposed to be wrapped around the socket server, not the HTTP server:

      $webServer = new Ratchet\Server\IoServer(
        new Ratchet\Http\HttpServer(
            new Ratchet\WebSocket\WsServer(
              new Ratchet\Wamp\WampServer(
                $pusher
              )
            )
          )
        ),
        new React\Socket\SecureServer( // Using React class here as suggested
          $webSock,
          $loop,
          array(
            'local_cert' => 'xxxx.pem',
            'allow_self_signed' => true,
            'verify_peer' => false
          )
        )
      );