Performance: Creating listeners
This affects how quickly your Laravel app responds to events and how much server resources are used during event handling.
Jump into concepts and practice - no test required
<?php
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
class SendWelcomeEmail implements ShouldQueue {
public function handle($event) {
Mail::to($event->user->email)->send(new WelcomeEmail());
}
}<?php
namespace App\Listeners;
use Illuminate\Support\Facades\Mail;
class SendWelcomeEmail {
public function handle($event) {
// Sending email synchronously
Mail::to($event->user->email)->send(new WelcomeEmail());
}
}| Pattern | Server Blocking | Queue Usage | Response Time Impact | Verdict |
|---|---|---|---|---|
| Synchronous Listener | Blocks request until done | No | Increases response time | [X] Bad |
| Queued Listener (ShouldQueue) | Non-blocking | Yes | Minimal impact on response | [OK] Good |
UserRegisteredListener?php artisan make:listener ListenerName.public function handle(UserRegistered $event) {
echo 'Welcome, ' . $event->user->name . '!';
}UserRegistered, which contains the user data.class SendNotification {
public function handle() {
// send notification code
}
}handle method to achieve this?