Cross-Application State Sharing and Event Buses
Cross-Application State Sharing and Event Buses
In a micro-frontend architecture, different micro-applications must frequently communicate: an Authentication Remote emits a login event, the Cart Remote notifies the Header Host to update badge counters, or the Navigation Remote triggers route transitions. However, tightly coupling micro-frontends to a single shared Redux store defeats the autonomy of independent teams. Enterprise systems use Decoupled Event Buses and native browser CustomEvent primitives for cross-application communication.
1. Architectural Communication Paradigms
Three primary approaches:
- 1Window CustomEvent (Recommended): Native browser events with zero dependencies; completely framework-agnostic.
- 2Custom Event Bus Singleton: A lightweight pub/sub emitter shared via Module Federation.
- 3Reactive Observable Stores: Shared lightweight stores (e.g. Zustand or RxJS Subject) passed via props or federation context.
2. Implementing a Type-Safe Browser Event Bus
3. React Custom Hook for Event Bus Subscriptions
4. Consuming Across Remotes
In Remote A (Cart Micro-Frontend):
In Host Shell Header:
5. Architectural Rule: Keep Cross-App Events Coarse-Grained
Never broadcast microscopic state changes (e.g. character-by-character typing). Events should be coarse-grained domain events ("order:submitted", "user:authenticated"). Sharing state fine-grained across micro-frontends creates tight distributed coupling, destroying the independence of your squads.
Practice Quiz
Production Build Optimization and Asset Compression
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Host and Remote Container Configuration | Production Build Optimization and Asset Compression |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.