Multiple Tab Support
- Multiple tab support is not currently available on Android.
- For Safari, use the
OPFSCoopSyncVFS
virtual file system to ensure stable multi-tab functionality.
shared-DB-worker-[dbFileName]
and shared-sync-[dbFileName]
will be created.
shared-DB-worker-[dbFileName]
The shared database worker will ensure writes to the database will instantly be available between tabs.
shared-sync-[dbFileName]
The shared sync worker connects directly to the PowerSync backend instance and applies changes to the database. Note that the shared sync worker will call the fetchCredentials
and uploadData
method of the latest opened available tab. Closing a tab will shift the latest tab to the previously opened one.
Currently, using the SDK in multiple tabs without enabling the enableMultiTabs flag will spawn a standard web worker per tab for DB operations. These workers are safe to operate on the DB concurrently, however changes from one tab may not update watches on other tabs. Only one tab can sync from the PowerSync instance at a time. The sync status will not be shared between tabs, only the oldest tab will connect and display the latest sync status.
Support is enabled by default if available. This can be disabled as below:
Using transactions to group changes
Read and write transactions present a context where multiple changes can be made then finally committed to the DB or rolled back. This ensures that either all the changes get persisted, or no change is made to the DB (in the case of a rollback or exception). PowerSyncDatabase.writeTransaction(callback) automatically commits changes after the transaction callback is completed iftx.rollback()
has not explicitly been called. If an exception is thrown in the callback then changes are automatically rolled back.
Subscribe to changes in data
Use PowerSyncDatabase.watch to watch for changes in source tables.Insert, update, and delete data in the local database
Use PowerSyncDatabase.execute to run INSERT, UPDATE or DELETE queries.Send changes in local data to your backend service
Override uploadData to send local updates to your backend service.Accessing PowerSync connection status information
Use PowerSyncDatabase.connected and register an event listener with PowerSyncDatabase.registerListener to listen for status changes to your PowerSync instance.Wait for the initial sync to complete
Use the hasSynced property (available since version 0.4.1 of the SDK) and register an event listener with PowerSyncDatabase.registerListener to indicate to the user whether the initial sync is in progress.Report sync download progress
You can show users a progress bar when data downloads using thedownloadProgress
property from the
SyncStatus class. This is especially useful for long-running initial syncs. downloadProgress.downloadedFraction
gives you a value from 0.0 to 1.0 representing the total sync progress.
Example (React, using MUI components):
Using PowerSyncDatabase Flags
This guide provides an overview of the customizable flags available for thePowerSyncDatabase
in the JavaScript Web SDK. These flags allow you to enable or disable specific features to suit your application’s requirements.
Configuring Flags
You can configure flags during the initialization of thePowerSyncDatabase
. Flags can be set using the flags
property, which allows you to enable or disable specific functionalities.
Available Flags
enableMultiTabs
default:
true
Enables support for multiple tabs using shared web workers. When enabled, multiple tabs can interact with the same database and sync data seamlessly.broadcastLogs
default:
false
Enables the broadcasting of logs for debugging purposes. This flag helps monitor shared worker logs in a multi-tab environment.disableSSRWarning
default:
false
Disables warnings when running in SSR (Server-Side Rendering) mode.ssrMode
default:
false
Enables SSR mode. In this mode, only empty query results will be returned, and syncing with the backend is disabled.useWebWorker
default:
true
Enables the use of web workers for database operations. Disabling this flag also disables multi-tab support.Flag Behavior
Example 1: Multi-Tab Support
By default, multi-tab support is enabled if supported by the browser. To explicitly disable this feature:Example 2: SSR Mode
To enable SSR mode and suppress warnings:Example 3: Verbose Debugging with Broadcast Logs
To enable detailed logging for debugging:Recommendations
- Set
enableMultiTabs
totrue
if your application requires seamless data sharing across multiple tabs. - Set
useWebWorker
totrue
for efficient database operations using web workers. - Set
broadcastLogs
totrue
during development to troubleshoot and monitor database and sync operations. - Set
disableSSRWarning
totrue
when running in SSR mode to avoid unnecessary console warnings. - Test combinations of flags to validate their behavior in your application’s specific use case.