Skip to content

Event Handler

Medium — good to knowArchitecture

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.