Skip to content

@statesync/persistence


@statesync/persistence / createCrossTabSync

Function: createCrossTabSync()

ts
function createCrossTabSync<T>(options): CrossTabSync<T>;

Defined in: persistence/src/cross-tab.ts:185

Creates a CrossTabSync manager for real-time state synchronization between browser tabs.

Uses the BroadcastChannel API to send and receive messages. If BroadcastChannel is not supported (e.g., in Node.js or restricted iframes), a no-op implementation is returned silently.

Messages from the current tab are automatically ignored to prevent echo loops.

Type Parameters

Type ParameterDescription
TThe shape of the application state.

Parameters

ParameterTypeDescription
optionsCrossTabSyncHandlers<T>Channel configuration and event handler callbacks.

Returns

CrossTabSync<T>

A cross-tab sync manager. Call CrossTabSync.dispose when done.

Example

typescript
const crossTab = createCrossTabSync<MyState>({
  channelName: 'state-sync:settings',
  onSnapshot: (snapshot, tabId) => {
    console.log(`Received state from tab ${tabId}`);
    applier.apply(snapshot);
  },
});

Released under the MIT License.