Event systems solve a fundamental problem in software design: how do components communicate without knowing about each other? Instead of Component A calling Component B directly (tight coupling), A emits an event and B listens — they never reference each other directly.
This decoupling pattern is foundational to JavaScript:
- DOM events —
addEventListeneris a pub/sub system built into every browser - Node.js EventEmitter — every stream, server, and process inherits from it
- React synthetic events — normalized event system delegated to the document root
- Redux — actions are events; reducers are subscribers; the store is the bus
- RxJS — Observables are event streams with composable operators
- WebSockets — server pushes named events; client subscribes per event type
Pub/Sub vs Observer — the key difference
Pub/Sub — fire-and-forget via an intermediary bus. Publisher emits to the bus; subscriber registers with the bus. Neither party holds a direct reference to the other. Zero coupling.
Observer Pattern — the observable subject maintains a direct list of observer objects and calls them on state change. Observers must hold a reference to the subject to subscribe. Slightly more coupled.
Real-world use cases
- Notification systems — one user action broadcasts to toast, analytics, and audit log simultaneously without any of them knowing about the others
- Cross-component communication — sidebar updates when main content changes, no prop drilling, no shared parent state
- Plugin architectures — core emits lifecycle events; plugins hook in without modifying core source
- Real-time features — WebSocket messages dispatched as named events to UI subscribers across the app