btanner
btanner

Reputation: 93

Laravel broadcastOn() method in event never fires

I am new to laravel and to websockets. I have got my websockets working on the laravel-websockets dashboard, and now am trying to trigger a websocket event with this javascript command:

axios.post('updatequeue', {queue_position: newPos});

newPos is a number.

This is my controller method:

public function updateQueue(Request $request){
        $queueposition = $request->input('queue_position');
        event(new QueueUpdate($queueposition));
}

This is my Event:

class QueueUpdate implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $queue_position;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($queue_position)
    {
        $this->queue_position = $queue_position;

    }

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

When I watch for events in the dashboard, nothing shows up. I get a 200 response from the axios request. I have placed logs throughout, and my events __construct method is called, but broadcastOn() is not. I am really stuck here, if anyone has any ideas, I would be very grateful.

EDIT

here is my broadcasting.php:

<?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'),
                'host'          => env('WEBSOCKET_BROADCAST_HOST'),
                'port'          => env('WEBSOCKET_BROADCAST_PORT'),
                'scheme'        => env('WEBSOCKET_SCHEME'),
                'encrypted'     => env('WEBSOCKET_ENCRYPTED'),
            ],
        ],

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

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

        'null' => [
            'driver' => 'null',
        ],

    ],

];

and 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'),
            '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' => env('LARAVEL_WEBSOCKETS_SSL_VERIFY_PEER', true),
    ],


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

EDIT

I am local. Here are the values for broadcasting.php:

'driver' => 'pusher',
            'key' => portalkey,
            'secret' => secret,
            'app_id' => portalID,
                'cluster'       => portalcluster,
                'host'          => 127.0.0.1,
                'port'          => 6001,
                'scheme'        => http,
                'encrypted'     => false,

Upvotes: 3

Views: 2519

Answers (4)

Agu Dondo
Agu Dondo

Reputation: 13569

Laravel uses queues for broadcasting, you need to have your queues running for events to broadcast

Upvotes: 0

Titanium
Titanium

Reputation: 51

For Kindred spirits that may land on this issue. I had a similar experience.

The broadcastOn() method was not being called, but the event completes without any errors, I validated this by placing logger before the return statement (the one that returns the channel).

My solution (as at the time of written this) was implementing the ShouldBroadCastNow interface instead of ShouldBroadCast and ensuring that the calling class was not in a queue.

So running it synchronously works.

It feels a bit weird that being in a queue was causing it to fail silently but there's that.

Upvotes: 2

Tharindu Thisarasinghe
Tharindu Thisarasinghe

Reputation: 3998

You should set the QUEUE_CONNECTION=sync in your .env file.

This will make the broadcast work and fix your problem.

Upvotes: 0

Uzair Riaz
Uzair Riaz

Reputation: 919

If you can connect to the laravel-websockets dashboard but the events won't show up, chances are your /laravel-websockets/auth request is failing due to csrf token. Try adding laravel-websockets to the $except variable in VerifyCsrfToken middleware.

Upvotes: 1

Related Questions