Skip to content

Usage ​

Busly carries events between parts of your application that otherwise have no way of talking to each other. It is built for the browser and only works there; the Scope page explains why. If an adapter runs on the server, it simply does nothing (more on that in Server-side rendering).

This page walks through the ideas every adapter shares. Once they're familiar, pick the guide for your framework. Each one starts with the packages you need to install:

If you're looking for the complete API of a specific package, head over to the Packages section.

Define events ​

Events are the contract between your applications, so it pays to define them once, in a module that every application can import. That module can live in a shared library, a shared package, or even be copied from one repository to another. What matters is that the type strings match on both sides.

ts
// counter.events.ts
import { eventGroup, type } from '@busly/events';

export const counterPageEvents = eventGroup({
  source: 'Counter Page',
  events: {
    increment: type<void>(),
    set: type<number>(),
    loadSuccess: type<{ count: number }>(),
  },
});

Each event gets a type made from the group's source and the event name, in the form [source] eventName. In this example, the set event has the type '[Counter Page] set'.

Calling a creator gives you a plain event object, so counterPageEvents.set(42) returns { type: '[Counter Page] set', payload: 42 }. The creator also exposes its type directly, as counterPageEvents.set.type. The type<T>() helper exists only to tell TypeScript what the payload looks like; it does nothing at runtime.

Keep your groups at module level rather than defining them inline inside a component, so the same creators are reused everywhere.

Event creators ​

Busly adapters don't depend on any particular event library. All they need is a function that builds a { type, payload } object and carries a type property:

ts
type EventCreator = ((payload: any) => { type: string; payload: unknown }) & {
  type: string;
};

That means creators from @busly/events, from @ngrx/signals/events, or ones you write by hand all work, and you can mix them freely. Events are matched by their type string, not by which creator produced them.

ts
// hand-written creator, no library
const set = Object.assign((payload: number) => ({ type: '[Counter Page] set', payload }), {
  type: '[Counter Page] set',
});

Payload rules ​

Payloads travel between browsing contexts through BroadcastChannel, and that shapes what you can put in them.

First, a payload must be structured-cloneable. Functions, DOM nodes, and class instances that carry methods can't be cloned, so dispatch throws a DataCloneError when it meets one, and the event isn't delivered anywhere.

Second, receivers get a copy of the payload, taken at the moment of dispatch. That includes listeners in the sender's own application, which share a single copy among themselves. If the sender changes its data afterwards, copies that were already sent don't change with it.

Third, the payload is all a receiver ever sees. Another microfrontend can't read your state, so put everything it needs into the payload, and dispatch after you've updated your own state.

Finally, Busly never validates payloads. It checks that an incoming message has the shape of a Busly envelope, but what's inside is up to you. The reasoning is in Scope.

What a receiver gets ​

A listener receives the { type, payload } object that the creator built, along with a few properties Busly adds when it wraps the event in an envelope:

PropertyMeaning
typeEvent type
payloadPayload clone
sourceSender's source label
tsDispatch time, Date.now() on the sender
vEnvelope version

Your logic should rely on type and payload. The source and ts properties are mostly useful for logging and debugging.

Listeners inside the same application all receive the same event object, so treat it as read-only. If one listener mutates the payload, the listeners after it will see the change.

Source ​

Every event carries a source label that identifies who sent it. Each adapter has its own default ('busly-js', 'busly-angular', 'busly-react') and a way to change it. Giving each application its own label, such as 'shell' or 'checkout-mfe', makes it much easier to tell in your logs which application sent what.

on and lazy ​

@busly/js and @busly/angular, along with the store integrations built on top of them, split listeners into two groups, called cohorts:

CohortUse forRuns
onState updatesFirst
lazySide effectsAfter every on listener of the event

For each event, every on listener runs before any lazy listener, no matter which one subscribed first. In practice, this means a lazy listener can rely on the state that the on listeners have just written:

ts
receiver.lazy(counterPageEvents.set, () => save(state.count)); // subscribed first
receiver.on(counterPageEvents.set, ({ payload }) => (state.count = payload));
// on → state.count = 42, then lazy → save(42)

If you've used NgRx, the idea will feel familiar: on plays the role of reducers, and lazy plays the role of effects.

The guarantee does have limits, though. It only covers synchronous work: an on listener that writes state after an await, a setTimeout, or an HTTP call will finish after the lazy listeners have already run.

It also only holds within a single copy of the adapter. If two microfrontends each bundle their own @busly/js, each copy orders its own listeners independently. When the order needs to hold across them, share one copy, for example through import maps or module federation.

React is the exception here, and its adapter doesn't offer on and lazy at all. Because setState is asynchronous, the ordering wouldn't buy you anything. The React guide explains what to do instead.

Delivery ​

Delivery is never synchronous. dispatch returns before any listener runs: listeners in the same context are called in microtasks, and other contexts receive the event through BroadcastChannel.

The sender's own application receives its events too. That makes it possible for a single dispatch to be the one path that updates every context, the current one included.

Events never interleave. If a listener dispatches a new event, that event arrives only after the current one has reached all of its on and lazy listeners.

A few more guarantees round this out. If one listener throws, the others still run. If you list the same event type twice, each event is still delivered once. And you always have to name at least one event when listening, because Busly has no "listen to everything" mode.

Reach ​

Busly works across everything that shares your origin: microfrontends on the same page, iframes, other tabs and windows, and workers. Pages on a different origin can neither send nor receive events.

Within your origin, Busly trusts every script. Any code running there can dispatch events, just as it could read your storage or call your APIs. Busly does check that incoming messages look like its own envelopes, but that check exists to avoid collisions with other libraries; it isn't a security boundary.

Server-side rendering ​

@busly/core keeps a single listener registry per JavaScript process. A Node server renders many requests in that same process, so an event dispatched while rendering one request would reach listeners belonging to the others. To avoid this, every adapter turns itself off on the server: dispatching sends nothing, and listeners never register.

This means you should dispatch from code that runs in the browser, such as event handlers, useEffect, afterNextRender, or connectedCallback. It also means state that comes from Busly events will never appear in server-rendered HTML. If the server needs to render some state, load it the usual way.

@busly/js, @busly/react, and @busly/zustand consider the environment a server when there is no window and the code isn't running in a worker, while @busly/angular relies on Angular's own platform check. Either way, workers are real Busly contexts, so they work normally.