npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

redux-broadcast-sync

v0.1.0

Published

Redux middleware to sync state across iframes and browser tabs via BroadcastChannel

Readme

redux-broadcast-sync

Redux middleware to sync state across iframes and browser tabs via the BroadcastChannel API.

  • Syncs state deltas (not actions) — only what changed is broadcast
  • Works across iframes, tabs, and any browsing context on the same origin
  • New contexts receive the current state automatically on mount
  • Per-sender sequence numbers prevent stale or out-of-order updates
  • Configurable: channel name, debounce, excluded slices and actions

Installation

npm install redux-broadcast-sync

@reduxjs/toolkit is a peer dependency — make sure it's installed in your project.


Setup

1. Wrap your root reducer

// store.ts
import { combineReducers, configureStore } from "@reduxjs/toolkit";
import { createBroadcastMiddleware, createSyncableReducer } from "redux-broadcast-sync";

const rootReducer = combineReducers({
  user: userReducer,
  products: productsReducer,
});

const { middleware, cleanup } = createBroadcastMiddleware();

export const store = configureStore({
  reducer: createSyncableReducer(rootReducer),
  middleware: (getDefault) => getDefault().concat(middleware),
});

// HMR cleanup (Vite)
if (import.meta.hot) {
  import.meta.hot.dispose(cleanup);
}

2. Use Redux normally

No changes needed in your components or slices. Any dispatch() call will automatically sync the state delta to all other contexts sharing the same channelName.


API

createBroadcastMiddleware(options?)

Creates the middleware and returns { middleware, cleanup }.

| Option | Type | Default | Description | |---|---|---|---| | channelName | string | "redux-sync" | Name of the shared BroadcastChannel | | debounceMs | number | 0 | Debounce delay before emitting a STATE_UPDATE | | fallbackTimeoutMs | number | 50 | Timeout before a new context declares itself ready if no peer responds | | excludeSlices | string[] | [] | State slices to exclude from sync (e.g. local UI state) | | excludeActions | string[] | [] | Action types that will not trigger a sync (e.g. local-only actions) | | excludeAction | (action) => boolean | undefined | Predicate to exclude actions dynamically — return true to skip sync | | createChannel | (name: string) => BroadcastChannel | (name) => new BroadcastChannel(name) | Custom channel factory — use this to inject a polyfill | | generateId | () => string | () => crypto.randomUUID() | Custom instance ID generator — useful when crypto.randomUUID is unavailable | | maxPendingUpdates | number | 100 | Max STATE_UPDATEs buffered before INIT_RESPONSE — oldest are dropped if exceeded |

const { middleware, cleanup } = createBroadcastMiddleware({
  channelName: "my-app",
  excludeSlices: ["ui", "router"],
});

createSyncableReducer(rootReducer)

Wraps your root reducer so it can receive state patches from other contexts.

const store = configureStore({
  reducer: createSyncableReducer(rootReducer),
});

SYNC_ACTION_TYPE

The internal action type used to apply incoming state patches ("@@BROADCAST_SYNC"). Useful if you need to filter it out in other middleware or redux-devtools.


How it works

Context A (iframe / tab)          Context B (iframe / tab)
─────────────────────────         ─────────────────────────
dispatch(addUser(...))
  → rootReducer updates state
  → shallowDiff detects delta
  → BroadcastChannel.postMessage
        STATE_UPDATE { user: {...} }
                                    → handleStateUpdate()
                                    → dispatch(@@BROADCAST_SYNC)
                                    → syncableReducer merges delta
                                    → React re-renders

New context initialization:

When a new context boots, it sends a REQUEST_STATE message. Any existing context responds with its full state (INIT_RESPONSE). If no peer responds within fallbackTimeoutMs (50ms by default), the context starts fresh with its own initial state.


Example: iframes with shared Redux state

// Each iframe creates its own store with the same setup.
// State stays in sync across all of them automatically.

// iframe-a/store.ts
const { middleware } = createBroadcastMiddleware({ channelName: "app" });
export const store = configureStore({
  reducer: createSyncableReducer(rootReducer),
  middleware: (get) => get().concat(middleware),
});

// iframe-b/store.ts — identical setup, different React tree
const { middleware } = createBroadcastMiddleware({ channelName: "app" });
export const store = configureStore({
  reducer: createSyncableReducer(rootReducer),
  middleware: (get) => get().concat(middleware),
});

Excluding local state

Use excludeSlices to prevent entire state slices from being broadcast to other contexts:

createBroadcastMiddleware({
  excludeSlices: ["ui", "notifications", "router"],
});

Use excludeActions to prevent specific actions from triggering a sync — the action still runs through the reducer locally, but the resulting state delta is not broadcast:

createBroadcastMiddleware({
  excludeActions: ["ui/setHoveredRow", "ui/openDropdown"],
});

Use excludeAction for dynamic filtering with a predicate — useful when a list of strings isn't expressive enough:

createBroadcastMiddleware({
  // Exclude an entire feature by prefix
  excludeAction: (action) => action.type.startsWith("ui/"),
});

createBroadcastMiddleware({
  // Exclude based on payload
  excludeAction: (action) => action.type === "items/select" && action.payload.temporary,
});

Both excludeActions and excludeAction can be used together — an action is excluded if either matches.


Browser support

Requires the BroadcastChannel API — supported in all modern browsers. Not available in Web Workers or Service Workers without a polyfill.

To use in environments without native BroadcastChannel support (e.g. Web Workers, older browsers), inject a polyfill via the createChannel option:

import { BroadcastChannel } from "broadcast-channel";

createBroadcastMiddleware({
  createChannel: (name) => new BroadcastChannel(name),
});