Skip to content

About

No description, website, or topics provided.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Native federation communication

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.

Verify library Coverage total

Quick start

npm install @softarc/native-federation-communication

There are two roles:

  1. The host installs the bus, once. Make this the first import of the module that initFederation loads, often bootstrap.ts. main.ts is too early: shared packages resolve only after initFederation. Without a build step, load the install.js script tag instead.

    import '@softarc/native-federation-communication/install';
  2. The host and every remote import bus and 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.

Documentation

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

License

MIT, see LICENSE.md.

About

No description, website, or topics provided.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages