Skip to content

Multiple Tabs ​

By default, every tab of your app holds its own copy of the data and runs its own SyncManager. Each tab pulls on its own, and each one listens for remote changes. With a WebSocket that notifies the client about changes, every open tab opens a connection and pulls after every notification.

With two pieces, an app syncs once per browser instead:

  1. Shared data. A WorkerDataAdapterHost in a SharedWorker serves every tab. All tabs work on the same storage, and a write in one tab shows up in the live queries of all others.
  2. One syncing tab. A createSyncCoordinator elects one tab to pull, push and call registerRemoteChange. When that tab closes, another one takes over.

Why every tab still records its writes ​

A SyncManager learns about a local change from the added, changed and removed events of the Collection it is attached to. A collection emits these only for writes made through it. A write in another tab changes the shared data, but it emits no event here. If only one tab ran a SyncManager, the writes of all other tabs would never be pushed, and that tab's next sync could even remove them again, because the server does not know them.

So every tab creates a SyncManager and registers its collections. All of them record their own writes. Only the elected one talks to the server, and it pushes the changes of every tab. This works because the SyncManager's pending changes, snapshots and sync operations live on the shared data as well: that is what its dataAdapter option is for.

Setup ​

ts
// shared-worker.ts
import { WorkerDataAdapterHost } from '@signaldb/core'
import createIndexedDBAdapter from '@signaldb/indexeddb'

const host = new WorkerDataAdapterHost(null, {
  storage: name => createIndexedDBAdapter(name),
})

self.addEventListener('connect', (event) => {
  const [port] = event.ports
  host.connect(port)
  port.start()
})
ts
// in every tab
import { Collection, WorkerDataAdapter } from '@signaldb/core'
import { SyncManager, createSyncCoordinator } from '@signaldb/sync'

const worker = new SharedWorker(new URL('./shared-worker.ts', import.meta.url), {
  type: 'module',
})
worker.port.start()
const dataAdapter = new WorkerDataAdapter(worker.port, { id: 'my-app' })
const coordinator = createSyncCoordinator('my-app')

const Todos = new Collection('todos', coordinator.wrap(dataAdapter))

const syncManager = new SyncManager({
  dataAdapter, // pending changes are shared between the tabs
  coordinator,
  pull: async ({ name }) => { /* … */ },
  push: async ({ name }, { changes }) => { /* … */ },
  registerRemoteChange: ({ name }, onChange) => {
    // only called in the elected tab
  },
})
syncManager.addCollection(Todos, { name: 'todos' })

Three details matter:

  • Pass dataAdapter to the SyncManager. The constructor throws when coordinator is set without it. It has to be an adapter all tabs share; with a DefaultDataAdapter per tab, the pending changes of a tab that is not elected would never be pushed.
  • Create the synced collections with coordinator.wrap(dataAdapter). A write of another tab is recorded as a change a moment after it reached the shared data. The wrapped adapter makes the elected tab wait for that moment before it applies pulled data, and makes writes wait while it does. Without it, pulled data could briefly overwrite such a write.
  • Use the same name in every tab, and a different one for a different app on the same origin.

What each tab does ​

Elected tabOther tabs
Records its own writesYesYes
PushesThe changes of all tabsNo
Pulls, calls registerRemoteChangeYesNo
sync(), syncAll(), pushChanges()SyncsAsks the elected tab, resolves once it has synced
isSyncing()Its own syncsThe syncs of the elected tab
isLeader() ⚡️truefalse

When the elected tab closes, the browser hands its lock to another tab. That tab pushes what is pending and pulls once, to catch up on notifications that arrived while no tab was listening. A push that was in flight when the elected tab closed is sent again by the next one, so make push safe to repeat — which it already has to be for a push that fails.

Environments without SharedWorker ​

Without shared data there is nothing to coordinate: leave out coordinator, and let every tab run its own SyncManager with its own data, as before. Each write is still pushed exactly once, by the tab that made it; the cost is one pull and one registerRemoteChange per tab. Keep the SyncManager's own bookkeeping per tab in that case — do not point the dataAdapter of several tabs at one shared persistent store.

SharedWorker is available in desktop browsers and in Safari on iOS 16 and later. Chrome on Android enabled it again only recently, so check support for the versions you target. In React Native and Node there is only one instance, and createSyncCoordinator simply elects it.

Electron windows share their data through the main process rather than a SharedWorker. The Web Locks API does not reach the main process, so createSyncCoordinator does not coordinate it; run the SyncManager in one place there.

Released under the MIT License.