Stream Data to Atlas - React Native SDK
On this page
You can use Data Ingest to stream data from the client application to a Flexible Sync-enabled Atlas App Services App.
You might want to sync data unidirectionally in IoT applications, such as a weather sensor sending data to the cloud. Data Ingest is also useful for writing other types of immutable data where you do not require conflict resolution, such as creating invoices from a retail app or logging application events.
Data Ingest is optimized to provide performance improvements for heavy client-side insert-only workloads.
Sync Data Unidirectionally from a Client Application
Define an Asymmetric Object
Asymmetric objects sync data unidirectionally. Define an asymmetric object
by setting asymmetric
to true
in your object model:
class WeatherSensor extends Realm.Object { static schema = { name: 'WeatherSensor', // sync WeatherSensor objects one way from your device // to your Atlas database. asymmetric: true, primaryKey: '_id', properties: { _id: 'objectId', deviceId: 'string', temperatureInFahrenheit: 'int', barometricPressureInHg: 'float', windSpeedInMph: 'float', }, }; }
class WeatherSensor extends Realm.Object<WeatherSensor> { _id!: Realm.BSON.ObjectId; deviceId!: string; temperatureInFahrenheit!: number; barometricPressureInHg!: number; windSpeedInMph!: number; static schema: ObjectSchema = { name: 'WeatherSensor', // sync WeatherSensor objects one way from your device // to your Atlas database. asymmetric: true, primaryKey: '_id', properties: { _id: 'objectId', deviceId: 'string', temperatureInFahrenheit: 'int', barometricPressureInHg: 'float', windSpeedInMph: 'float', }, }; }
For more information on how to define an asymmetric object, refer to Define an Asymmetric Object.
Connect and Authenticate with an App Services App
To stream data from the client to your backend App, you must connect to an App Services backend and authenticate a user.
function LogIn() { const app = useApp(); useEffect(() => { app.logIn(Realm.Credentials.anonymous()); }, []); return <></>; }
Data Ingest is a feature of Flexible Sync, so the App you connect to must use Flexible Sync.
Open a Realm
After you have an authenticated user, you can open a synced realm using a Flexible Sync configuration object.
// Create a configuration object const realmConfig = {schema: [WeatherSensor]}; // Create a realm context const {RealmProvider, useRealm, useObject, useQuery} = createRealmContext(realmConfig); // Expose a sync realm function AppWrapperSync() { return ( <AppProvider id={APP_ID}> <UserProvider fallback={LogIn}> <RealmProvider sync={{flexible: true}}> <App /> </RealmProvider> </UserProvider> </AppProvider> ); }
// Create a configuration object const realmConfig: Realm.Configuration = { schema: [WeatherSensor], }; // Create a realm context const {RealmProvider, useRealm} = createRealmContext(realmConfig); // Expose a sync realm function AppWrapperSync() { return ( <AppProvider id={APP_ID}> <UserProvider fallback={LogIn}> <RealmProvider sync={{flexible: true}}> <App /> </RealmProvider> </UserProvider> </AppProvider> ); }
Unlike bi-directional Sync, Data Ingest does not use a Flexible Sync subscription.
You cannot query an asymmetric object or write it to a local realm, so asymmetric objects are incompatible with bi-directional Flexible Sync, Partition-Based Sync, or local Realm use.
Create Asymmetric Objects
Once you have an open Realm, you can create an asymmetric object inside
a write transaction using Realm.create(). When creating an asymmetric object,
Realm.create()
returns undefined
rather than the object itself.
const App = () => { // Getting access to our opened realm instance const realm = useRealm(); const handleAddSensor = () => { realm.write(() => { realm.create('WeatherSensor', { _id: weatherSensorPrimaryKey, deviceId: 'WX1278UIT', temperatureInFahrenheit: 66.7, barometricPressureInHg: 29.65, windSpeedInMph: 2, }); }); }; return ( <Button title='Add A New Sensor' onPress={() => handleAddSensor()} /> ); };
You cannot read these objects. Once created, they sync to the App Services backend and the linked Atlas database.
Atlas Device Sync completely manages the lifecycle of this data. It is maintained on the device until Data Ingest synchronization is complete, and then removed from the device.