Kiyarash
Kiyarash

Reputation: 2578

Laravel beyondcode websockets do not connect

I'm using this Laravel websockets package to have my own websocket server.

As mentioned in package documentation, I have this configuration:

.env setting:

PUSHER_APP_ID=761772
PUSHER_APP_KEY=qwerty
PUSHER_APP_SECRET=secret
PUSHER_APP_CLUSTER=ap2

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'),
            'encrypted' => true,
            //'host' => '105.208.174.8', <--I did test this too
            'host' => '127.0.0.1',
            'port' => 6001,
            'scheme' => 'https'//<--Tested with http
        ],
    ],

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,
    ],
],

bootstrap.js:

/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'qwerty',
    wsHost: window.location.hostname,
    wsPort: 6001,
    disableStats: true,
    encrypted: true,
    enabledTransports: ['ws', 'wss'] //This was added from issue 86

});

This is issue number 86 in package repository

I'm using letsencrypt with my directadmin control panel and this is my SSL part of websockets.php configuration:

    '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' => null,
    'local_cert' => '/home/myDomain/domains/myDomain/public_html/vendor/react/socket/examples/localhost.pem',
      //'local_cert' => '/usr/local/directadmin/data/users/myDomain/domains/myDomain.ir.cert',
    /*
     * Path to local private key file on filesystem in case of separate files for
     * certificate (local_cert) and private key.
     */
    //'local_pk' => null,
    'local_pk' => '/usr/local/directadmin/data/users/myDomain/domains/myDomain.ir.key',

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

But when I use php artisan websockets:serve, It seems there's something wrong about connection and the myDomain.com/laravel-websockets admin panel says:

Channel's current state is unavailable

and the console says:

Firefox can’t establish a connection to the server at wss://myDomain.ir:6001/app/qwerty?protocol=7&client=js&version=4.3.1&flash=false.

pusher.min.js:8:6335 The connection to wss://myDomain.ir:6001/app/qwerty?protocol=7&client=js&version=4.3.1&flash=false was interrupted while the page was loading.

Does anyone know what's my issue and how should I solve this?

Upvotes: 11

Views: 15670

Answers (6)

PHANTOM-X
PHANTOM-X

Reputation: 586

I was struggling with 500 Error till I figured it out

Error message: Attempt to read property "key" on null

  authEndpoint: `http://localhost:3060/laravel-websockets/auth`,
  auth: {
    headers: {
      'x-app-id': '**App ID**', // => my issue was HERE
      Authorization: 'Bearer  jwt TOKEN',
      'Access-Control-Allow-Origin': '*'
    }
  }

and I found it by looking into the file in vendor\beyondcode\laravel-websockets\src\Dashboard\Http\Controllers\AuthenticateDashboard.php

$app = App::findById($request->header('x-app-id'));

$broadcaster = new PusherBroadcaster(new Pusher(
  $app->key,
  $app->secret,
  $app->id,
  []
));

and changed the middlware in App\Providers\BroadcastServiceProvider.php

// from 
// Broadcast::routes();
// to
Broadcast::routes(['middleware' => ['auth:api']]);

another change in App\config\websockets.php

'middleware' => [
  'api', // => changed it from web to api
   Authorize::class,
],

enter image description here

hope this help :)

Upvotes: 0

james albert
james albert

Reputation: 17

my be you have not run server

Upvotes: 0

Jovanni G
Jovanni G

Reputation: 331

If you are using Laravel < 8.0, try composer require pusher/pusher-php-server ^4.1.

Upvotes: 0

Jack
Jack

Reputation: 31

I had the same issue first you have to make sure that port you are trying to run is open or not. if you are running socket server on shared hosting then its not possible or you can talk with customer support and they will open port for you. if you have vps or dedicated server you can open port from your control panel. first try to run without ssl if its running fine it means that your port is open then config your ssl config.

Upvotes: 0

mchev
mchev

Reputation: 725

I had the same issue and the problem was the read access of the local_cert and local_pk.

You can use sudo php artisan websocket:serve to try if this is the issue.

If it's the case, add read access to the files or use a user with the access in /etc/supervisor/conf.d/websockets.conf

Upvotes: 3

CodeFreak
CodeFreak

Reputation: 1

I had the same problem, I was using Homestead, are you on homestead, if so you should be using supervisor(Debian based) or supervisord (REDHat based) OS to run the laravel socket without manually entering the command "php artisan websockets:serve" at all times.

You can refer to this documentation. https://docs.beyondco.de/laravel-websockets/1.0/basic-usage/starting.html#restricting-the-listening-host

For SSL I also followed this documentation with Valet: https://docs.beyondco.de/laravel-websockets/1.0/basic-usage/ssl.html#usage-with-laravel-valet

Upvotes: 0

Related Questions