A tiny, page-wide channel bus for micro frontends that are built and deployed apart. A listener that subscribes late still gets the last value, every copy of the package on the page shares one bus, and TypeScript is optional.
npm install @softarc/native-federation-communicationThere are two roles:
-
The host installs the bus, once. Make this the first import of the module that
initFederationloads, oftenbootstrap.ts.main.tsis too early: shared packages resolve only afterinitFederation. Without a build step, load theinstall.jsscript tag instead.import '@softarc/native-federation-communication/install';
-
The host and every remote import
busand talk over named channels:import { bus } from '@softarc/native-federation-communication'; // checkout: publish state on the 'cart:changed' channel bus.emit('cart:changed', { count: 3 }); // header, loaded before or after: called with { count: 3 } at once, then with every later value const off = bus.on('cart:changed', ({ count }) => (badge.textContent = String(count))); off(); // when the header is torn down // a command: only the listeners subscribed right now get it, and nothing is kept for later ones bus.emit('ui:toast', { text: 'Added to cart' }, { retain: false });
A remote never installs a bus. If the host hasn't installed one, bus drops every call and logs a
warning.
Use the bus only for what nothing else carries: navigation state belongs in the URL, data from a parent to a child component in attributes.
| Guide | Read it to |
|---|---|
| Getting started | Install the bus, then publish state and send commands |
| API | Look up every export, option, error and warning |
| Typing | Check channel values in TypeScript and JavaScript |
| Recipes | Use it from any framework, plain JS, RxJS, SSR and tests |
| Multiple copies | Mix versions on one page, run outside a browser, semver |
| Cross-tab | Share channels between tabs of the same origin |
| Migrating from the registry | Replace the orchestrator's __NF_REGISTRY__ with the bus |
MIT, see LICENSE.md.