@bimetal/broker-broadcast
Scope — cross-tab pub/sub on ONE origin, in ONE browser. No server, no cross-origin, and
no ordering guarantee: the port broker reports capabilities.ordering: 'none', so a receiver
must tolerate out-of-order delivery (see bridgeBrokerToEventStore and the cookbook recipe).
BroadcastChannel adapter for @bimetal/broker — cross-tab, same-origin pub/sub. Every browser tab/worker that opens a broker on the same channel name forms one bus. Zero external dependencies; a thin wrapper over createPortBroker.
Installation
npm install @bimetal/broker-broadcast @bimetal/broker
Usage
import { createBroadcastBroker } from '@bimetal/broker-broadcast';
const broker = createBroadcastBroker({ channel: 'bimetal' });
broker.subscribe('calendar.created', (msg, topic) => {
});
await broker.publish('calendar.created', { id: '1' });
What's Inside
createBroadcastBroker(options) → a full Broker<TMessage> (plus close()/flush()).
channel — the BroadcastChannel name; the shared bus identity.
factory? — override channel creation (default new BroadcastChannel(name)); inject a fake in tests.
onError? — handler-error sink (default console.error).
BroadcastBrokerOptions, BroadcastBroker<TMessage>, BroadcastChannelLike
Adapter properties
- Capabilities:
{ patterns: true, replay: false, ordering: 'none' }. Pattern routing (*/#) is done locally on the receiving side. Cross-tab with multiple publishers has no shared clock → no ordering promise.
- Local + remote in one publish. A BroadcastChannel never echoes to its own sender, so the relay fans a publish out to local subscribers AND posts it to the channel — every tab including the originator hears it, exactly once.
- Structured clone. Messages must be structured-cloneable (DomainEvents are). A non-cloneable payload throws at
postMessage.
- Foreign messages ignored. Only broker-tagged envelopes are delivered; other traffic on the channel is skipped.
close() detaches the listener and closes the channel.
License
Apache License 2.0