laravel-websockets icon indicating copy to clipboard operation
laravel-websockets copied to clipboard

How can I configure Laravel websocket on digitalocean without using forge

Open Fajendagba opened this issue 3 years ago • 4 comments

So, I have a real time app which is working fine locally, but moving it to production I realize the real time feature is no longer working mainly because of Laravel websocket error: "WebSocket is closed before the connection is established."

"WebSocket connection to 'wss://mirrorlog.com/app/*******?protocol=7&client=js&version=7.0.6&flash=false' failed: "

I've been making lots of research about this, but most are just referring to forge.

My app is hosted on digital ocean

Your help would be appreciated

Fajendagba avatar Apr 19 '22 22:04 Fajendagba

Have you added SSL certificate details in websockets configuration file?

I was facing same issue and after 3 days of juggling with configuration, this worked for me. (Digitalocean / Ubuntu / Apache).

websockets.php

....
'ssl' => [

        'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null), // Add this variable in .env file

        'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null), // Add this variable in .env file

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

        'verify_peer'   =>  false,  //Don't forget to add this line.
    ],
....

.env

....
LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT="/etc/letsencrypt/live/yourdomain.com/fullchain.pem"
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK="/etc/letsencrypt/live/yourdomain.com/privkey.pem"
LARAVEL_WEBSOCKETS_SSL_PASSPHRASE="YourPassPhraseIfAny"

boradcasting.php

...
'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'host' => 'yourdomain.com', // 127.0.0.1 didn't worked for me
                'port' => 6001,
                'scheme' => 'https',
                'encrypted' => true,
                'curl_options' => [
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ]
            ],
            'client_options' => [

            ],
        ],
...

Try running php artisan websocket:serve with sudo user or run sudo php artisan websocket:serve. If you are using supervisor run the supervisor with sudo user. Also make sure that your SSL certificate files are accessible and have read permission.

rajatpatelz avatar May 16 '22 06:05 rajatpatelz

@rajatpatelz thank you do much.

But can you please explain how I can generate these keys:

LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT="/etc/letsencrypt/live/yourdomain.com/fullchain.pem" LARAVEL_WEBSOCKETS_SSL_LOCAL_PK="/etc/letsencrypt/live/yourdomain.com/privkey.pem" LARAVEL_WEBSOCKETS_SSL_PASSPHRASE="YourPassPhraseIfAny

I use cloudflare for SSL

Fajendagba avatar May 16 '22 07:05 Fajendagba

Sorry I can't help you with that, better google it.

rajatpatelz avatar May 16 '22 10:05 rajatpatelz

Have you added SSL certificate details in websockets configuration file?

I was facing same issue and after 3 days of juggling with configuration, this worked for me. (Digitalocean / Ubuntu / Apache).

websockets.php

....
'ssl' => [

        'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null), // Add this variable in .env file

        'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null), // Add this variable in .env file

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

        'verify_peer'   =>  false,  //Don't forget to add this line.
    ],
....

.env

....
LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT="/etc/letsencrypt/live/yourdomain.com/fullchain.pem"
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK="/etc/letsencrypt/live/yourdomain.com/privkey.pem"
LARAVEL_WEBSOCKETS_SSL_PASSPHRASE="YourPassPhraseIfAny"

boradcasting.php

...
'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'host' => 'yourdomain.com', // 127.0.0.1 didn't worked for me
                'port' => 6001,
                'scheme' => 'https',
                'encrypted' => true,
                'curl_options' => [
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ]
            ],
            'client_options' => [

            ],
        ],
...

Try running php artisan websocket:serve with sudo user or run sudo php artisan websocket:serve. If you are using supervisor run the supervisor with sudo user. Also make sure that your SSL certificate files are accessible and have read permission.

ily bro

AmrAb-05 avatar Jul 30 '22 16:07 AmrAb-05

Have you added SSL certificate details in websockets configuration file?

I was facing same issue and after 3 days of juggling with configuration, this worked for me. (Digitalocean / Ubuntu / Apache).

websockets.php

....
'ssl' => [

        'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null), // Add this variable in .env file

        'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null), // Add this variable in .env file

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

        'verify_peer'   =>  false,  //Don't forget to add this line.
    ],
....

.env

....
LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT="/etc/letsencrypt/live/yourdomain.com/fullchain.pem"
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK="/etc/letsencrypt/live/yourdomain.com/privkey.pem"
LARAVEL_WEBSOCKETS_SSL_PASSPHRASE="YourPassPhraseIfAny"

boradcasting.php

...
'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'host' => 'yourdomain.com', // 127.0.0.1 didn't worked for me
                'port' => 6001,
                'scheme' => 'https',
                'encrypted' => true,
                'curl_options' => [
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ]
            ],
            'client_options' => [

            ],
        ],
...

Try running php artisan websocket:serve with sudo user or run sudo php artisan websocket:serve. If you are using supervisor run the supervisor with sudo user. Also make sure that your SSL certificate files are accessible and have read permission.

Thanks men it works! it is my 3rd day now exploring this issue

JustinPaul1025 avatar Apr 07 '23 13:04 JustinPaul1025

    'verify_peer'   =>  false,  //Don't forget to add this line.

Nobody ever talked about that. After a few days debugging, now it works, thanks a lot!!

lucaspeixoto avatar Jan 19 '24 19:01 lucaspeixoto