Skip to content

Event Bus

In this lesson you will learn event bus in single-spa, why it matters within communication, and how to use it correctly with clear, copy-ready examples.

Event Bus Overview

At its core, event bus is about doing one thing well inside your single-spa project. Once you understand the pattern, you can apply it consistently across features and teams.

Good event bus pays off across the whole codebase: fewer surprises, easier testing, and smoother onboarding. The snippet below is a solid starting point.

// shared utility module exposed via the import map
export const authService = {
  getToken: () => localStorage.getItem('token'),
};

// consumed by any micro frontend
import { authService } from '@org/utils';

Shared utility modules provide cross-app services like auth without tight coupling.

Event Bus Example

registerApplication({
  name: '@org/app',
  app: () => System.import('@org/app'),
  activeWhen: ['/app'],
});
start();
  • Start from a minimal Event Bus example and grow it only as needed.
  • Keep configuration explicit so Event Bus behaves the same in every environment.
  • Name things clearly so teammates understand your Event Bus at a glance.
  • Add tests around Event Bus early to lock in expected behaviour.

Single-SPA Cheatsheet

Core single-spa APIs related to event bus.

API Example Purpose
registerApplication registerApplication({ name, app, activeWhen }) Register a micro frontend
activeWhen activeWhen: ['/checkout'] Route ownership
start start() Begin routing
bootstrap export async function bootstrap() One-time setup
mount export async function mount(props) Render the app
unmount export async function unmount(props) Clean up the app
import map systemjs-importmap Locate app bundles

How Event Bus Works in Single-SPA

Event Bus is part of how single-spa lets multiple applications — even in different frameworks — coexist on one page. A root config registers each app and controls when it is active.

Shared utility modules provide cross-app services like auth without tight coupling.

  • A root config registers apps and calls start().
  • Each app exports bootstrap, mount, and unmount lifecycles.
  • activeWhen decides which routes each app owns.
  • Import maps resolve each app's bundle at runtime.

Practical Guidance for Event Bus

For reliable micro frontends, event bus should isolate failures and keep shared state minimal. Let each team own its app end to end while agreeing on a few shared contracts.

Concern Recommendation
Isolation One app's crash should not break others
Shared state Prefer shared utility modules over globals
Routing Keep activeWhen rules explicit and non-overlapping
Deployment Release via import-map updates per app

Common Mistakes

  • Copying event bus snippets without understanding what each line does.
  • Skipping error handling and edge cases when wiring up event bus.
  • Leaving event bus untested, so regressions slip into production.
  • Over-engineering event bus before you actually need the extra flexibility.

Key Takeaways

  • Event Bus is a core part of working effectively with single-spa.
  • Start small and keep event bus focused on a single responsibility.
  • Apply consistent patterns so event bus scales across your project.
  • Test and document event bus to keep it maintainable over time.

Pro Tip

Bookmark this event bus pattern and reuse it. Consistency across your single-spa codebase is worth more than clever one-off solutions.