Search code examples
laravel-5websocketratchet

How to Configure WebSocket Server to Accept Secure Connection Request


Before applying ssl(I take from cloudflare) my website is loaded over http and my socket connection is made over ws and it's working fine and connection was made successfully.

conn = new WebSocket('ws://myDomain:8090');

But after applying ssl when my website loads over https the I use wss (otherwise it give error)

conn = new WebSocket('wss://myDomain:8090');

Now it gives me the error

WebSocket connection to 'wss://myDomain:8090/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

The websocket server is started over 8090 port I also change the port to 9991 but to no avail.

Here is the code for websocket server

public function handle()
{
    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                new WebSocketController()
            )
        ),
        8090
    );
    $server->run();
}

I don't configure apache to to run a websocket server to accept secure connection request. May be due to this I am getting an error. It means that I am sending a secure connection request to an insecure websocket server. If I am right can you tell me how I configure my websocket server so that it can accept secure connection request.

I am again telling you that I am using the SSL from cloud flare. I tell me my domain and they provide me nameservers to replace it with my existing nameservers.

I requested you to give a clear solution to solve this. I am not using nginx, I am using apache on Lampp.


Solution

  • Someone solved my problem. So I am posting the solution here to help others. I was making two mistakes.

    • I am using SSL from cloudflare, which causes some issues. So I buy a paid SSL certificate.
    • I don't configure my websocket server for wss

    So here is the code to configure your websocket server for wss in Laravel with Ratchet

    public function handle()
    {
    
        $loop   = Factory::create();
        $webSock = new SecureServer(
            new Server('0.0.0.0:8090', $loop),
            $loop,
            array(
                'local_cert'        => '', // path to your cert
                'local_pk'          => '', // path to your server private key
                'allow_self_signed' => TRUE, // Allow self signed certs (should be false 
                                                in production)
                'verify_peer' => FALSE
            )
        );
        // Ratchet magic
        $webServer = new IoServer(
            new HttpServer(
                new WsServer(
                    new WebSocketController()
                )
            ),
            $webSock
        );
        $loop->run();
    
    }