Library · Version 0.0.1
lorentz-event-bus
Communication between micro frontends without sharing any instance.
Source code ↗See the demo: Wormhole
lorentz-event-bus
The Wormhole library. Messages travel on a browser CustomEvent named lorentz-event-bus dispatched on window, so separately built and deployed micro frontends can talk to each other with no shared runtime dependency. It also ships a directive to load Module Federation remotes.
Design principles
- No shared instance: the channel is a browser event.
- Addressing: a specific recipient or ALL for broadcast.
- Three message types: user (authentication), service (bus housekeeping), application (application data).
- RxJS-based listening with automatic teardown through takeUntilDestroyed.
What’s in it
LorentzEventBusService- Sends messages with sendEventBusMessage and receives them with listen.
EventMessage- Message with recipient, text, optional sender and type.
announcePresence- A micro frontend tells everyone it has connected.
SetComponentDirective- Loads at runtime a component exposed by a Module Federation remote.
Example
Sending and listening to a message
const bus = inject(LorentzEventBusService)
bus.sendEventBusMessage(new EventMessage('1986', 'Hello from 2019', '2019'))
bus.listen('1986')
.pipe(takeUntilDestroyed())
.subscribe(m => console.log(m.from, m.message))