Laravel-websockets: Not working with Laravel Valet

Created on 6 Dec 2018  路  14Comments  路  Source: beyondcode/laravel-websockets

Thank you for making this package. It comes at the perfect time for me, I'm just starting to use websockets on a project I've got running.

It might be an oversight on my part, but I can't get this to work with Laravel Valet. Using Pusher with Laravel's default config works, but as soon as I make the necessary changes in the config and try to broadcast an event I get an empty Illuminate\Broadcasting\BroadcastException.

config/broadcasting.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' => '127.0.0.1',
                'port' => 6001,
                'scheme' => 'https',
                'curl_options' => [
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ]
            ],
        ],

config/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' => false,
            'enable_statistics' => true,
        ],
    ],

    'app_provider' => BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider::class,

    'allowed_origins' => [
        //
    ],

    'max_request_size_in_kb' => 250,

    'path' => 'laravel-websockets',

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

        'interval_in_seconds' => 60,

        'delete_statistics_older_than_days' => 60,
    ],

    'ssl' => [
        'local_cert' => '/Users/ronflorax/.config/valet/Certificates/snookerscores.test.crt',

        'local_pk' => '/Users/ronflorax/.config/valet/Certificates/snookerscores.test.key',

        'passphrase' => null,

        'verify_peer' => false,
    ],

All 14 comments

Are you seeing any errors in your browser console, like a closed connection or a 404 error? It's possible you just need to update your Echo config to use encrypted connections over wss.

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    wsHost: window.location.hostname,
    wsPort: 6001,
    wssPort: 6001,
    encrypted: true,
    disableStats: true
});

Also, in broadcasting.php, add 'encrypted' => true to your pusher config options.

you don't need to add encrypted => true to your broadcasting.php

however you do need to add wssPort: 6001 in your bootstrap.js and then rebuild the app using npm run prod

had the same problem yesterday and it's working now. This is all explained here too https://docs.beyondco.de/laravel-websockets/1.0/basic-usage/ssl.html#usage-with-laravel-valet

Thanks for the tips, however neither work, as it's actually going wrong before consuming the events, it goes wrong when dispatching them from an artisan command, so no javascript involved yet. I tried adding 'encrypted' => true either way and it made no difference.

I am having the same issue using Laravel Valet. Dumping the $response from the broadcast()-method in Illuminate\Broadcasting\Broadcasters\PusherBroadcast returns this:

["body" => false, "status" => 0]

Dumping the actual $this->pusher-object returns this:

Pusher {#692 -crypto: null -settings: array:13 [ "scheme" => "https" "port" => 6001 "timeout" => 30 "debug" => false "curl_options" => [] "encryption_master_key" => "" "auth_key" => "馃懟" "secret" => "馃挬" "app_id" => "612626" "base_path" => "/apps/612626" "notification_host" => "nativepush-cluster1.pusher.com" "notification_scheme" => "https" "host" => "127.0.0.1" ] -ch: curl resource @14 url: "https://127.0.0.1:6001/apps/612626/events?auth_key=馃懟&auth_signature=fbe9e1974ccb7441f68c651018f9d5904ff226d96a41360f67392bc20f088c67&auth_timestamp=1544372635&auth_version=1.0&body_md5=bcfbdf58c64c6e17e8966e114d2e7fd8" content_type: null http_code: 0 header_size: 0 request_size: 0 filetime: -1 ssl_verify_result: 20 redirect_count: 0 total_time: 0.043775 namelookup_time: 0.017807 connect_time: 0.018154 pretransfer_time: 0.0 size_upload: 0.0 size_download: 0.0 speed_download: 0.0 speed_upload: 0.0 download_content_length: -1.0 upload_content_length: -1.0 starttransfer_time: 0.0 redirect_time: 0.0 redirect_url: "" primary_ip: "127.0.0.1" certinfo: [] primary_port: 6001 local_ip: "127.0.0.1" local_port: 50593 } #logger: null }

I tried every possible means to follow the docs with regards to SSL but just couldn't get it to work. However, this is how I finally got it working with SSL on homestead.

changed boostrap.js to listen on 6002 instead
window.Echo = new Echo({ broadcaster: 'pusher', key: process.env.MIX_PUSHER_APP_KEY, wsHost: process.env.MIX_PUSHER_WEBSOCKET_HOST,//window.location.hostname, wsPort: 6002, wssPort: 6002, disableStats: true, encrypted: true });

added these two keys to my .env file
MIX_PUSHER_WEBSOCKET_HOST="${PUSHER_WEBSOCKET_HOST}"
PUSHER_WEBSOCKET_HOST=socket.websocket-demo.app

note that the above, websocket host, is actually a subdomain,

'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'), 'encrypted' => true, 'host' => env('PUSHER_WEBSOCKET_HOST','127.0.0.1'), 'port' => 6002, 'scheme' => 'https', 'curl_options' => [ CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0, ] ], ],
in broadcasting.php

and then finally using the sample nginx config found at https://docs.beyondco.de/laravel-websockets/1.0/basic-usage/ssl.html#usage-with-a-reverse-proxy-like-nginx

except, adding the line in the config to listen on 6002

listen 443 ssl; listen [::]:443 ssl; listen 6002;

and that's just about it

Why is this issue closed? 馃槼

I was thinking the answer of @anabeto93 solved the issue. Reopended.

@ronfloraxCT @Livijn

I was having trouble getting this to work with Laravel Valet and now everything works fine. The demo works fine and I also have it working with a standalone websocket app that is now handling web sockets for 5 of my apps (this is all on my macbook dev using Laravel Valet with --secure

My thread here might prove useful https://github.com/beyondcode/laravel-websockets/issues/8

I suggest you take a step back and try to get the demo working with Laravel Valet first. Once you have that working then you can try to get your own apps working.

Also I think @anabeto93's response has nothing to do with Valet.. correct me if I'm wrong but when you use Homestead you don't use Valet at all.

Adding this solved it for me!

'curl_options' => [
    CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_SSL_VERIFYPEER => 0,
]

@vesper8 Unfortunately that thread doesn't help me, because most of it concerns Laravel Echo, and my problem occurs before that. The one post in it which does show the same error as me is somehow resolved by using the same settings I have been using from the start, which is weird. I copy-pasted them to be sure. So in short, I'm still stuck with this!

Turns out it does work if the server is running, but if it's not running it fails as described in the original post. I think it's quite misleading that it fails without any error to be honest! I don't have the server running permanently in my test environment, but it seems I'll have to keep an eye on this. Closing this for now.

@ronfloraxCT ah. Yes. The server does need to be running to be able to broadcast any events since the events are broadcasted over the http(s) API.

You will also get a exception if Pusher goes down for example since the broadcast API request fails.

You could for example add a check around each broadcast, for example if (env('ENABLE_BROADCASTS', true)) and add ENABLE_BROADCASTS=false to your .env locally to quickly disable broadcasting for your local environment.

Try this in your config/websockets.php

'ssl' => [

    'local_cert' => '/Users/YOUR-USERNAME/.valet/Certificates/VALET-SITE.TLD.crt',

    'local_pk' => '/Users/YOUR-USERNAME/.valet/Certificates/VALET-SITE.TLD.key',

    'passphrase' => null,

    'verify_peer' => false

]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

NIkita-Kim picture NIkita-Kim  路  4Comments

semsphy picture semsphy  路  3Comments

ahmed-aliraqi picture ahmed-aliraqi  路  4Comments

fridzema picture fridzema  路  4Comments

rikless picture rikless  路  4Comments