Sync Data in Atlas with a Client Application
On this page
If your organization already has data in MongoDB Atlas, you can sync that data to client devices using Device Sync and Realm Database. Follow these high-level steps to get started:
Set up Atlas Access
Create an Atlas App Services App
The App Services application is the gateway that enables your client device to connect to MongoDB Atlas. When you create an App, you name it, link it to an Atlas cluster, and specify the deployment model and deployment region that work best for your application.
To learn how to create an App Services App, see Create an App.
Configure Device Sync
Device Sync is the service that manages synchronizing your Atlas data with your client devices. Device Sync, together with the Realm SDKs, automatically manages network connectivity, conflict resolution, user authentication, and user permissions and data access.
When you configure Device Sync, you specify the data source that the client devices can access, as well as the permissions that determine what data a user can read and write.
You can configure Device Sync via the Atlas UI, the Atlas App Services Command Line Interface, or the App Services Admin API. The first time you configure Device Sync, you may find it helpful to use the Atlas UI as it provides links and information about various settings and options.
Enable an Authentication Provider
Your client users must authenticate in order to access synced data. App Services provides several authentication providers to enable your users to authenticate. Configure one or more of these authentication providers to enable authentication in your client application.
You can configure authentication providers within the App Services App UI by selecting Authentication in the left navigation menu, and then click a provider to configure it. You can also configure authentication providers by editing the App Services configuration with the Atlas App Services Command Line Interface, or the App Services Admin API.
Generate Client Object Models
When you already have data in Atlas, you can generate object models that match that data to use in your client application.
To generate an object model, you must have an App Services schema. Once you have a schema, you can generate object models that match that schema. You can select a programming language to use to generate the schema, which you can then view and copy to use in your client application code.
You can generate a schema from existing Atlas data using the App Services UI. For more information on how to do this, refer to: Create a Realm Object Schema from an App Services Schema.
Once you have a schema, you can generate a Realm object model using the App Services UI or the Atlas App Services Command Line Interface.
Add Sync to the Client Application
After this, you've got everything set up that you need on the Atlas side, and you can prepare your client application to sync data. Device Sync synchronizes data with Atlas using a Realm Database SDK. You can use the Realm SDK in your client application code to connect to Atlas and sync data with a Realm Database on the device.
Open a Synced Realm
Once you have an authenticated user, you can open a synchronized instance of Realm Database to use for that user.
You define a Flexible Sync query subscription in your client code to determine what data to sync to the client application. Device Sync looks for Atlas documents that match the query, which the user has permission to read and possibly write, and synchronizes those documents to the client device as Realm objects. You can add, remove, or update Flexible Sync query subscriptions to change the documents that sync to the device.
C++ SDK
Flutter SDK
Java SDK
Kotlin SDK
.NET SDK
Node.js SDK
React Native SDK
Swift SDK
Use the Synced Realm
The syntax to read, write, and watch for changes on a synced realm is identical to the syntax for non-synced realms. While you work with local data, a background thread efficiently integrates, uploads, and downloads changesets. When a user who has write permissions makes changes on the device, the data persists locally. When the user has a network connection, the data automatically syncs back to Atlas and other devices.
For a quick guide to how to do common tasks with Realm Database, refer to the Realm SDK quick starts.
If you prefer to study a working client app to learn, some of the SDKs provide a working Device Sync app, which we call template apps. Optionally, you can go through SDK-specific tutorials that build on the Device Sync template app.
C++ SDK
Flutter SDK
Java SDK
Kotlin SDK
.NET SDK
Node.js SDK
React Native SDK
Swift SDK