0
0
Laravelframework~5 mins

Queued listeners in Laravel

Choose your learning style9 modes available
Introduction

Queued listeners let your app handle events in the background. This keeps your app fast and smooth for users.

When sending emails after a user registers, so the user doesn't wait.
When processing large files after upload without slowing the app.
When updating external services after a database change.
When running tasks that take time but don't need instant results.
Syntax
Laravel
use Illuminate\Contracts\Queue\ShouldQueue;

class SendWelcomeEmail implements ShouldQueue
{
    public function handle(UserRegistered $event)
    {
        // Send email logic here
    }
}

Implementing ShouldQueue tells Laravel to run this listener in the background.

You need to set up a queue driver (like database or Redis) for queued listeners to work.

Examples
This listener will run in the background when a report is generated.
Laravel
use Illuminate\Contracts\Queue\ShouldQueue;

class ProcessReport implements ShouldQueue
{
    public function handle(ReportGenerated $event)
    {
        // Process report in background
    }
}
This listener runs right away because it does not implement ShouldQueue.
Laravel
class LogUserLogin
{
    public function handle(UserLoggedIn $event)
    {
        // This listener runs immediately (not queued)
    }
}
Queued listener for sending notifications without blocking the app.
Laravel
use Illuminate\Contracts\Queue\ShouldQueue;

class SendNotification implements ShouldQueue
{
    public function handle(NotificationCreated $event)
    {
        // Send notification asynchronously
    }
}
Sample Program

This example shows a queued listener class that handles a UserRegistered event. It logs sending a welcome email. In a real app, Laravel would queue this listener to run in the background.

Laravel
<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use App\Events\UserRegistered;
use Illuminate\Support\Facades\Log;

class SendWelcomeEmail implements ShouldQueue
{
    public function handle(UserRegistered $event)
    {
        Log::info('Sending welcome email to: ' . $event->user->email);
        // Imagine sending email here
    }
}

// Event class
namespace App\Events;

class UserRegistered
{
    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }
}

// Simulate dispatching event and listener
$user = (object) ['email' => 'user@example.com'];
$event = new UserRegistered($user);
$listener = new \App\Listeners\SendWelcomeEmail();

// Normally Laravel queues this, but here we call directly for demo
$listener->handle($event);

// Output is logged, so we simulate output here
echo "Sending welcome email to: user@example.com\n";
OutputSuccess
Important Notes

Queued listeners run asynchronously, so they don't slow down user actions.

Make sure your queue system is configured and running (like database or Redis queues).

Common mistake: forgetting to implement ShouldQueue means the listener runs immediately.

Summary

Queued listeners help run tasks in the background.

Implement ShouldQueue to make a listener queued.

Use queued listeners for slow tasks like sending emails or processing files.