Reputation: 609
I am developing a app to process videos. I try to push 10 videos into a queue. After a video was processed I fired a event like this:
...
$production->render();
event(new VideoHasProcessed(basename($this->video->path)));
...
And I do following this video to push a notification to users (https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/12)
The problem is: I want to push a notification when each video was processed but in fact, after 10 videos was processed the notification was displayed.
This is my script:
<script>
(function () {
var pusher = new Pusher(PUSHER_ID, {
encrypted: true
});
var channel = pusher.subscribe('test');
channel.bind('App\\Events\\VideoHasProcessed', function(data) {
console.log(data);
});
})();
</script>
Update:
Dispatch a job
foreach ($request->input('files') as $file)
{
...
// Dispatch a job.
$this->dispatch(new ProduceVideo($video));
....
}
And my job class:
public function handle(ProductionRepository $production) { ...
$production->render();
event(new VideoHasProcessed(basename($this->video->path)));
...
}
Upvotes: 3
Views: 3172
Reputation: 61
After digging into laravel 5.3 core I found out the solution. You event class needs to extend ShouldBrodcastNow interface:
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class MyEvent implements ShouldBrodcastNow
{
...
}
In that way if you need the event to be broadcasted right away, this should do the trick.
NOTE: there is not need for MyEvent
to implement both interfaces (ShouldBroadcastNow, ShouldBroadcast)
because the interface ShouldBroadcastNow
already extends ShouldBroadcast
Upvotes: 5
Reputation: 5784
If each of your 10 videos are processed as separate queued jobs you'll just need to fire your event in each queued job. Here's an example handle function for a job class (see https://laravel.com/docs/5.2/queues#job-class-structure):
/**
* Execute the job.
*/
public function handle()
{
// Your video processing script
// Fire your event
event(new VideoHasProcessed(basename($this->video->path)));
}
If your videos are processed as 1 job you'll need to add your event inside any loop you may be using to process each video:
foreach($videos as $video):
// Your video processing script
// Fire your event
event(new VideoHasProcessed(basename($this->video->path)));
endforeach;
Upvotes: 0