This article targets developers looking to build the Realm mobile database into their mobile apps and (optionally) use MongoDB Realm Sync. It focuses on the data architecture, both the schema and the partitioning strategy. I use a chat app as an example, but you can apply the same principals to any mobile app. This post will equip you with the knowledge needed to design an efficient, performant, and robust data architecture for your mobile app.
RChat is a chat application. Members of a chat room share messages, photos, location, and presence information with each other. The initial version is an iOS (Swift and SwiftUI) app, but we will use the same data model and back end Realm application to build an Android version in the future.
RChat makes an interesting use case for several reasons:
- A chat message needs to be viewable by all members of a chat room and no one else.
- New messages must be pushed to the chat room for all online members in real-time.
- The app should notify a user that there are new messages even when they don't have that chat room open.
- Users should be able to observe the "presence" of other users (e.g., whether they're currently logged into the app).
- There's no limit on how many messages users send in a chat room, and so the data structures must allow them to grow indefinitely.
If you're looking to add a chat feature to your mobile app, you can repurpose the code from this article and the associated repo. If not, treat it as a case study that explains the reasoning behind the data model and partitioning/syncing decisions taken. You'll likely need to make similar design choices in your apps.
#Prerequisites
If you want to build and run the app for yourself, this is what you'll need:
- iOS14.2+
- XCode 12.3+
#Front End App Features
A user can register and then log into the app. They provide an avatar image and select options such as whether to share location information in chat messages.
Users can create new chat rooms and include other registered users.
The list of chat rooms is automatically updated to show how many unread messages are in that room. The members of the room are shown, together with an indication of their current status.
A user can open a chat room to view the existing messages or send new ones.
Chat messages can contain text, images, and location details.
Watch this demo of the app in action.
#Running the App for Yourself
I like to see an app in action before I start delving into the code. If you're the same, you can find the instructions in the README.
#The Data
Figuring out how to store, access, sync, and share your data is key to designing a functional, performant, secure, and scalable application. Here are some things to consider:
- What data should a user be able to see? What should they be able to change?
- What data needs to be available in the mobile app for the current user?
- What data changes need to be communicated to which users?
- What pieces of data will be accessed at the same time?
- Are there instances where data should be duplicated for performance, scalability, or security purposes?
This article describes how I chose to organize and access the data, as well as why I made those choices.
#Data Architecture
I store virtually all of the application's data both on the mobile device (in Realm) and in the back end (in MongoDB Atlas). MongoDB Realm Sync is used to keep the multiple copies in sync.
The Realm schema is defined in code – I write the classes, and Realm handles the rest. I specify the back end (Atlas) schema through JSON schemas (though I cheated and used the developer mode to infer the schema from the Realm model).
I use Realm Triggers to automatically create or modify data as a side effect of other actions, such as a new user registering with the app or adding a message to a chat room. Triggers simplify the front end application code and increase security by limiting what data needs to be accessible from the mobile app.
When the mobile app opens a Realm, it provides a list of the classes it should contain and a partition value. In combination, Realm uses that information to decide what data it should synchronize between the local Realm and the back end (and onto other instances of the app).
Realm Sync currently requires that an application must use the same partition key (name and type) in all of its Realm Objects and Atlas documents.
A common use case would be to use a string named "username" as the partition key. The mobile app would then open a Realm by setting the partition to the current user's name, ensuring that all of that user's data is available (but no data for other users).
For RChat, I needed something a bit more flexible. For example, multiple users need to be able to view a chat message, while a user should only be able to update their own profile details. I chose a string partition key, where the string is always composed of a key-value pair — for example, "user=874798352934983"
or "conversation=768723786839"
.
I needed to add back end rules to prevent a rogue user from hacking the mobile app and syncing data that they don't own. Realm sync permissions are defined through two JSON rules – one for read connections, one for writes. For this app, the rules delegate the decision to Realm functions:

The Realm functions split the partition key into its key and value components. They perform different checks depending on the key component:
1 const splitPartition = partition.split("="); 2 if (splitPartition.length == 2) { 3 partitionKey = splitPartition[0]; 4 partitionValue = splitPartition[1]; 5 console.log(`Partition key = ${partitionKey}; partition value = ${partitionValue}`); 6 } else { 7 console.log(`Couldn't extract the partition key/value from ${partition}`); 8 return; 9 } 10 11 switch (partitionKey) { 12 case "user": 13 // ... 14 case "conversation": 15 // ... 16 case "all-users": 17 // ... 18 default: 19 console.log(`Unexpected partition key: ${partitionKey}`); 20 return false; 21 }
The full logic for the partition checks can be found in the canReadPartition and canWritePartition Realm Functions. I'll cover how each of the cases are handled later.
#Data Model
There are three top-level Realm Objects, and I'll work through them in turn.

#User Object
The User class represents an application user:
1 class User: Object { 2 @objc dynamic var _id = UUID().uuidString 3 @objc dynamic var partition = "" // "user=_id" 4 @objc dynamic var userName = "" 5 @objc dynamic var userPreferences: UserPreferences? 6 @objc dynamic var lastSeenAt: Date? 7 let conversations = List<Conversation>() 8 @objc dynamic var presence = "Off-Line" 9 }
I declare that the User
class top-level Realm objects, by making it inherit from Realm's Object
class.
The partition key is a string. I always set the partition to "user=_id"
where _id
is a unique identifier for the user's User
object.
User
includes some simple attributes such as strings for the user name and presence state.
User preferences are embedded within the User class:
1 class UserPreferences: EmbeddedObject { 2 @objc dynamic var displayName: String? 3 @objc dynamic var avatarImage: Photo? 4 }
It's the inheritance from Realm's EmbeddedObject
that tags this as a class that must always be embedded within a higher-level Realm object.
Note that only the top-level Realm Object class needs to include the partition field. The partition's embedded objects get included automatically.
UserPreferences
only contains two attributes, so I could have chosen to include them directly in the User
class. I decided to add the extra level of hierarchy as I felt it made the code easier to understand, but it has no functional impact.
Breaking the avatar image into its own embedded class was a more critical design decision as I reuse the Photo
class elsewhere. This is the Photo class:
1 class Photo: EmbeddedObject, ObservableObject { 2 @objc dynamic var _id = UUID().uuidString 3 @objc dynamic var thumbNail: Data? 4 @objc dynamic var picture: Data? 5 @objc dynamic var date = Date() 6 }
The User
class includes a Realm List
of embedded Conversation objects:
1 class Conversation: EmbeddedObject, ObservableObject, Identifiable { 2 @objc dynamic var id = UUID().uuidString 3 @objc dynamic var displayName = "" 4 @objc dynamic var unreadCount = 0 5 let members = List<Member>() 6 }
I've intentionally duplicated some data by embedding the conversation data into the User
object. Every member of a conversation (chat room) will have a copy of the conversation's data. Only the unreadCount
attribute is unique to each user.
#What was the alternative?
I could have made Conversation
a top-level Realm object and set the partition to a string of the format "conversation=conversation-id"
. The User object would then have contained an array of conversation-ids. If a user were a member of 20 conversations, then the app would need to open 20 Realms (one for each of the partitions) to fetch all of the data it needed to display a list of the user's conversations. That would be a very inefficient approach.
#What are the downsides to duplicating the conversation data?
Firstly, it uses more storage in the back end. The cost isn't too high as the Conversation
only contains meta-data about the chat room and not the actual chat messages (and embedded photos). There are relatively few conversations compared to the number of chat messages.
The second drawback is that I need to keep the different versions of the conversation consistent. That does add some extra complexity, but I contain the logic within a Realm Trigger in the back end. This reasonably simple function ensures that all instances of the conversation data are updated when someone adds a new chat message:
1 exports = function(changeEvent) { 2 if (changeEvent.operationType != "insert") { 3 console.log(`ChatMessage ${changeEvent.operationType} event – currently ignored.`); 4 return; 5 } 6 7 console.log(`ChatMessage Insert event being processed`); 8 let userCollection = context.services.get("mongodb-atlas").db("RChat").collection("User"); 9 let chatMessage = changeEvent.fullDocument; 10 let conversation = ""; 11 12 if (chatMessage.partition) { 13 const splitPartition = chatMessage.partition.split("="); 14 if (splitPartition.length == 2) { 15 conversation = splitPartition[1]; 16 console.log(`Partition/conversation = ${conversation}`); 17 } else { 18 console.log("Couldn't extract the conversation from partition ${chatMessage.partition}"); 19 return; 20 } 21 } else { 22 console.log("partition not set"); 23 return; 24 } 25 26 const matchingUserQuery = { 27 conversations: { 28 $elemMatch: { 29 id: conversation 30 } 31 } 32 }; 33 34 const updateOperator = { 35 $inc: { 36 "conversations.$[element].unreadCount": 1 37 } 38 }; 39 40 const arrayFilter = { 41 arrayFilters:[ 42 { 43 "element.id": conversation 44 } 45 ] 46 }; 47 48 userCollection.updateMany(matchingUserQuery, updateOperator, arrayFilter) 49 .then ( result => { 50 console.log(`Matched ${result.matchedCount} User docs; updated ${result.modifiedCount}`); 51 }, error => { 52 console.log(`Failed to match and update User docs: ${error}`); 53 }); 54 };
Note that the function increments the unreadCount
for all conversation members. When those changes are synced to the mobile app for each of those users, the app will update its rendered list of conversations to alert the user about the unread messages.
Conversations
, in turn, contain a List of Members:
1 class Member: EmbeddedObject, Identifiable { 2 @objc dynamic var userName = "" 3 @objc dynamic var membershipStatus: String = "User added, but invite pending" 4 }
Again, there's some complexity to ensure that the User
object for all conversation members contains the full list of members. Once more, a back end Realm trigger handles this.
This is how the iOS app opens a User Realm:
1 let realmConfig = user.configuration(partitionValue: "user=\(user.id)") 2 return Realm.asyncOpen(configuration: realmConfig)
For efficiency, I open the User Realm when the user logs in and don't close it until the user logs out.
The Realm sync rules to determine whether a user can open a synced read or read/write Realm of User objects are very simple. Sync is allowed only if the value component of the partition string matches the logged-in user's id
:
1 case "user": 2 console.log(`Checking if partitionValue(${partitionValue}) matches user.id(${user.id}) – ${partitionKey === user.id}`); 3 return partitionValue === user.id;
#Chatster Object
Realm Sync doesn't currently have a way to give one user permission to sync all elements of an object/document while restricting a different user to syncing just a subset of the attributes. The User
object contains some attributes that should only be accessible by the user it represents (e.g., the list of conversations that they are members of). The impact is that we can't sync User
objects to other users. But, there is also data in there that we would like to share (e.g., the user's avatar image).
The way I worked within the current constraints is to duplicate some of the User
data in the Chatster Object:
1 class Chatster: Object { 2 @objc dynamic var _id = UUID().uuidString // This will match the _id of the associated User 3 @objc dynamic var partition = "all-users=all-the-users" 4 @objc dynamic var userName: String? 5 @objc dynamic var displayName: String? 6 @objc dynamic var avatarImage: Photo? 7 @objc dynamic var lastSeenAt: Date? 8 @objc dynamic var presence = "Off-Line" 9 }
I want all Chatster
objects to be available to all users. For example, when creating a new conversation, the user can search for potential members based on their username. To make that happen, I set the partition to "all-users=all-the-users"
for every instance.
A Realm Trigger handles the complexity of maintaining consistency between the User
and Chatster
collections/objects. The iOS app doesn't need any additional logic.
An alternate solution would have been to implement and call Realm Functions to fetch the required subset of User
data and to search usernames. The functions approach would remove the data duplication, but it would add extra latency and wouldn't work when the device is offline.
This is how the iOS app opens a Chatster Realm:
1 let realmConfig = user.configuration(partitionValue: "all-users=all-the-users") 2 return Realm.asyncOpen(configuration: realmConfig)
For efficiency, I open the Chatster
Realm when the user logs in and don't close it until the user logs out.
The Realm sync rules to determine whether a user can open a synced read or read/write Realm of User objects are even more straightforward.
It's always possible to open a synced Chatster
Realm for reads:
1 case "all-users": 2 console.log(`Any user can read all-users partitions`); 3 return true;
It's never possible to open a synced Chatster
Realm for writes (the trigger is the only place that needs to make changes):
1 case "all-users": 2 console.log(`No user can write to an all-users partitions`); 3 return false;
#ChatMessage Object
The third and final top-level Realm Object is ChatMessage:
1 class ChatMessage: Object { 2 @objc dynamic var _id = UUID().uuidString 3 @objc dynamic var partition = "" // "conversation=<conversation-id>" 4 @objc dynamic var author: String? 5 @objc dynamic var text = "" 6 @objc dynamic var image: Photo? 7 let location = List<Double>() 8 @objc dynamic var timestamp = Date() 9 }
The partition is set to "conversation=<conversation-id>"
. This means that all messages in a single conversation are in the same partition.
An alternate approach would be to embed chat messages within the Conversation
object. That approach has a severe drawback that Conversation objects/documents would indefinitely grow as users send new chat messages to the chat room. Recall that the ChatMessage
includes photos, and so the size of the objects/documents could snowball, possibly exhausting MongoDB's 16MB limit. Unbounded document growth is a major MongoDB anti-pattern and should be avoided.
This is how the iOS app opens a ChatMessage
Realm:
1 let realmConfig = user.configuration(partitionValue: "conversation=\(conversation.id)") 2 Realm.asyncOpen(configuration: realmConfig)
There is a different partition for each group of ChatMessages
that form a conversation, and so every opened conversation requires its own synced Realm. If the app kept many ChatMessage
Realms open simultaneously, it could quickly hit device resource limits. To keep things efficient, I only open ChatMessage
Realms when a chat room's view is opened, and then I close them (set to nil
) when the conversation view is closed.
The Realm sync rules to determine whether a user can open a synced Realm of ChatMessage objects are a little more complicated than for User
and Chatster
objects. A user can only open a synced ChatMessage
Realm if their conversation list contains the value component of the partition key:
1 case "conversation": 2 console.log(`Looking up User document for _id = ${user.id}`); 3 return userCollection.findOne({ _id: user.id }) 4 .then (userDoc => { 5 if (userDoc.conversations) { 6 let foundMatch = false; 7 userDoc.conversations.forEach( conversation => { 8 console.log(`Checking if conversaion.id (${conversation.id}) === ${partitionValue}`) 9 if (conversation.id === partitionValue) { 10 console.log(`Found matching conversation element for id = ${partitionValue}`); 11 foundMatch = true; 12 } 13 }); 14 if (foundMatch) { 15 console.log(`Found Match`); 16 return true; 17 } else { 18 console.log(`Checked all of the user's conversations but found none with id == ${partitionValue}`); 19 return false; 20 } 21 } else { 22 console.log(`No conversations attribute in User doc`); 23 return false; 24 } 25 }, error => { 26 console.log(`Unable to read User document: ${error}`); 27 return false; 28 });
#Summary
RChat demonstrates how to develop a mobile app with complex data requirements using Realm.
So far, we've only implemented RChat for iOS, but we'll add an Android version soon – which will use the same back end Realm application. The data architecture for the Android app will also be the same. By the magic of MongoDB Realm Sync, Android users will be able to chat with iOS users.
If you're adding a chat capability to your iOS app, you'll be able to use much of the code from RChat. If you're adding chat to an Android app, you should use the data architecture described here. If your app has no chat component, you should still consider the design choices described in this article, as you'll likely face similar decisions.
#References
- If you're building your first SwiftUI/Realm app, then check out Build Your First iOS Mobile App Using Realm, SwiftUI, & Combine
- WildAid O-FISH – an example of a much bigger app built on Realm and MongoDB Realm Sync
If you have questions, please head to our developer community website where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.