Device Sync - Flutter SDK
On this page
Device Sync automatically synchronizes data between client applications and an Atlas App Services backend application. When a client device is online, Sync asynchronously synchronizes data in the background between the device and your App Services backend.
The Flutter SDK only supports Flexible Sync. You must configure your App Services backend to use Flexible Sync.
Note
The Flutter SDK does not support Partition-Based Sync.
Synced Realms
You can configure a realm to automatically synchronize data between many devices that each have their own local copy of the data. Synced realms use a different configuration than local-only realms and require an Atlas App Services backend to handle the synchronization process.
Applications can always create, modify, and delete synced realm objects locally, even when offline. Whenever a network connection is available, the Realm Flutter SDK opens a connection to an application server and syncs changes to and from other clients. The Atlas Device Sync protocol and server-side operational transforms guarantee that all fully synced instances of a realm see exactly the same data, even if some changes occurred offline and/or were received out of order.
Synced Realms vs. Non-Synced Realms
Synced realms differ from non-synced local Realm in a couple of ways:
Synced realms attempt to sync changes with your backend App Services App, whereas non-synced realms do not.
Synced realms can be accessed by authenticated users, while non-synced realms have no concept of users or authentication.
You can copy data from a non-synced realm to a synced realm, and vice versa, but you cannot sync a non-synced realm. To convert a non-synced realm to a synced realm, you can follow the process described in Migrate a Local-Only App to a Sync-Enabled App.
What is Flexible Sync?
When you select Flexible Sync for your App Services backend configuration, your client implementation must include subscriptions to queries on queryable fields. Flexible Sync works by synchronizing data that matches query subscriptions you maintain in the client application.
A subscription set contains a set of queries. Flexible Sync returns documents matching those queries, where the user has the appropriate permissions to read or write the documents. If documents match the query, but the client does not have the permission to read or write them, they do not sync to the client application.
You can form Flexible Sync queries using a limited subset of Realm Query Language. To learn more about which fields you can query, refer to the Queryable Fields documentation.
Subscription sets are based on a specific type of Realm object. To sync data for many types of Realm objects, you must have multiple subscriptions.
To use Device Sync in your client application, open a synced realm with a Flexible Sync configuration. Then, manage subscriptions to determine which documents to sync.
Enable Flexible Sync on the Backend
Before you can start syncing data from your client application using the SDK, you must enable Flexible Sync in the App Services backend.
To begin, refer to the backend documentation on enabling Device Sync.
Group Updates for Improved Performance
Every write transaction for a subscription set has a performance cost. If you need to make multiple updates to a Realm object during a session, consider keeping edited objects in memory until all changes are complete. This improves sync performance by only writing the complete and updated object to your realm instead of every change.
How Device Sync Works
Device Sync adds network synchronization between an App Services backend and client devices on top of all of the functionality of Realm. When you use Realm with Sync, realms exist on device, similar to using Realm without Sync. However, changes to the data stored in those realms synchronize between all client devices through a backend App Services instance. That backend also stores realm data in a cloud-based Atlas cluster running MongoDB.
Device Sync relies on a worker client that communicates with your application backend in a dedicated thread in your application. Additionally, synced realms keep a history of changes to contained objects. Sync uses this history to resolve conflicts between client changes and backend changes.
Applications that use Device Sync define their schema on the backend using JSON Schema. Client applications must match that backend schema to synchronize data. However, if you prefer to define your initial schema in your application's programming language, you can use Development Mode to create a backend JSON Schema based on native SDK objects as you write your application. However, once your application is used for production purposes, you should alter your schema using JSON Schema on the backend.