Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 804
sumit Avatar asked Dec 30 '16 04:12

sumit


1 Answers

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.

like image 50
nmfzone Avatar answered Oct 09 '22 03:10

nmfzone