Timothy
Timothy

Reputation: 4285

Laravel Broadcast Event not firing

I am trying to broadcast an event every time a new market snapshot is created. I followed the laravel tutorial here.

But it seems I am missing something since even the log is not being created:

    namespace App\Events;

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

    class NewMarketSnapshot 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 Channel|array
         */
        public function broadcastOn()
        {
            \Log::info('event broadcasted');

            return new Channel('snapshot');
        }
    }

I even registered the event in EventServiceProvider (not mentioned on the laravel link)

protected $listen = [
    'App\Events\NewMarketSnapshot' => [
      'App\Listeners\NewMarketSnapshotListener',
    ],
];

The handler is being called but the event is not being broadcasted.

    namespace App\Listeners;

    use App\Events\NewMarketSnapshot;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use \Log;

    class NewMarketSnapshotListener
    {
        /**
         * Create the event listener.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }

        /**
         * Handle the event.
         *
         * @param  NewMarketSnapshot  $event
         * @return void
         */
        public function handle(NewMarketSnapshot $event)
        {
            Log::info('handler called.');
        }
    }

Testing with the simple route:

Route::get('/test', function(){
    $snapshot = ['USD'=>100];
    return event(new App\Events\NewMarketSnapshot($snapshot));
});

Lines in my .env file

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=redis

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

Upvotes: 1

Views: 5095

Answers (2)

hassanrazadev
hassanrazadev

Reputation: 664

I was facing a similar issue, everything was set up correctly even I tested pusher at the start and it was working OK. The problem was, I switch QUEUE_CONNECTION to database to use Laravel's queue system for sending email asynchronously.
So the solution that worked for is to implements ShouldBroadcastNow for my event as
class NewNotificationEvent implements ShouldBroadcastNow and everything working OK again.

Upvotes: 0

Matthew Daly
Matthew Daly

Reputation: 9476

I note that you are using Redis as both the queue and broadcast driver. My experience has been that using it for both breaks the broadcasting (basically it sends the wrong command to Redis on the broadcast event).

If you switch the queue to a different driver or use separate Redis instances it should hopefully work OK.

EDIT: I believe under these circumstances it uses the Redis RPUSH command, when it should use PUBLISH instead, and as such the subscriber never picks it up.

EDIT2: Don't forget to clear any cached config with "php artisan config:clear"

Upvotes: 9

Related Questions