'PHP WebSocket connection issue over SSL

I have some issues connecting a web socket in PHP.

I'm using this repo: https://github.com/ghedipunk/PHP-Websockets

If I run in localhost, everything is fine, the service is on. But when I move to the server is where everything comes down. I have an AWS Lightsail hosting, my domain has SSL, and I'm using Plesk.

In my AWS panel, I opened port 8000 By console I added this line in /etc/apache2/mods-enabled/proxy_wstunnel.load:

ProxyPass "/chat/"  "wss://localhost:8000/"

and also

ProxyPass "/chat/"  "wss://socket.injectxgames.io:8000/"

Then sudo service apache2 restart

Then I enter the console to execute the PHP file with the service and I have this:

Server started
Listening on: localhost:8000
Master socket: Resource id #6

Everything is just fine, but JS is where I can't able to connect.

function init() {
    // Apuntar a la IP/Puerto configurado en el contructor del WebServerSocket, que es donde está escuchando el socket.
    var host = "wss://socket.injectxgames.io:8000"; 
    try {
        socket = new WebSocket(host);
        log('WebSocket - status '+socket.readyState);
        socket.onopen    = function(msg) { 
                               log("Welcome - status "+this.readyState); 
                           };
        socket.onmessage = function(msg) { 
                               log("Received: "+msg.data); 
                           };
        socket.onclose   = function(msg) { 
                               log("Disconnected - status "+this.readyState); 
                           };
    }
    catch(ex){ 
        log(ex); 
    }
    $("msg").focus();
}

Even I have tried as www.injectxgames.io/chat and nothing, I have this error:

"WebSocket connection to 'wss://www.injectxgames.io:8000/' failed"

What could be missing?



Solution 1:[1]

Please try using these:

Insert this for the non-SSL vhost of your domain:

ProxyPass /chat ws://localhost:8000
ProxyPassReverse /chat ws://localhost:8000

and this inside the SSL vhost for your domain:

ProxyPass /chat wss://YOUR_DOMAIN_WITH_SSL.COM:8000
ProxyPassReverse /chat wss://YOUR_DOMAIN_WITH_SSL.COM:8000

If still does not work, you might want to find a managed hosting provider who can help with the server setup and similar configurations for you. You can do some research and find a suitable one at: https://hostadvice.com/managed-hosting/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BrunoMirchevski