StorageTransport
Cross-tab delivery over the storage event, for browsers with no
BroadcastChannel.
The mechanism is a quirk turned to advantage: writing to localStorage fires
a storage event in every other same-origin tab and never in the writer —
exactly BroadcastChannel’s no-self-echo semantics, for free.
Two differences from the real thing, both deliberate and both documented:
- Fidelity is JSON, not structured clone.
localStorageholds strings. The default serializer therefore rejects every value JSON would quietly change — aDate, aMap, a function — rather than let a write appear to succeed while peers receive something else. Pass aSerializer(devalue, superjson) to carry those instead. - The entry is removed immediately after writing. Peers have already been
notified by then (the event carries the value), and leaving application
state sitting in
localStoragewould be both a quota cost and a privacy one. The removal fires a second event with a nullnewValue, which receivers ignore.
Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new StorageTransport( name, storage?, serializer?): StorageTransport;Parameters
Section titled “Parameters”| Parameter | Type | Default value |
|---|---|---|
name | string | undefined |
storage | Storage | globalThis.localStorage |
serializer | Serializer | jsonSerializer |
Returns
Section titled “Returns”StorageTransport
Properties
Section titled “Properties”readonly kind: TransportKind = 'storage';What this transport is. Optional so a custom transport need not declare one.
Implementation of
Section titled “Implementation of”Methods
Section titled “Methods”close()
Section titled “close()”close(): void;Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”post()
Section titled “post()”post(data): void;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
data | unknown |
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”subscribe()
Section titled “subscribe()”subscribe(listener): () => void;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
listener | (data) => void |
Returns
Section titled “Returns”() => void