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

NoobDev0410's avatar

Events vs Jobs Clarifications

Good Day

I have had some trouble deciding when to go for a job or an events in some situations. I understand mainly jobs will be running in the background while events also can be queued if needed. and to call an event some action needs to happen such as like "PostSubmited". Also is it applicable to dispatch jobs inside event listeners? .Is there any guideline to refer when deciding to select between jobs or events? I hope my confusion could be clarified. Would it be bad to have a job perform multiple operations ?

Thank you and have a good day.

2 likes
10 replies
vincent15000's avatar
Level 63

I was exactly in the same situation some years ago.

Imagine you have an application in which the admin can create new users. And when a user is created you want that the application sends a notification by email to the created user.

The store method is designed to store the new user, but it is not designed to send a notification. In this case you can create an event or use a built in event (via an observer) to send the notification via a listener.

The listener is a class where a function is executed when the event is dispatched.

If the function executed to send the notification is long to execute, the idea is to put the function to be execute in a queue (via a job) to be executed in the background.

So you can use at the same time an event and a job.

1 like
NoobDev0410's avatar

@vincent15000 Thanks for this so its fine in using a jobs inside listeners then?

Another thing i was thinking is for one notification by email . replacing the event by the job would it do the work. while if the side effects get increased switching to the event listener flow. does it make any sense?

1 like
vincent15000's avatar

@NoobDev0410 Yes you can replace the event by the job.

But like I said if you have a store function to store a new user, it's designed only for storing a new user.

Sure you can dispatch a job directly from the store function, but it wouldn't respect some rule about how to write a function : one function => one action.

So it's better to use an event and export the additional code inside a listener rather than overloading the store method.

1 like
NoobDev0410's avatar

@vincent15000 I agree. i have an action where that happens and in there i dispatched a job. i must think to shift to an event if have to deal with multiple effects.( i could use it in the controller in that case). i hope it would be valid.

thanks

1 like
vincent15000's avatar

@NoobDev0410 In reality, if the function is very short in the controller, you can dispatch the job from the controller, it's not a problem.

Dividing the code into several classes (events, listeners, jobs, services, ...) is useful when you have very big functions in your controllers.

martinbean's avatar

@noobdev0410 Events and jobs are not interchangeable. They serve different purposes.

Events say something happened, i.e. a post was created.

Jobs are for encapsulating business logic to (usually) be ran asynchronously because it may take a long time, may interact with external services, etc and is something you don’t want/need the user to wait for that logic to complete before returning a response to them. So jobs are good for things like sending emails, syncing data with a third party, processing images, etc.

1 like
NoobDev0410's avatar

@martinbean Thanks Sir. for the answer. Since events have listeners we intend to perform what happen after this happens like that. and if sending mails generate csv or notifying users is also a part of that sequence. it will become a listeners that would subscribed to the event. then if there is also some long task that would happen then making the event queueble would be better than calling jobs inside the listeners of that long task? (sorry i'm kind of new to this area). furthermore are jobs specifically designed to handle only type of task for example if we have send mail ,notify user could that both go inside one job?

Have good day

Please or to participate in this conversation.