Event Handler
ELI5 — The Vibe Check
An event handler is the code that says 'oh, something happened? Let me react to that.' When an 'OrderPlaced' event fires, the email handler sends a confirmation, the inventory handler reserves stock, and the analytics handler logs it. Everyone reacts independently.
Real Talk
A function or class that processes domain events, performing side effects or state changes in response. Event handlers are typically decoupled from event producers and can be synchronous or asynchronous. Multiple handlers can subscribe to the same event, enabling extensible, loosely-coupled architectures.
Show Me The Code
class OrderPlacedHandler {
async handle(event: OrderPlacedEvent) {
await this.emailService.sendConfirmation(event.userId);
}
}
class InventoryHandler {
async handle(event: OrderPlacedEvent) {
await this.inventory.reserve(event.items);
}
}
eventBus.subscribe('OrderPlaced', [OrderPlacedHandler, InventoryHandler]);
When You'll Hear This
"We added a new analytics event handler without touching the order service at all." / "Event handlers should be idempotent — the same event delivered twice should produce the same result."
Related Terms
Command Bus
A command bus is the postal service for 'please do this' messages.
Event-Driven Architecture
Event-Driven Architecture is like a gossip network. When something happens (order placed!), it broadcasts the news.
Observer Pattern
Think of a newsletter. You (the publisher/subject) publish content. Your subscribers (observers) automatically get notified when new content arrives.
Pub/Sub (Pub/Sub)
Pub/Sub is like a newspaper service. Publishers write articles and drop them off.