Search code examples
phpsymfonywebsocketratchet

Symfony Ratchet WSS


I've got a problem with HTTPS => WS communication and can't find the way to solve it.

I'm using Symfony 4.1 and Ratchet WsServer. Server starts through Symfony command on 9090 port and work properly with ws on local machine with http. Of course on https I switched it to wss and got this error:

WebSocket connection to 'wss://servername:9090/' failed: WebSocket opening handshake timed out

My back end code:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $server = IoServer::factory(new HttpServer(
        new WsServer(
            new Widget($this->getContainer(), $this->logger)
        )
    ), 9090);

    $server->run();
}

May be anybody know how to solve it. I saw some guides about nginx and apache configuration in same situations, but I'm not sure, because in this case Server starts from PHP and Symfony.

Another interesting moment is then I try to connect through browser to WSS, I get the same error. Looks like problem not in certificate, but in server.


Solution

  • I solve it with NGINX proxy. I have NGINX+Apache and it this case it helps to add proxy to NGINX.

    upstream websocket_server {
         server app-ip:9091;
    }
    server {
        listen      46.101.45.214:443;
        server_name app-name ;
        ssl         on;
        ssl_certificate      /home/admin/conf/web/ssl.app-name.pem;
        ssl_certificate_key  /home/admin/conf/web/ssl.app-name.key;
        error_log  /var/log/apache2/domains/app-name.error.log error;
    
        location /wss/ {
            proxy_pass  http://websocket_server;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
            proxy_read_timeout 86400;
        }
    .....