2

Using laravel-websockets package and a very strange thing is happening. I could say all is working but the websockets server seems to receive events one yes one not.

I connect via wss and works fine.

config/broadcasting:

<?php

return [

    'default' => env('BROADCAST_DRIVER', 'null'),
    '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' => env('APP_URL'),
                'port' => env('LARAVEL_WEBSOCKETS_PORT'),
                'scheme' => 'https',
                'curl_options' => [
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ],
            ],
        ],
       
    ],
];

config/websockets.php:

<?php

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

return [

    'dashboard' => [
        'port' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
    ],

    'apps' => [
        [
            'id' => env('PUSHER_APP_ID'),
            'name' => env('APP_NAME'),
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'capacity' => null,
            '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',

    'middleware' => [
        'web',
        Authorize::class,
    ],

    'statistics' => [
        'model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::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),        
        'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null),
        'verify_peer' => false,
    ],

    'channel_manager' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class,
];

.env:

...
BROADCAST_DRIVER=pusher
CACHE_DRIVER=memcached
QUEUE_CONNECTION=sync
SESSION_DRIVER=memcached
SESSION_LIFETIME=120
...
PUSHER_APP_ID=(my app id)
PUSHER_APP_KEY=(my app key)
PUSHER_APP_SECRET=(my app secret)
PUSHER_APP_CLUSTER=eu
LARAVEL_WEBSOCKETS_PORT=6001

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
MIX_PUERTO_WEBSOCKETS="${LARAVEL_WEBSOCKETS_PORT}"

LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT=/etc/letsencrypt/archive/(mydomain)/fullchain1.pem
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK=/etc/letsencrypt/archive/(mydomain)/privkey1.pem

In my web.php I have the route:

Route::get('prueba', function(){
    broadcast(new App\Events\EventPrueba());
});

So whenever I visit mysite/prueba EventPrueba event is broadcasted.

This is my EventPrueba:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use App\MensajeChat;

class EventPrueba implements ShouldBroadcast {

    use Dispatchable,
        InteractsWithSockets,
        SerializesModels;    

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct() {    
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn() {       
        return new Channel('prueba');
    }

}

So event broadcasts over a channel named 'prueba'.

In Debug Dashboard (mysite/laravel-websockets) I can connect to websockets server and I can see all events the server is receiving, and here is the strange thing. When I visit mysite/prueba several times, one event is received, the next is not received, the next is received, the next is not received, and so on...

In fact, if I change my web.php route:

Route::get('prueba', function(){
    broadcast(new App\Events\EventPrueba());
    broadcast(new App\Events\EventPrueba());
    broadcast(new App\Events\EventPrueba());
    broadcast(new App\Events\EventPrueba());
    broadcast(new App\Events\EventPrueba());
    broadcast(new App\Events\EventPrueba());
    broadcast(new App\Events\EventPrueba());
    broadcast(new App\Events\EventPrueba());
});

In Debug Dashboard I see 4 times event received and not 8 times!

I hope you can understand my explanation.

Please help. Thanks

Jaime
  • 328
  • 1
  • 6
  • 19
  • Ok I forgot to tell something important, in fact the reason why one event arrives to the websockets server and the next doesn't. I host this application in a NFS shared storage, two frontal servers and a load balancer. So we have here really two websockets servers. For sure the load balancer is driving one message to a frontal and the next to the other frontal. So, the question is now, does Laravel allow to config an application to work in a load balancer hosting? This is getting interesting!! Thanks – Jaime Jun 26 '20 at 16:44
  • It's seem like you need a queue work for that. – GTHell Jun 27 '20 at 08:34

0 Answers0