← Blog · Frontend Development

How an Event Bus Keeps Your Frontend Microfrontends in Sync

When you start splitting a large web app into microfrontends, everything feels great — until they need to talk to each other.

Each microfrontend (MFE) owns its own code, state, and deployment. But sometimes, one MFE triggers an action that others must respond to:

“User just logged in,” “Cart updated,” “Theme changed,” “Language switched,” etc.

How do you share that event without creating tight coupling?
Enter the Event Bus.


The Problem: Cross-MFE Communication Chaos

Without an event bus, MFEs might:

  • Poll global state repeatedly.
  • Expose global functions (anti-pattern).
  • Depend on shared stores (like Redux) that couple codebases.

Result: hidden dependencies, hard-to-test code, and brittle integration.


The Idea: Decouple Communication with an Event Bus

An event bus is a lightweight pub/sub system that lets MFEs broadcast and listen to events without knowing each other directly.

flowchart LR
    A(MFE-A: Auth) -- "publish: user:login" --> Bus(Event Bus)
    Bus --> B(MFE-B: Navbar)
    Bus --> C(MFE-C: Dashboard)

Each MFE:

  • Publishes events when something happens.
  • Subscribes to events it cares about.

They never import each other’s code — they just agree on event names and payloads.

Implementing a Simple Event Bus

You don’t need a big library — a few lines of TypeScript will do:

// event-bus.ts
type EventHandler<T = any> = (payload: T) => void;

class EventBus {
  private listeners = new Map<string, Set<EventHandler>>();

  on(event: string, handler: EventHandler) {
    if (!this.listeners.has(event)) this.listeners.set(event, new Set());
    this.listeners.get(event)!.add(handler);
  }

  off(event: string, handler: EventHandler) {
    this.listeners.get(event)?.delete(handler);
  }

  emit<T>(event: string, payload: T) {
    this.listeners.get(event)?.forEach(h => h(payload));
  }
}

export const eventBus = new EventBus();

Now MFEs can use it like this:

// MFE-A: Authentication
import { eventBus } from "shared/event-bus";
eventBus.emit("user:login", { id: "u123", name: "Deni" });

// MFE-B: Navbar
eventBus.on("user:login", user => {
  document.querySelector("#username")!.textContent = user.name;
});
  • ✅ No imports between MFEs.
  • ✅ Loose coupling.
  • ✅ Independent deploys.

🧱 Where to Store It

You have three common options:

OptionDescriptionProsCons
Shared npm packageEach MFE imports the same event-bus moduleSimple, versionedMust coordinate versions
Window globalAttach to window.EVENT_BUSZero depsHarder to type-safely manage
Custom framework shellHost app injects a single bus instanceCentral controlSlightly more setup

For production, use a single instance shared via module federation or global context.

Example: Event Bus in Module Federation

In your host app (shell):

// expose global bus
window.eventBus = eventBus;

In remote MFEs:

const bus = window.eventBus;
bus.emit("cart:updated", { total: 5 });
bus.on("theme:changed", theme => applyTheme(theme));

You can even expose the bus as a shared module in Webpack Module Federation:

// webpack.config.js
shared: {
  "shared/event-bus": { singleton: true, eager: true }
}

This guarantees one shared event bus instance across all MFEs.

Advanced: Type-Safe Event Bus

If your app grows, define event types:

interface Events {
  "user:login": { id: string; name: string };
  "cart:updated": { total: number };
}

class TypedEventBus<E extends Record<string, any>> {
  private listeners = new Map<keyof E, Set<(p: any) => void>>();
  on<K extends keyof E>(event: K, handler: (p: E[K]) => void) {
    if (!this.listeners.has(event)) this.listeners.set(event, new Set());
    this.listeners.get(event)!.add(handler as any);
  }
  emit<K extends keyof E>(event: K, payload: E[K]) {
    this.listeners.get(event)?.forEach(h => h(payload));
  }
}

Now TypeScript enforces correct payloads across MFEs

Bonus: Security & Isolation Tips

  • Never expose sensitive data in event payloads.
  • Prefix events per domain: auth:, cart:, ui:*.
  • Remove listeners (off) on unmount to prevent memory leaks.
  • Add analytics/logging to track cross-MFE interactions.
micro frontendWebDevelopment

Got a slow dashboard or messy data?Punya dashboard lambat atau data berantakan?

Let's talkAyo ngobrol