Performance: Queued listeners
Queued listeners affect how event handling impacts user response time and server load by deferring work to background jobs.
Jump into concepts and practice - no test required
use IlluminateContractsQueueShouldQueue; class SomeListener implements ShouldQueue { public function handle(SomeEvent $event) { // heavy processing here sleep(5); // simulate long task } } Event::listen(SomeEvent::class, SomeListener::class);
Event::listen(SomeEvent::class, function ($event) { // heavy processing here sleep(5); // simulate long task });
| Pattern | Server Blocking | Response Delay | User Interaction Impact | Verdict |
|---|---|---|---|---|
| Synchronous listener | Blocks server during event | Delays response by event duration | High INP due to slow response | [X] Bad |
| Queued listener | No blocking on main request | Immediate response, event handled later | Low INP, better user experience | [OK] Good |
queued listeners in Laravel?ShouldQueue interface to run them asynchronously.ShouldBroadcast is for broadcasting events, ShouldCache and ShouldLog do not exist in this context.use Illuminate\Contracts\Queue\ShouldQueue;
class SendReport implements ShouldQueue
{
public function handle($event)
{
// send report logic
}
}ShouldQueue, so Laravel queues it for background processing.use Illuminate\Contracts\Queue\ShouldQueue;
class ProcessData implements ShouldQueue
{
public function handle($event)
{
// process data
}
public function queue()
{
// queue logic
}
}ShouldQueue. Defining a queue method is unnecessary and ignored.handle is not static, and ShouldBroadcast is unrelated.queue method; Laravel handles queuing automatically -> Option BShouldQueue and sends the email in the handle method runs asynchronously in the background. Sending email directly in the controller, using a synchronous listener, or placing code in the model constructor would delay the response or violate design principles.ShouldQueue and sends the email in the handle method -> Option A