Docs Menu

Docs HomeRealm

Sync Data from Multiple Processes

You can use a single realm database to synchronize data from multiple processes using Atlas Device Sync.

Examples of scenarios where you might want to open a synced realm in multiple processes include:

  • A multi-window desktop application where each window writes to the same synced realm.

  • A server application that writes to a synced realm from multiple processes.

On a high level, syncing data to a realm from multiple processes consists of the following:

  1. Creating a single main process that opens a Realm using a standard flexible sync configuration. The main process handles synchronization.

  2. Creating one or more secondary processes that open the same realm using a disconnected sync configuration. Using a disconnected sync configuration, the secondary processes reads and writes data to the realm without handling the synchronization. The main process handles the synchronization of all data for the secondary process.

To open a synced realm in the main process, use the Configuration.flexibleSync() constructor. For more information, refer to Open a Synced Realm.

main_process.dart
// Same realm file location as secondary process
String realmPath =
path.join(Configuration.defaultStoragePath, 'synced.realm');
Configuration flexibleConfig =
Configuration.flexibleSync(currentUser, schema, path: realmPath);
Realm realmWithSync = Realm(flexibleConfig);

To open a synced realm in a secondary process, create a Configuration with the Configuration.disconnectedSync() constructor. Include the schema and any additional optional named arguments.

secondary_process.dart
// Same realm file location as primary process
final sameRealmPath =
path.join(Configuration.defaultStoragePath, 'synced.realm');
final disconnectedSyncConfig =
Configuration.disconnectedSync(schema, path: sameRealmPath);
Realm realmWithDisconnectedSync = Realm(disconnectedSyncConfig);
←  Manage a Sync Session - Flutter SDKRealm Java SDK →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.