A lightweight middleware to sync your redux state across browser tabs. It will listen to the Broadcast Channel and dispatch exactly the same actions dispatched in other tabs to keep the redux state in sync.
Why Redux-State-Sync 3.0?
In redux state sync 1.0, communicate between tabs are fully rely on local storage which means the actions send to other tabs are actually saved in the local storage. We also need to use JSON.stringify and JSON.parse while we saving or retrieving it and all localStorage calls are synchronous. All of this could cause performance problems.
Thanks to BroadcastChannel, we now have a more efficient way to communicate between tabs instead of using any type of local storage. However, Not all the browsers support BroadcastChannel API for now. So I used pubkey's BroadcastChannel to find the best way to communicate between tabs for redux-state-sync 3.0. BroadcastChannel will make sure that the communication between tabs always works.
How to install
Install with npm.
npm install --save redux-state-sync
Install with yarn
yarn add redux-state-sync
Install with npm.
npm install --save-dev @types/redux-state-sync
Install with yarn
yarn add --dev @types/redux-state-sync
Types are defined here
How to use
Create the state sync middleware with config:
;;const config =// TOGGLE_TODO will not be triggered in other tabsblacklist: "TOGGLE_TODO";const middlewares = ;const store = ;
Init new tabs with existing state:
- Use initStateWithPrevTab to get existing state from other tabs
;;const config =// TOGGLE_TODO will not be triggered in other tabsblacklist: "TOGGLE_TODO";const middlewares = ;const store = ;// init state with other tabs;
- Wrap your root reducer with
;const rootReducer =;rootReducer;
Note: ignore this if you are using
redux-persist, because you will always inite your app with the state in the storage. However, if you don't want to persist the state in the storage and still want to init new tabs with opening tabs' state, you can follow the example above.
Unique name for Broadcast Channel
const config =channel: "my_broadcast_channel";const middlewares = ;
A function to let you filter the actions as you wanted.
Note: Since version 3.0 the function receives the action itself and not only the action type.
const config =// All actions will be triggered in other tabs except 'TOGGLE_TODO'actiontype !== "TOGGLE_TODO";const middlewares = ;
A list of action types that you don't want to be triggered in other tabs.
const config =// All actions will be triggered in other tabs except 'TOGGLE_TODO'blacklist: "TOGGLE_TODO";const middlewares = ;
Only actions in this list will be triggered in other tabs.
const config =// Only 'TOGGLE_TODO' will be triggered in other tabswhitelist: "TOGGLE_TODO";const middlewares = ;
Warning: You should only use one of the option to filter your actions. if you have all 3 options predicate, blacklist, and whitelist, only one will be effective and the priority is predicate > blacklist > whitelist.
Redux-state-sync is using BroadcastChannel to comunicate between tabs. broadcastChannelOption is the option passed to broadcastChannel when we creating the channel.
const config =// Only 'TOGGLE_TODO' will be triggered in other tabswhitelist: "TOGGLE_TODO"// enforce a type, oneOf['native', 'idb', 'localstorage', 'node']broadcastChannelOption: type: "localstorage";const middlewares = ;
Prepare state for sending to channel. Will be helpful when using Immutable.js
default: state => state
const config =// Map immutable object to jsstate;const middlewares = ;