Skip to content

@busly/core ​

npmweekly downloadsmonthly downloadstypesbundle size

Framework-agnostic event bus for same-origin browsing contexts: microfrontends on one page and separate tabs.

This is the low-level bus that every other package builds on. Most applications never use it directly and reach for an adapter instead: @busly/js, @busly/angular, or @busly/react. Using core directly makes the most sense when you're building an adapter of your own.

Install ​

bash
pnpm add @busly/core

Install it only once per page, so that every microfrontend and adapter shares the same copy.

emit ​

ts
function emit<K = unknown>(type: string, payload: K, options?: EventOptions): void;

Sends an event to every listener of type, both in the current context and in other same-origin contexts.

ParamTypeNotes
typestringEvent type
payloadKMust be structured-cloneable
options.sourcestringSender label. Default 'default'

emit wraps the payload in an envelope, { v, type, payload, ts: Date.now(), source }, and clones it before doing anything else. If the payload can't be cloned, it throws a DataCloneError and nothing is delivered.

Once the clone succeeds, the envelope is posted to the BroadcastChannel named @busly/core for other contexts, and the local handlers are queued. Delivery is never synchronous: emit returns before any handler runs. Where BroadcastChannel isn't available, emit still delivers to handlers in the current context.

ts
emit('[Counter] set', 42, { source: 'counter-mfe' });

subscribe ​

ts
function subscribe<K = unknown>(type: string, handler: SubscriptionHandler<K>): () => void;

Registers handler for type and returns a function that removes it.

Each handler runs in its own microtask, so one that throws doesn't affect the others, and a handler removed before its turn is simply skipped. All the handlers in a context receive the same envelope object.

The first call to subscribe opens the channel. From then on, incoming messages that aren't well-formed envelopes of the current version are ignored. The payload itself is never validated.

ts
const off = subscribe<number>('[Counter] set', (envelope) => {
  console.log(envelope.payload, envelope.source, envelope.ts);
});

off();

isBusEvent ​

ts
function isBusEvent(value: unknown): value is IBusEvent;

Returns true for objects that have a string type and a payload key. It's a soft guard against accidental collisions, not a security check.

detectServer ​

ts
function detectServer(): boolean;

Returns true when there is no window and the code isn't running in a worker. Adapters use it to turn themselves off on the server; core itself never calls it.

Types ​

ts
interface Envelope<K = unknown> {
  v: number;       // envelope version
  type: string;
  payload: K;
  ts: number;      // Date.now() on the sender
  source: string;
}

type SubscriptionHandler<K = unknown> = (event: Envelope<K>) => void;

interface EventOptions {
  source?: string;
}

interface EnvOptions {
  /** Override server detection. Default: no `window` and not a worker. */
  server?: boolean;
}

interface IBusEvent<T = unknown> {
  type: string;
  payload: T;
}

type IBusEventCreator = ((payload: any) => IBusEvent) & { type: string };

type IBusEventGroup = Record<string, IBusEventCreator>;

/** One method per creator: calling it creates and dispatches the event. */
type SelfDispatchingEvents<G extends IBusEventGroup> = {
  readonly [K in keyof G]: Parameters<G[K]> extends [infer P] ? (payload: P) => void : () => void;
};

These structural types match creators from @busly/events and @ngrx/signals/events by their shape, so adapters never need to import an event library.

Server ​

Don't emit or subscribe on the server. Core keeps one registry per JavaScript process, so on Node, events would reach listeners belonging to other concurrent requests, and Node's own BroadcastChannel would carry them to other worker threads too. The official adapters turn themselves off on the server, and custom adapters need to do the same. Server-side rendering has the details.

Proper SSR support is on the roadmap and currently in triage.

Security ​

Core trusts everything on the same origin, and any script there can post to the channel. The envelope check protects against accidental collisions with other libraries, not against attacks. See Scope for more.