Lets learn how to queue event listeners in Laravel 11 easily. Check examples and available queue configurations.
Why you should queue event listeners in Laravel?
Imagine that your Laravel project has hundreds of events fired within a minute or less that has database transactions, etc. If you do not queue your event listeners it will break the server or database for executing all the transactions at once. In order to save server and database execute event listeners one after another like a queue.
How to Queue Event Listeners
Just implement ShouldQueue interface class on a event listener not on event class. Below shows step by step in creating a queued event listener including event and listener creation.
Step 1: Create a Event Class
Create a sample event class using artisan command like below.
php artisan make:event CommentPosted
Step 2: Create a Event Listener class
Use below artisan command to a sample event listener.
php artisan make:listener NotifyOwner
Step 3: Queue Event Listener
Open recently created event listener and implement ShouldQueue interface add that event listener to queue.
<?php
namespace App\Listeners;
use App\Events\CommentPosted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class NotifyOwner implements ShouldQueue
{
/**
* Handle the event.
*/
public function handle(CommentPosted $event): void
{
echo "comment posted event running";
}
}
Step 4: Run event listener
<?php
use App\Events\CommentPosted;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
CommentPosted::dispatch();
});
Step 5: check queues
You can listen to all queues using below artisan command.
First run your Laravel application. Then run below artisan command in your terminal to view queues in your Laravel application.
php artisan queue:work
You will see running and completed queues in your terminal like below example.
2024-11-25 04:34:39 App\Listeners\NotifyOwner ......................................................................... RUNNING
comment posted event running 2024-11-25 04:34:39 App\Listeners\NotifyOwner .......................................................
Change used Queue connection for event listener
Use connection public variable of the shouldQueue interface change the queue connection easily.
<?php
namespace App\Listeners;
use App\Events\CommentPosted;
use Illuminate\Contracts\Queue\ShouldQueue;
class NotifyOwner implements ShouldQueue
{
//The name of the connection the job should be sent to.
public $connection = 'sqs';
/**
* Handle the event.
*/
public function handle(CommentPosted $event): void
{
echo "comment posted event running";
}
}
Change used Queue in event Listener
If you have many named queued and want to send this event listener to one of the named queued. You can use $queue variable.
<?php
namespace App\Listeners;
use App\Events\CommentPosted;
use Illuminate\Contracts\Queue\ShouldQueue;
class NotifyOwner implements ShouldQueue
{
//The name of the queue the job should be sent to.
public $queue = 'listeners';
/**
* Handle the event.
*/
public function handle(CommentPosted $event): void
{
echo "comment posted event running";
}
}
How to delay executing a event listener
To delay the event listener use $delay public variable of the shouldQueue interface.
<?php
namespace App\Listeners;
use App\Events\CommentPosted;
use Illuminate\Contracts\Queue\ShouldQueue;
class NotifyOwner implements ShouldQueue
{
//The time (seconds) before the job should be processed.
public $delay = 60;
/**
* Handle the event.
*/
public function handle(CommentPosted $event): void
{
echo "comment posted event running";
}
}