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

ajschmaltz's avatar

Events: Best Practices when you need synchronous events

I would like some opinions on this type of situation:

  • A piece of content is created
  • That content needs an addition from a time consuming task
  • Subscribers need to be alerted of the content creation.

These 3 things MUST happen in the order and the last 2 MUST be triggered in an event.

One way of dealing with this situation is creating a ContentWasCreated event and sticking these handlers in the service provider in the order they need to be executed.

The second way of dealing with this is throwing 2 events. The ContentWasCreated event is fired and triggers the time consuming task. When the task completes, it fires a ContentIsReadyToBeSent which handles the alert.

How would you handle this sort of situation. Many times I've run across synchronously dependent events.

0 likes
5 replies
kfirba's avatar

Your second approach sounds more logical to me only if the third action depends on the second action, otherwise, it's just redundant.

ajschmaltz's avatar

I actually have several tasks that need to complete before the final alert - I'll be more specific, because I'm running into another problem. Where do you call the next event if there's multiple handlers?

First, content is posted and ContentWasCreated is fired.

Second, the piece of content needs to be geocoded & a phone number needs to be reserve via an API.

Lastly, that phone number needs to be used to send alerts to people who are found via the geocode.

So right now I have

ContentWasCreated [
  GeocodeContent,
  ReserveNumber,
]

ContentIsReady[
  SendAlerts
]

But, now, where do you call ContentIsReady since both GeocodeContent and ReserveNumber need to finish before SendAlerts can start? I guess both could check if the other is done, but that seems weird.

kfirba's avatar

@ajschmaltz So you got 2 events that do not depends on each other but another event that does depend on both of them.

Since GeocodeContent and ReserveNumber are not related you can simply prioritize them by doing something like:

Event::listen('ContentWasCreated', ' GeocodeContent'); // maybe even add a third parameter to explicitly indicate the priority
Event::listen('ContentWasCreated', ' ReserveNumber');

At this point it's safe to say that ReserveNumber was called AFTER GeocodeContent was executed:

Listeners with higher priority will be run first, while listeners that have the same priority will be run in order of subscription.

Now simply trigger the ContentIsReady event.

rallyagency's avatar

That priority doesn't appear in Laravel 5 docs - is it still supported?

graham's avatar

Looks like priority is still there:

/**
 * Register an event listener with the dispatcher.
 *
 * @param  string|array  $events
 * @param  mixed   $listener
 * @param  int     $priority
 * @return void
 */
public function listen($events, $listener, $priority = 0)
{
    foreach ((array) $events as $event)
    {
        if (str_contains($event, '*'))
        {
            $this->setupWildcardListen($event, $listener);
        }
        else
        {
            $this->listeners[$event][$priority][] = $this->makeListener($listener);
            unset($this->sorted[$event]);
        }
    }
}
2 likes

Please or to participate in this conversation.