Why BroadCastEvent are queued in Laravel? How to stop that?

9.6k views Asked by At

I am working on the project which need to broadcast latitude and longitude on realtime

I have something like below

namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Support\Facades\Redis;

class TrackersBroadcast extends Event implements ShouldBroadcast
{
        public  $lat, $lng,$imei,$date_time

    use SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(
                                    $lat, 
                                    $lng, 
                                    $imei, 
                                    $date_time 

                                )

    {
        $this->lat = $lat;
        $this->lng = $lng;
        $this->imei = $imei;
        $this->date_time = $date_time;

    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['tracker-channel'];
    }


}

In some case I need to trigger real time email , so I decided to implement laravel message queue like below

namespace App\Jobs;

use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Mail;

class SendAlertEmail extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;
    public  $data;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data=$data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        Mail::send('emails.test', ['testVar' => $this->data], function($message) {
            $message->from('[email protected]', 'Website Name');
            $message->to('[email protected]')->subject('A simple test');
        });
    }
}

whenever I run php artisan queue:listen database it will queue my broadcasting event too . I dont want to queue the broadcast event . How to do that?

enter image description here

3

There are 3 answers

3
nmfzone On BEST ANSWER

Because Laravel Event Broadcasting queued by default if you extend ShouldBroadcast interface. If you don't want Event Broadcasting queued, you should extend ShouldBroadcastNow interface.

use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;

class TrackersBroadcast implements ShouldBroadcastNow
{
......
}

So It means your Event Broadcasting will using sync queue driver.

0
vijaykumar On

May be you can mention queue name in broadcast event like this and don't listen that queue incase you don't need to process

//php artisan queue:listen --queue=broadcast-queue

    /**
         * @return string
         */
        public function onQueue()
        {
            return 'broadcast-queue';
        }
1
Gayan On

In Laravel all event broadcasting is queued by default.

Before broadcasting events, you will also need to configure and run a queue listener. All event broadcasting is done via queued jobs so that the response time of your application is not seriously affected.

In you case you gonna need two queue drivers. One for event broadcasting with real time support. One for handling emails with queue support. For this you gonna need two queue drivers. You can define them in config/queue.php

In your SendAlertEmail class do this.

public $broadcastQueue = 'queue-name-for-handling-emails';

In your TrackersBroadcast state the real time queue. This is your redis queue driver.

public $broadcastQueue = 'real-time-queue';

Refer Broadcast queue under Defining broadcast events

Then you can listen for your two queues like this

$php artisan queue:listen --queue="queue-name-for-handling-emails"
$php artisan queue:listen --queue="real-time-queue"

Refer Why event broadcast is queued ?

Hope you find this useful.