Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

highnoon's avatar

Laravel Events

Hi

In my mind eventing is a mechanism to decouple the main code and start a 'seperate' branch of code to be handled in the listener of the invoked event. This way the main code can continue its flow without being hold up by the code in the event. Right?

When I include a sleep(10) in the listener of the event the main code still waits 10 seconds and then continues the code. Is this how it is supposed to be?

Many thanks

0 likes
3 replies
spekkionu's avatar
Level 46

By default PHP isn't multithreaded or asynchronous and doesn't really have the capability to have your events use a separate process.

There are tools to allow this like the pcntl extension or pthreads or ways of faking it with curl but I haven't seen these used all that much.

If you want that kind of functionality you can use a queued event listener and your listener will use the queue to process rather than just run immediately.

https://laravel.com/docs/5.4/events#queued-event-listeners

1 like
Snapey's avatar

not taking away from @spekkionu good and correct answer, this is what happens when the queue driver is 'sync' it means that the listener is processed in the same request cycle.

Using events allows you to a) queue things that take a long time, b) relocate code to separate classes so that you can maintain single responsibility principle. For instance queueing multiple events that need to occur that are triggered by the original action but are not the primary responsibility of the class.

highnoon's avatar

Thanks, that does help. According to https://laravel.com/docs/5.4/events#queued-event-listeners there are a few db options to choose from: Redis and Beanstalk. Any preferences? Currently Im using Redis in conjunction with sockets. However, if I move my project to Forge Beanstalk might be the better option since Forge adds additional functionality?

Please or to participate in this conversation.