Event Listeners – What Every Developer Should Know

aman goyal
3 min readMar 21, 2024

--

An event consumer in a component that actively listens for some messages or events and process them any way we want to handle it. An event can be — when a T-shirt order is placed on amazon and amazon uses that event to send email back to user to let share some details about the order.

Event listener in general has main three components.

1. Event Dispatcher

Upon receiving an event from a producer, the event dispatcher dispatches it to all relevant handlers. When an event occurs event dispatcher checks for all the event-handlers registered with it and then the event is passed to the event-handlers which are responsible for taking care of that event type.

Example:

Flowchart detailing the event dispatching process for order creation and cancellation, leading to user and delivery handler.
  • Order Create Event & Order Cancelled Event: These are the initial triggers in the system. When an order is created or cancelled, these events are generated.
  • Event Dispatcher: This is the central mechanism that processes the incoming events. It acts as a router, directing the events to the appropriate handlers.
  • Notify Users: This step indicates that when either an order is created or cancelled, a notification is sent out to users. This could be an email, SMS, or any other form of user notification.
  • Notify Delivery: Similarly, the delivery system is also notified when these events occur. This could involve updating delivery schedules, informing delivery personnel, or adjusting logistics planning.

2. Event Handler

Event handlers are responsible for informing event dispatcher (normally at the time of application start) the type of event it can handle. Generally, each event handler typically handles a specific type of event. Each event handler is a event processor which then actually do something about that event.

Diagram illustrating separate event handling processes for users and delivery, with ‘User Handler’ processing Event-1 & Event-2 for user notifications, and ‘Delivery Handler’ processing ‘Event-2’ for delivery updates.

3. Event Processor

Event processors receive events from event handlers. Once an event is received, they are responsible for performing business logic or carrying out specific tasks in response to events. This may involve interacting with databases, invoking external services, updating system state, or triggering further actions within the system.

Flowchart outlining the email notification process for order creation and cancellation and informing the delivery team about order cancellation.

In conclusion, an high level event listener architecture looks like this.

Event Listener Architecture

Conclusion

Event consumer architecture as simple as it can get. It has three main component Dispatcher, Handler and Processor. They are core to event driven architecture.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

aman goyal
aman goyal

Written by aman goyal

Aman is Software Engineer. I write for fun or when I learn something interesting.

No responses yet

Write a response