Skip to content

Instantly share code, notes, and snippets.

@LukasCCB
Last active September 25, 2024 10:29
Show Gist options
  • Save LukasCCB/dbc89f2a70b4fbb3cdefd89fbb31aa6a to your computer and use it in GitHub Desktop.
Save LukasCCB/dbc89f2a70b4fbb3cdefd89fbb31aa6a to your computer and use it in GitHub Desktop.
How I Finally Solved Websocket SSL Issues in Laravel
I've encountered WebSocket issues in Laravel since Laravel 5, and it persisted through versions 9, 10, and now 11. The primary problem has always been getting WebSockets to run over WSS/HTTPS.
After numerous attempts, I found a solution to successfully run WebSocket over SSL. Here's how I did it.
Steps to Run WebSocket with SSL in Laravel
1. Generate SSL Certificates for your Domain or Host IP
First, generate the private key, CSR (Certificate Signing Request), and certificate for your domain (e.g., you-domain.net).
# Generate a private key
openssl genrsa -out you-domain.net.key 2048
# Create a Certificate Signing Request (CSR)
openssl req -new -key you-domain.net.key -out you-domain.net.csr
# Generate a self-signed certificate
openssl x509 -req -days 365 -in you-domain.net.csr -signkey you-domain.net.key -out you-domain.net.crt
2. Combine Keys into a Single cert.pem File
After generating the .crt and .key files, combine them into a single cert.pem file.
# Combine the certificate and private key into cert.pem
cat you-domain.net.crt you-domain.net.key > cert.pem
3. Configure Laravel to Use Your SSL Certificate
In Laravel, you need to reference your cert.pem file in the WebSocket configuration.
Update your config/reverb.php file to include the path to your certificate and disable SSL verification (for development purposes; ensure to handle this securely in production):
'options' => [
'verify' => false, // Disable SSL verification (Adjust for production)
'tls' => [
'local_cert' => base_path(env('REVERB_TLS_CERT_PATH', '')),
],
],
4. Update Your .env File
Ensure your .env file is correctly set up with the domain and IP address for WebSockets.
REVERB_HOST=you-domain.net
REVERB_SERVER_HOST=77.37.69.219
REVERB_TLS_CERT_PATH=/storage/certificates/cert.pem
5. Additional Configuration for SSL
In both config/reverb.php and config/broadcasting.php, ensure that you add the 'verify' => false option in the Guzzle client settings to bypass SSL verification (for development purposes only):
'client_options' => [
'verify' => false,
],
6. Start the WebSocket Server
Finally, start the WebSocket server using Artisan:m php artisan reverb:start
Now your Laravel application should be running WebSockets over SSL, and the connection should be secured with the cert.pem file.
Note:
Although this setup works, disabling SSL verification ('verify' => false) is not recommended for production environments, as it compromises security. Ensure to handle SSL verification properly when moving to production.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment