Ratchet: Can't connect to websocket with wss

I’m having problems adding SSL/WSS support to my app

this is my chat-server.php:

$loop = Factory::create();

$secure_websockets = new Server($loop);
$secure_websockets->listen('8080', '0.0.0.0');
$webSock = new SecureServer($secure_websockets, $loop, [
    'local_cert' => 'path-to-file\server.crt',
    'local_pk' => 'path-to-file\server.key',
    'verify_peer' => false,
    'allow_self_signed' => true
]);

$webServer = new IoServer(
    new HttpServer(
        new WsServer(
            new Chat()
            )
        ),
    $webSock
    );
$loop->run();

On client side in javascript I’m trying to connect to the websocket with this:

connection = new WebSocket('wss://ip-address:8080');

I know that ip-address is correct and port is open

On firefox console, I’m getting this error when I try to connect to the websocket:

error { target: WebSocket, isTrusted: true, srcElement: WebSocket, eventPhase: 0, bubbles: false, cancelable: false, returnValue: true, defaultPrevented: false, composed: false, timeStamp: 2213441, type: "error"}

I’m not sure how to get this working, can someone help me please?

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 27 (3 by maintainers)

Most upvoted comments

<?php

//ReactPHP Event Loop
$loop   = React\EventLoop\Factory::create();

$webSock = new React\Socket\SecureServer(
	new React\Socket\Server('0.0.0.0:8080', $loop),
	$loop,
	array(
        'local_cert' => '/home/username/public_html/certsfolder/server.crt',
		'local_pk' => '/home/username/public_html/certsfolder/server.key',
		'allow_self_signed' => false,
		'verify_peer' => false
	)
);
						
// Ratchet magic
$webServer = new Ratchet\Server\IoServer(
	new Ratchet\Http\HttpServer(
		new Ratchet\WebSocket\WsServer(
			new Chat()
		)
	),
	$webSock
);

$loop->run();

You need to copy the TLS Certificates (Letsencrypt generated or other) into a folder that is accessible to this ssh user and provide the absolute path over there.

local_cert -> path to certificate local_pk -> path to private key

for me, self signed certificates did not work somehow, probably due to some other errors in that time, and everything worked, after trying Letsencrypt certificates and some experimentation.

the client side error is not really helping, try looking into the server-side errormessages like this:

$secure_websockets->on('error', function (Exception $e) {
    echo 'Error' . $e->getMessage() . PHP_EOL;
});

Also, did you include “wss://” in your connection attempt?