MongoDB Realm and the Realm Mobile Database help mobile developers to build performant, resilient mobile applications in significantly less time. Apps built on Realm work when users are offline, and can sync data in realtime whenever devices are connected. When building a mobile app, MongoDB Realm’s SDKs and application development services are used alongside the Realm Mobile Database to speed development velocity.
Realm Sync: Key to MongoDB Realm is Realm Sync, an out-of-the-box data synchronization service that keeps data up-to-date between different users and devices. Realm Sync reduces the code a developer needs to write when they want to start syncing data between a mobile device and Atlas, and makes the work a lot simpler.
Realm Mobile Database: Realm Sync is used alongside the Realm Mobile Database. The Realm Mobile database is an open source, fully-featured mobile datastore that’s embedded directly on a user’s device. It allows apps to read/write locally, meaning apps can work even when a user is offline. The Realm Mobile Database is object-oriented, so it maps to the way mobile developers think and code, and it works cross-platform, for both iOS and Android.
In this blog we explain how inviz.ai built a search relevancy platform across different channels using MongoDB Atlas and Realm in a seamless way.
Problem Statement At inviz we built a crowd sourced data labeling solution. With this solution raw data gets distributed to human judges to get annotated. To minimize annotation costs, we picked judges from remote places, which presented connectivity challenges.
We saw that power failures and poor network connectivity were causing a significant loss of productivity from our human judges. There have been a high number of connection timeouts between UI or mobile applications to our cloud servers. A lack of network connectivity prevents the judges from being able to proceed with their work, and forces them to continuously check to see if the connection is strong enough to submit their assessments.
To account for this issue, we built a system where a sudden loss of connectivity would not prevent judges from continuing with their work.
Solution To reduce the issues we faced due to network hiccups, we wanted to build a solution where judges do not need to worry about being online in order to submit their judgement.
We decided the best way to solve the issue was to store the judgements locally first, directly on the judge’s device. Then, we would sync the data with the backend system whenever there was a strong enough network connection.
This idea led us to use MongoDB Realm, with Realm’s real-time data sync at the center of this new implementation.
With this new implementation every judge was assigned multiple tasks at a time. The judgement they created was first stored in the realm embedded on their mobile devices. At regular intervals, each device’s realm syncs the data to MongoDB’s cloud database, Atlas. With this approach the problems faced due to sudden drop of network connectivity have been solved to a great extent.
- MongoDB Atlas account
- Node JS >= 10.x
- Android/iOS development tools
- Android/iOS device or emulator
- List item
- Login to MongoDB Atlas account and create a database (e.g loopr).
- Add collections (products, projects, users) from MongoDB Atlas Collection Explorer, which can be done by connecting mongo shell through CLI.
- Add seed data to collection. For reference, see the images below.
Go to the Realm Tab from the MongoDB Cloud console. Now we have to connect the realm to mongodb. Visit the Realm Tutorial to set up a barbone backend.
In Realm these steps are required:
- Connect the mongodb database
- Select collections that need to be synchronized
- Define the schema, access controls, and relationships for collections
- Start the Sync
Please refer to the below images for the schema declaration of different collections:
(R-5) Users schema
Validation of collections data can be done through the Realm console to verify collections are valid with schema defined.
Writing the Client App
- Set up a starter project with react-native.
- Add npm packages for react mobile support like navigation, gesture, icons and Realm node package.
- Define Realm entrypoint and schemas, providers, views. Refer to the below image for project structure.
This is the first screen that comes up when the app starts. Users can login with a registered email and password.
After successful login, the app loads the project screen which lists all projects that a particular user has been assigned to. Users can scroll and click a particular project to view the Project Summary screen.
Project summary screens give a summary of annotated products like distribution of scores among products. Once users click on the Start Annotation button it takes them to the Products Annotation Screen.
The Products Annotation Screen displays a list of products added to a particular project. Products can be viewed by swiping left/right on the screen. There is a scoring group of buttons (Off-topic, Acceptable, Good, Excellent) after the product description. Users can rate the score by judging the query with the result image and description. Example: In the below images, the first query is Digital Camera but the result is memory card so the score would be Off-Topic. The second query is Digital Camera and the result is Canon digital camera so the score would be either Good or Excellent.
Realm Services and Features Used:
- Realm Authentication: Email/Password and OAuth provider login
- Realm Functions and Triggers: For specific tasks, like when a user is created add it to MongoDB
- Realm Rules: For access control of data
- Realm Mobile Database:
- Local Application storage: lightweight and simple. Native query is available for different platforms for storing, updating and accessing the data.
- Reactive UI: Live objects passed to views, so any change in data will directly update UI screens.
- Realm Sync Protocol: Provides network reliability. The Realm Mobile Database is offline first meaning whatever read/write happens, it first goes to the application’s local database, then a background thread update, and retrieves changes from the remote database over the network.