@bimetal/broker-broadcast
v0.30.0
Published
BroadcastChannel adapter for @bimetal/broker — cross-tab, same-origin pub/sub. Zero external dependencies.
Maintainers
Readme
@bimetal/broker-broadcast
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/brokerUsage
import { createBroadcastBroker } from '@bimetal/broker-broadcast';
const broker = createBroadcastBroker({ channel: 'bimetal' });
broker.subscribe('calendar.created', (msg, topic) => {
// fires in THIS tab and every other tab on the 'bimetal' channel
});
await broker.publish('calendar.created', { id: '1' });What's Inside
createBroadcastBroker(options)→ a fullBroker<TMessage>(plusclose()/flush()).channel— the BroadcastChannel name; the shared bus identity.factory?— override channel creation (defaultnew BroadcastChannel(name)); inject a fake in tests.onError?— handler-error sink (defaultconsole.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
PolyForm Noncommercial License 1.0.0
