MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Cross-Application State Sharing and Event Buses0%
Study Materials
React.js for AdvancedProgress 0%
1.1Control Props and Inversion of Control Pattern1.2State Reducer Pattern in Advanced React1.3Prop Getters and Flexible Design Systems1.4Headless UI Architecture and Accessibility Primitives
16.1Micro-Frontend Architectures and Module Federation16.2Host and Remote Container Configuration16.3Cross-Application State Sharing and Event Buses
HomeTutorialsReact.js for AdvancedCross-Application State Sharing and Event Buses
Production Build Optimization and Asset Compression

Cross-Application State Sharing and Event Buses

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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

1┌────────────────────────────────────────────────────────┐
2│ Host Shell (Event Bus Singleton / Window EventTarget) │
3└───────────▲────────────────────────────────▲───────────┘
4 │ 1. Emits 'auth:login' │ 2. Listens to 'auth:login'
5┌───────────┴──────┐ ┌──────────┴──────────┐
6│ Remote A (Auth) │ │ Remote B (Profile) │
7└──────────────────┘ └─────────────────────┘

Three primary approaches:

  1. 1
    Window CustomEvent (Recommended): Native browser events with zero dependencies; completely framework-agnostic.
  2. 2
    Custom Event Bus Singleton: A lightweight pub/sub emitter shared via Module Federation.
  3. 3
    Reactive 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

1// src/events/microEventBus.ts
2 
3export interface AppEvents {
4 "user:login": { userId: string; name: string; role: string };
5 "user:logout": undefined;
6 "cart:updated": { itemCount: number; totalAmount: number };
7 "theme:changed": { theme: "light" | "dark" };
8}
9 
10export class MicroEventBus {
11 // Emit event onto global window
12 static emit<K extends keyof AppEvents>(eventName: K, detail?: AppEvents[K]): void {
13 const event = new CustomEvent(eventName, {
14 detail,
15 bubbles: true,
16 composed: true, // Allows crossing Shadow DOM boundaries if used
17 });
18 window.dispatchEvent(event);
19 }
20 
21 // Subscribe to event with cleanup return
22 static on<K extends keyof AppEvents>(
23 eventName: K,
24 callback: (detail: AppEvents[K]) => void
25 ): () => void {
26 const handler = (event: Event) => {
27 const customEvent = event as CustomEvent<AppEvents[K]>;
28 callback(customEvent.detail);
29 };
30 
31 window.addEventListener(eventName, handler);
32 return () => window.removeEventListener(eventName, handler);
33 }
34}

3. React Custom Hook for Event Bus Subscriptions

1import { useEffect, useState } from "react";
2import { MicroEventBus, AppEvents } from "./microEventBus";
3 
4export function useMicroEvent<K extends keyof AppEvents>(
5 eventName: K,
6 handler: (detail: AppEvents[K]) => void
7) {
8 useEffect(() => {
9 // Automatically subscribes and handles teardown on unmount
10 const unsubscribe = MicroEventBus.on(eventName, handler);
11 return unsubscribe;
12 }, [eventName, handler]);
13}

4. Consuming Across Remotes

In Remote A (Cart Micro-Frontend):

1import React from "react";
2import { MicroEventBus } from "./events/microEventBus";
3 
4export function CheckoutWidget() {
5 const handleAddItem = () => {
6 // Emit event across window boundary to whoever is listening
7 MicroEventBus.emit("cart:updated", {
8 itemCount: 4,
9 totalAmount: 189.99,
10 });
11 };
12 
13 return (
14 <button
15 onClick={handleAddItem}
16 className="px-4 py-2 bg-emerald-600 text-white rounded font-semibold text-xs"
17 >
18 Add Enterprise License to Cart
19 </button>
20 );
21}

In Host Shell Header:

1import React, { useState, useCallback } from "react";
2import { useMicroEvent } from "./hooks/useMicroEvent";
3import { AppEvents } from "./events/microEventBus";
4 
5export function HostNavigationHeader() {
6 const [cartCount, setCartCount] = useState(0);
7 
8 // Listen to cross-remote broadcast
9 const handleCartUpdate = useCallback((detail: AppEvents["cart:updated"]) => {
10 setCartCount(detail.itemCount);
11 }, []);
12 
13 useMicroEvent("cart:updated", handleCartUpdate);
14 
15 return (
16 <header className="p-4 bg-slate-900 border-b border-slate-800 text-white flex justify-between">
17 <span className="font-bold">Enterprise Cloud Portal</span>
18 <div className="flex items-center gap-2">
19 <span className="text-xs text-slate-400">Cart Badge:</span>
20 <span className="px-2 py-0.5 bg-cyan-600 text-xs font-bold rounded-full">{cartCount}</span>
21 </div>
22 </header>
23 );
24}

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.


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Why is a shared monolithic Redux store discouraged across independently deployed micro-frontends?
Question 2
What browser-native primitive enables framework-agnostic cross-application messaging?
Question 3
What does composed: true accomplish in a CustomEvent initialization?
Question 4
Why must event listeners registered with window.addEventListener be cleaned up in useEffect?
Question 5
What is the recommended granularity for cross-micro-frontend event messages?
Next Lesson

Production Build Optimization and Asset Compression

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Host and Remote Container ConfigurationProduction Build Optimization and Asset Compression

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext