Laravel-websockets: Error when using https (encrypted)

Created on 7 Jun 2019  路  3Comments  路  Source: beyondcode/laravel-websockets

When I use http unencrypted all things works fine
but after using https the connection opens but when broadcast(Event) it gives me error

{message: "", exception: "Illuminate\Broadcasting\BroadcastException",鈥 exception: "Illuminate\Broadcasting\BroadcastException" file: 
"/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php" line: 117 message: "" trace: [{,鈥, {function: "handle", class: 
"Illuminate\Broadcasting\BroadcastEvent", type: "->"},
 {,鈥, {,鈥,鈥 0: {,鈥
 class: "Illuminate\Broadcasting\Broadcasters\PusherBroadcaster" file: 
"/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php" function: "broadcast" line: 48 type: "->" 1: {function: "handle", class: 
"Illuminate\Broadcasting\BroadcastEvent", type: "->"} 2: {,鈥 file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php" function: "call_user_func_array" line: 29 3: {,鈥 4: {,鈥 5: {,鈥 6: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php",鈥 7: {,鈥 8: {,鈥 9: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php",鈥 10: {,鈥 11: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php",鈥 12: {,鈥 13: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Queue/Queue.php",鈥 14: {,鈥 15: {,鈥 16: {,鈥 17: {,鈥 18: {,鈥 19: {file: "/Applications/MAMP/htdocs/saafca/app/Http/Controllers/users/CheckoutController.php", line: 37,鈥 20: {function: "addCredit", class: "App\Http\Controllers\users\CheckoutController", type: "->"} 21: {,鈥 22: {,鈥 23: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Routing/Route.php",鈥 24: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Routing/Route.php",鈥 25: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Routing/Router.php",鈥 26: {,鈥 27: {,鈥 28: {,鈥 29: {,鈥 30: {,鈥 31: {,鈥 32: {,鈥 33: {,鈥 34: {,鈥 35: {,鈥 36: {,鈥 37: {,鈥 38: {,鈥 39: {,鈥 40: {,鈥 41: {,鈥 42: {,鈥 43: {,鈥 44: {,鈥 45: {,鈥 46: {,鈥 47: {,鈥 48: {,鈥 49: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Routing/Router.php",鈥 50: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Routing/Router.php",鈥 51: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Routing/Router.php",鈥 52: {file: "/Applications/MAMP/htdocs/saafca/vendor/laravel/framework/src/Illuminate/Routing/Router.php",鈥 53: {,鈥 54: {,鈥 55: {file: "/Applications/MAMP/htdocs/saafca/vendor/fideloper/proxy/src/TrustProxies.php", line: 57,鈥 56: {,鈥 57: {,鈥 58: {,鈥 59: {,鈥 60: {,鈥 61: {,鈥 62: {,鈥 63: {,鈥 64: {,鈥 65: {,鈥 66: {,鈥 67: {,鈥 68: {,鈥 69: {,鈥 70: {,鈥 71: {,鈥 72: {,鈥 73: {file: "/Applications/MAMP/htdocs/saafca/public/index.php", line: 55, function: "handle",鈥 class: "Illuminate\Foundation\Http\Kernel" file: "/Applications/MAMP/htdocs/saafca/public/index.php" function: "handle" line: 55 type: "->" Name

My bootstrap.js

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


});

websocket.php

<?php

use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize;

return [

    /*
     * This package comes with multi tenancy out of the box. Here you can
     * configure the different apps that can use the webSockets server.
     *
     * Optionally you can disable client events so clients cannot send
     * messages to each other via the webSockets.
     */
    '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,
        ],
    ],


    /*
     * This class is responsible for finding the apps. The default provider
     * will use the apps defined in this config file.
     *
     * You can create a custom provider by implementing the
     * `AppProvider` interface.
     */
    'app_provider' => BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider::class,

    /*
     * This array contains the hosts of which you want to allow incoming requests.
     * Leave this empty if you want to accept requests from all hosts.
     */
    'allowed_origins' => [
        //
    ],

    /*
     * The maximum request size in kilobytes that is allowed for an incoming WebSocket request.
     */
    'max_request_size_in_kb' => 250,

    /*
     * This path will be used to register the necessary routes for the package.
     */
    'path' => 'laravel-websockets',

    /*
     * Dashboard Routes Middleware
     *
     * These middleware will be assigned to every dashboard route, giving you
     * the chance to add your own middleware to this list or change any of
     * the existing middleware. Or, you can simply stick with this list.
     */
    'middleware' => [
        'web',
        Authorize::class,
    ],

    'statistics' => [
        /*
         * This model will be used to store the statistics of the WebSocketsServer.
         * The only requirement is that the model should extend
         * `WebSocketsStatisticsEntry` provided by this package.
         */
        'model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class,

        /*
         * Here you can specify the interval in seconds at which statistics should be logged.
         */
        'interval_in_seconds' => 60,

        /*
         * When the clean-command is executed, all recorded statistics older than
         * the number of days specified here will be deleted.
         */
        'delete_statistics_older_than_days' => 60,

        /*
         * Use an DNS resolver to make the requests to the statistics logger
         * default is to resolve everything to 127.0.0.1.
         */
        'perform_dns_lookup' => false,
    ],

    /*
     * Define the optional SSL context for your WebSocket connections.
     * You can see all available options at: http://php.net/manual/en/context.ssl.php
     */
    'ssl' => [
        /*
         * Path to local certificate file on filesystem. It must be a PEM encoded file which
         * contains your certificate and private key. It can optionally contain the
         * certificate chain of issuers. The private key also may be contained
         * in a separate file specified by local_pk.
         */
        'local_cert' =>  "/Users/sasuke/ssl/server.crt",

        /*
         * Path to local private key file on filesystem in case of separate files for
         * certificate (local_cert) and private key.
         */
        'local_pk' =>  "/Users/sasuke/ssl/server.key",

        /*
         * Passphrase for your local_cert file.
         */
        'passphrase' => null,
        'verify_peer'=>false
    ],

    /*
     * Channel Manager
     * This class handles how channel persistence is handled.
     * By default, persistence is stored in an array by the running webserver.
     * The only requirement is that the class should implement
     * `ChannelManager` interface provided by this package.
     */
    'channel_manager' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class,
];

broadcasting.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Broadcaster
    |--------------------------------------------------------------------------
    |
    | This option controls the default broadcaster that will be used by the
    | framework when an event needs to be broadcast. You may set this to
    | any of the connections defined in the "connections" array below.
    |
    | Supported: "pusher", "redis", "log", "null"
    |
    */

    'default' => env('BROADCAST_DRIVER', 'null'),

    /*
    |--------------------------------------------------------------------------
    | Broadcast Connections
    |--------------------------------------------------------------------------
    |
    | Here you may define all of the broadcast connections that will be used
    | to broadcast events to other systems or over websockets. Samples of
    | each available type of connection are provided inside this array.
    |
    */

    'connections' => [

        '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' => 'localhost',
                'port' => 6001,
                'scheme' => 'https'
            ],
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],

        'log' => [
            'driver' => 'log',
        ],

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

    ],

];

All 3 comments

try encrypted: false

Is it a self-signed cert? If so, that won't work out of the box, because cURL can't trust the certificate provided by laravel-websockets over HTTPS.

Instead, I recommend using https://github.com/FiloSottile/mkcert to make yourself a local CA and add it to your trust store, for development. In prod, you'll need to use a real cert. You can use Caddy and/or Let's Encrypt to issue one.

Add some CURLOPT options:

'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' => env('PUSHER_APP_SCHEME', 'http'),
        'encrypted' => true,
        'curl_options' => [
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ],
    ],
],
Was this page helpful?
0 / 5 - 0 ratings