'Cannot connect to own websocket server when on secured domain

I deployed a standalone WebSocket server in google cloud. Following the laravel-websockets configuration, I can connect to my server when I go to <ip address>/laravel-websockets.

enter image description here

I installed an SSL certificate from Let's Encrypt then I configured the .env file to point to my certs and key files:

LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT=/etc/letsencrypt/live/mydomain.com/fullchain.pem LARAVEL_WEBSOCKETS_SSL_LOCAL_PK=/etc/letsencrypt/live/mydomain.com/privkey.pem

Then I open the WebSockets dashboard, using the domain name instead of the IP mydomain.com/laravel-websockets. I restarted apache sudo systemctl restart apache2 and run php artisan WebSockets:serve. Then I tried connecting but failed.

enter image description here

The console shows:

WebSocket connection to 'wss://mydomain.com:6001/app/mypusherkey?protocol=7&client=js&version=4.3.1&flash=false' failed

Please advise as I need this to work soon. Thank you.



Solution 1:[1]

From what I see in documentation here: https://github.com/beyondcode/laravel-websockets/blob/master/docs/basic-usage/ssl.md and from you mentioning the .env file, I am afraid you have a simple misconfiguration.

Variables that are configured to the PHP $_ENV or to be retrieved with getenv() are configured via php.ini file. If you use contenerized environment what does suggest to me the fact, that you are straightforward mentioning .env file, here on shiphp is a decent article about some solutions to achieve custom env vars. I would first try one of two mentioned there solutions:

Mix the --env-file to the docker command

shiphp suggests command

docker run --rm -v $(pwd):/app -w /app --env-file .env php:cli php index.php

and this is pretty close to what you need to run PHP (here php:cli image) with your custom .env file as variables configuration

Docker ENV variables

This is less straightforward but more managable. Also shiphp suggests that you can create your own image:

FROM php:cli
ENV LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT=...
ENV LARAVEL_WEBSOCKETS_SSL_LOCAL_PK=...

All suggestions are related to docker or configuration, because unfortunately you left very little information for PHP developer to help you here. Imo your socket is not up, or is up but with default configuration, what would be non-ssl. Please try to first determine if the socket is up, before trying to debug connection from other application to it. You can connect from HTTP website to both WS and WSS. Bare in mind connecting from HTTPS is possible only to WSS.

Solution 2:[2]

I agree with @yergo, sounds like you have a misconfiguration, you can hit it via IP but not domain, check your env inside the docker container. "docker -exec -it echo $<ENV_VAR>". If response is empty then your env settinhs are not applied to the container.

Solution 3:[3]

This might be a dumb question, but are you sure if the port 6001 isn't blocked by a firewall?

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 yergo
Solution 2 Benz683
Solution 3 Honk der Hase