Rxova
Skip to content

SharedWorkerTransport

Cross-tab delivery through one SharedWorker per origin.

BroadcastChannel is the better default for pure fan-out and stays the default. This transport buys something else: a single place that is not a tab. The relay outlives any individual tab, so the thing every “the leader owns the socket” design actually wants — one connection, held somewhere no user can close mid-flight — becomes possible without electing a leader at all.

What it does not buy: durability. The worker is torn down when the last port closes, exactly like a BroadcastChannel with no listeners. State still lives in the tabs; this moves the wire, not the source of truth.

The relay script is three lines and ships with the library:

// sw-relay.js — your app hosts this file
import 'use-everywhere/shared-worker';
createSharedStore('cart', {
transport: new SharedWorkerTransport({
url: new URL('./sw-relay.js', import.meta.url),
}),
});
new SharedWorkerTransport(options): SharedWorkerTransport;
ParameterType
optionsSharedWorkerTransportOptions

SharedWorkerTransport

readonly kind: TransportKind = 'shared-worker';

What this transport is. Optional so a custom transport need not declare one.

Transport.kind

close(): void;

void

Transport.close


post(data): void;
ParameterType
dataunknown

void

Transport.post


subscribe(listener): () => void;
ParameterType
listener(data) => void

() => void

Transport.subscribe