23

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('no-reply@sample.com', 'Website Name');
            $message->to('developer@gmail.com')->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

sumit
  • 15,003
  • 12
  • 69
  • 110
  • Which driver are you using for event broadcasting? Redis or pusher? – vijaykumar Dec 30 '16 at 04:52
  • You can achieve this using multiple queue process as I've mentioned in the answer below. – Gayan Jan 06 '17 at 06:39
  • @sumit did you find an answer for this question..? – Gayan Jan 09 '17 at 06:58
  • @sumit to further clarify your question by excluding broadcast event from queue what are you trying to achieve? – Gayan Jan 10 '17 at 03:34
  • I dont want to put the event which broadcast data from GPS device on queue..whenever I implement queue for email , the broadcast event is also queued... – sumit Jan 10 '17 at 03:36

3 Answers3

37

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.

nmfzone
  • 2,755
  • 1
  • 19
  • 32
7

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.

Gayan
  • 3,614
  • 1
  • 27
  • 34
  • Your solution is to seperate out 2 queues , sorry it still didnt helped becuase I need to exclude broadcast event from queue. I realised that all event broadcasting are queued and we should not bother about it . Thanks for your contribution. – sumit Jan 09 '17 at 22:44
1

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';
        }
vijaykumar
  • 4,658
  • 6
  • 37
  • 54