broadcastQueryClient is a utility for broadcasting and syncing the state of your QueryClient between browser tabs/windows with the same origin.
Use Cases
- Keep data synchronized across multiple tabs
- Reflect mutations in all open tabs instantly
- Share cache updates across windows
- Provide a seamless multi-tab experience
Installation
This utility comes as a separate package:Quick Start
How It Works
The utility uses the Broadcast Channel API to communicate between tabs:- When the QueryClient cache changes in one tab, the change is broadcast
- Other tabs receive the broadcast and update their QueryClient
- All tabs stay in sync automatically
The Broadcast Channel API is supported in all modern browsers (Chrome 54+, Firefox 38+, Edge 79+, Safari 15.4+). It does not work across different origins.
API
broadcastQueryClient
queryClient(required): The QueryClient instance to broadcastbroadcastChannel(optional): Channel name for communication (default:'tanstack-query')
Options Interface
Examples
Basic Usage
App.tsx
- Fetch data in one tab, other tabs get updated
- Run a mutation in one tab, other tabs reflect the change
- Invalidate queries in one tab, other tabs refetch
Multiple Applications
Use different channel names for different apps:With React
With Persistence
Combine with persistence for offline-first multi-tab apps:- Real-time sync across tabs (broadcast)
- Data persisted across sessions (persist)
What Gets Synchronized?
The broadcast synchronizes:- Query data: Fresh data from successful queries
- Query status: Loading, error, success states
- Mutations: Mutation results and status
- Cache updates: Manual cache updates via
setQueryData - Invalidations: Query invalidations trigger refetches in all tabs
The broadcast is one-way communication. Each tab maintains its own QueryClient but listens for updates from other tabs.
Performance Considerations
Throttling
By default, broadcasts are throttled to prevent performance issues. The system automatically:- Batches rapid changes
- Debounces updates
- Only broadcasts meaningful changes
Large Caches
For applications with large caches:Debugging
Enable logging to see broadcast activity:Browser Compatibility
| Browser | Version | Support |
|---|---|---|
| Chrome | 54+ | ✅ |
| Firefox | 38+ | ✅ |
| Safari | 15.4+ | ✅ |
| Edge | 79+ | ✅ |
| Opera | 41+ | ✅ |
| IE | Any | ❌ |
The utility will fail silently in browsers that don’t support the Broadcast Channel API. Your app will continue to work, but without cross-tab synchronization.
Polyfills
For older browsers, you can use a polyfill:Security Considerations
Same-Origin Policy
https://example.comandhttps://example.com✅ Can communicatehttps://example.comandhttp://example.com❌ Cannot communicate (different protocol)https://example.comandhttps://example.com:3000❌ Cannot communicate (different port)https://app.example.comandhttps://example.com❌ Cannot communicate (different subdomain)
Data Privacy
Data broadcast via this API:- Is not encrypted
- Is only shared within the same origin
- Never leaves the user’s device
- Is not visible to other websites
Limitations
- Same origin only: Cannot sync across different domains/protocols/ports
- Message size limits: Very large updates may fail to broadcast
- Browser support: Requires modern browsers
- No persistence: Broadcasts are ephemeral (combine with persist for durability)
- Experimental API: Subject to breaking changes
Common Patterns
Sync After Login
Sync Shopping Cart
Sync Notifications
Troubleshooting
Changes not syncing
- Check browser support: Open DevTools and verify
BroadcastChannelexists - Verify same origin: Ensure all tabs are on the same protocol/domain/port
- Check channel name: Ensure all tabs use the same
broadcastChannelstring - Inspect console: Look for errors or warnings
Performance issues
- Reduce broadcast frequency: Increase throttle time or reduce query refetch frequency
- Limit broadcasted data: Only broadcast essential queries
- Check cache size: Large caches may cause issues
Memory leaks
- Clean up on unmount: Ensure QueryClient is properly disposed
- Limit cache size: Use
gcTimeto control cache size - Monitor DevTools: Check memory usage in Performance tab
TypeScript
Full TypeScript support:Further Reading
Broadcast Channel API
Learn about the underlying browser API
Persist Client
Combine broadcasting with persistence
Offline Support
Understand network modes
Mutations
Learn about mutations that trigger syncing