B
B
BonBon Slick2020-12-12 01:47:11
symfony
BonBon Slick, 2020-12-12 01:47:11

When to use Event Dispatcher and when to use Messenger Bus?

https://symfony.com/doc/current/components/event_d...
vs
https://symfony.com/doc/current/components/messeng...
for
https://martinfowler.com/articles/201701- event-dri...

CQRS or true Event Sourcing can be implemented by either package.


What, when and why?

Worth making table

Messenger | event
pros | cons | pros | cons

or make a specific list of comparison criteria so that you can always peep and immediately say what to use, and not remember, but what is under the hood in the EventDispatcher and how it works or Bus.

Sample

Messenger | Event
async: yes | no
custom transport: yes | no

And other parameters that this or that package has, including the general ones.
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-12-12
@BonBonSlick

The purpose of an event dispatcher is to allow one piece of code to "notify" an application that something has happened, and have someone else "listen" to that event and run some code.
Messenger allows handlers to be called asynchronously, while event listeners from an EventDispatcher are always synchronous. Although events can be made asynchronous through Messenger.
EventDispatcher implies the presence of an unlimited number of subscribers or no subscribers at all, and it also returns a result. Messenger has one handler and no processing result is returned.
The most common use of EventDispatcher is for domain events. Whenever you dispatch an event, if you want the listeners of that event to be able to communicate with you so that you can do something based on their feedback, use an EventDispatcher. But if you just want to say "it happened" and you don't need feedback from possible listeners or handlers, use Messenger.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question