Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Fellipe's avatar

WebSocket with SSL not connection and not log

hello, I've had this error for a long time now, and I can't solve it, could someone who has worked with websocket help me??

my .env

BROADCAST_DRIVER=pusher

CACHE_DRIVER=file

FILESYSTEM_DRIVER=local

QUEUE_CONNECTION=database

SESSION_DRIVER=file

SESSION_LIFETIME=120

LARAVEL_WEBSOCKETS_PORT=6001

LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT=/usr/local/directadmin/data/users/domain/domains/domain.com.br.cert.combined

LARAVEL_WEBSOCKETS_SSL_LOCAL_PK=/usr/local/directadmin/data/users/domain/domains/domain.com.br.key

LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT_CA=/usr/local/directadmin/data/users/domain/domains/domain.com.br.cacert

LARAVEL_WEBSOCKETS_ENCRYPT=C2dUFpgSCgrF/jfUbtsGENC2aFVfNWBhxmdv8dEi9MLNjaGsENxXJ/yHLHQImd6g3qelQ8Ht8CoLHP8RwU0UOQ==

PUSHER_APP_ID=be4536df5a40d5874254ac4dd434babb9ba1fe3418a97fa7cd2547a739377fec

PUSHER_APP_KEY=d2e8454a6bce7766b9b9d630babb1dcc8c6ed63ea2a59223d22b989f77d6bcd2

PUSHER_APP_SECRET=b70c0549cce09b5df070db3f519f4c13f111de137b2016f817e27adef6ef5efb

PUSHER_APP_CLUSTER=sa1

PUSHER_APP_HOST="${APP_URL}"

PUSHER_APP_PORT="${LARAVEL_WEBSOCKETS_PORT}"

MIX_PUSHER_APP_ID="${PUSHER_APP_ID}"

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"

MIX_PUSHER_APP_SECRET="${PUSHER_APP_SECRET}"

MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

MIX_PUSHER_APP_HOST="${PUSHER_APP_HOST}"

MIX_PUSHER_APP_PORT="${PUSHER_APP_PORT}"

my supervisord.conf

[program:worker]

directory=/home/domain/core

process_name=%(program_name)s_%(process_num)02d

command=php artisan queue:work

numprocs=8

priority=999

autostart=true

autorestart=true

startsecs=1

startretries=3

redirect_stderr=true

stdout_logfile=/home/domain/core/worker/worker.log

[program:websockets]

directory=/home/domain/core

command=sudo php artisan websockets:serve

numprocs=1

autostart=true

autorestart=true

stderr_logfile=/home/domain/core/websockets-log/websockets.err.log

stdout_logfile=/home/domain/core/websockets-log/websockets.out.log

my log websocket.conf

Starting the WebSocket server on port 6001...

my httpd.conf

<VirtualHost *:443>

ServerName domain.com.br

SSLEngine on
SSLCertificateFile /usr/local/directadmin/data/users/domain/domains/domain.com.br.cert.combined
SSLCertificateKeyFile /usr/local/directadmin/data/users/domain/domains/domain.com.br.key

ProxyPass / wss://127.0.0.1:6001/
ProxyPassReverse / wss://127.0.0.1:6001/

RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) wss://127.0.0.1:6001/ [P,L]

RequestHeader set Connection "upgrade" env=wsupgrade
RequestHeader set Upgrade "websocket" env=wsupgrade

my websockets.php

'apps' => [

[

    'id' => env('PUSHER_APP_ID'),

    'name' => env('APP_NAME'),

    'key' => env('PUSHER_APP_KEY'),

    'secret' => env('PUSHER_APP_SECRET'),

    'enable_client_messages' => true,

    'enable_statistics' => true,

    'capacity' => null

],

],

'statistics' => [

'model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class,

'logger' => BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger::class,

'interval_in_seconds' => 60,

'delete_statistics_older_than_days' => 60,

'perform_dns_lookup' => false,

],

'ssl' => [

'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),

'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null),

'capath' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT_CA', null),

'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null),

],

my broadcasting.php

'pusher' => [

'driver' => 'pusher',

'app_id' => env('PUSHER_APP_ID'),

'key' => env('PUSHER_APP_KEY'),

'secret' => env('PUSHER_APP_SECRET'),

'enable_client_messages' => true,

'enable_statistics' => true,

'options' => [

    'scheme' => 'https',

    'cluster' => env('PUSHER_APP_CLUSTER'),

    'host' => '127.0.0.1',

    'port' => env('LARAVEL_WEBSOCKETS_PORT', 443),

    'encrypted' => true,

    'useTLS' => true,

]

],

my bootstrap.js

import Echo from 'laravel-echo';

import Pusher from 'pusher-js';

window.Pusher = Pusher;

window.Echo = new Echo({

broadcaster: 'pusher',

key: process.env.MIX_PUSHER_APP_KEY,

wsHost: window.location.hostname,

wssPort: process.env.MIX_PUSHER_APP_PORT,

wsPort: process.env.MIX_PUSHER_APP_PORT,

forceTLS: true,

disableStats: true,

enabledTransports: ['ws', 'wss'],

cluster: process.env.MIX_PUSHER_APP_CLUSTER,

encrypted: true

});

composer.json

"php": "^7.3|^8.0",

"pusher/pusher-php-server": "^7.2"

package.json

"pusher-js": "^7.6.0",

systemctl status supervisord.service

● supervisord.service - Process Monitoring and Control Daemon

Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)

Active: active (running) since Wed 2023-04-26 18:09:45 -03; 8s ago Process: 18931 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)

Main PID: 18934 (supervisord)

Tasks: 9 (limit: 35948)

Memory: 310.5M

CGroup: /system.slice/supervisord.service

       ├─18934 /usr/bin/python3.6 /usr/bin/supervisord -c /etc/supervisord.conf

       ├─18936 php artisan queue:work

       ├─18937 php artisan queue:work

       ├─18938 php artisan queue:work

       ├─18939 php artisan queue:work

       ├─18940 php artisan queue:work

       ├─18941 php artisan queue:work

       ├─18942 php artisan queue:work

       └─18943 php artisan queue:work

sudo netstat -tulnp

tcp 0 0 0.0.0.0:6001 0.0.0.0:* LISTEN 18944/php

onlyvue.js:2 WebSocket connection to 'wss://domain.com.br:6001/app/d2e8454a6bce7766b9b9d630babb1dcc8c6ed63ea2a59223d22b989f77d6bcd2?protocol=7&client=js&version=7.6.0&flash=false' failed: WebSocket is closed before the connection is established.

0 likes
1 reply
LaryAI's avatar
Level 58

Based on the provided information, it seems that the WebSocket connection is failing due to SSL issues. Here are some steps that can be taken to resolve the issue:

  1. Check if the SSL certificate and key files are valid and accessible. Make sure that the paths specified in the .env file are correct and the files have the correct permissions.

  2. Verify that the SSL configuration in the httpd.conf file is correct. Check if the SSL certificate and key files are correctly specified and the ProxyPass and RewriteRule directives are properly configured.

  3. Check if the WebSocket server is running and listening on the correct port. Use the netstat command to verify if the server is listening on the specified port.

  4. Verify that the Pusher credentials are correct and the app is configured properly. Check if the app_id, key, and secret values in the .env file and the broadcasting.php file are correct.

  5. Check if the laravel-echo and pusher-js packages are installed and configured properly. Make sure that the bootstrap.js file is correctly configured to use the Pusher credentials.

Here is an example of how the SSL configuration in the httpd.conf file should look like:

<VirtualHost *:443>
    ServerName domain.com.br

    SSLEngine on
    SSLCertificateFile /usr/local/directadmin/data/users/domain/domains/domain.com.br.cert.combined
    SSLCertificateKeyFile /usr/local/directadmin/data/users/domain/domains/domain.com.br.key

    ProxyPass / http://127.0.0.1:6001/
    ProxyPassReverse / http://127.0.0.1:6001/

    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /(.*) ws://127.0.0.1:6001/ [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /(.*) http://127.0.0.1:6001/ [P,L]

    RequestHeader set Connection "upgrade" env=wsupgrade
    RequestHeader set Upgrade "websocket" env=wsupgrade
</VirtualHost>

Note that the ProxyPass and RewriteRule directives use the http protocol instead of the wss protocol. This is because the SSL termination is done by the Apache server and the WebSocket server only needs to handle unencrypted traffic.

Also, the RewriteCond and RewriteRule directives are used to handle WebSocket traffic separately from regular HTTP traffic. The RequestHeader directives are used to set the Connection and Upgrade headers to upgrade the connection to a WebSocket connection.

After making the necessary changes, restart the Apache server and the WebSocket server and try connecting to the WebSocket server again.

Please or to participate in this conversation.