The What
The EventBus allows communication between components without requiring the components to explicitly register with one another (promoting loose coupling).
The Why
Large systems need to be loosely coupled and should adhere tp separation of concerns to increase the maintainability of the system. The idea of an event bus is largely inspired by networking. You have a pipeline of “things” that get dispatched an event. They decide if they care (consume) about it or not.
Terminology
Subscribing - Registering a listener for a specific event/topic
Listener - An object that cares about events and exposes handler methods
Handling - The act of doing something with a registered event
Dispatching - Making the event available and sending it to subscribers
Sample Code
Event Dispatcher - https://gist.github.com/geggleto/28a36199df22d266cead4c5d78f32789
Conclusion
The Event Bus is a great tool for promoting loose coupling in a system allowing you to better document in code interdependencies between your objects. In my opinion it’s a founding component of good Domain Driven Design principles and also is a core infrastructure component to Event Sourcing.