Building a Mobile Chat App Using Realm – Data Architecture
Rate this code example
This article targets developers looking to build Realm into their mobile apps and (optionally) use MongoDB Atlas Device 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 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.
This is the first in a series of three articles on building this app:
This article was updated in July 2021 to replace
@Persistedannotation that was introduced in Realm-Cocoa 10.10.0.
If you want to build and run the app for yourself, this is what you'll need:
- XCode 12.3+
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.
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.
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).
Atlas Device 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,
The functions split the partition key into its key and value components. They perform different checks depending on the key component:
There are three top-level Realm Objects, and I'll work through them in turn.
I declare that the
Userclass top-level Realm objects, by making it inherit from Realm's
The partition key is a string. I always set the partition to
_idis a unique identifier for the user's
Userincludes some simple attributes such as strings for the user name and presence state.
It's the inheritance from Realm's
EmbeddedObjectthat 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.
UserPreferencesonly contains two attributes, so I could have chosen to include them directly in the
Userclass. 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.
I've intentionally duplicated some data by embedding the conversation data into the
Userobject. Every member of a conversation (chat room) will have a copy of the conversation's data. Only the
unreadCountattribute is unique to each user.
I could have made
Conversationa 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.
Firstly, it uses more storage in the back end. The cost isn't too high as the
Conversationonly 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 an 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:
Note that the function increments the
unreadCountfor 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.
Again, there's some complexity to ensure that the
Userobject for all conversation members contains the full list of members. Once more, a back end Atlas Trigger handles this.
This is how the iOS app opens a User Realm:
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
Atlas Device 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
Userobject 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
Userobjects to other users. But, there is also data in there that we would like to share (e.g., the user's avatar image).
I want all
Chatsterobjects 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 Trigger handles the complexity of maintaining consistency between the
Chatstercollections/objects. The iOS app doesn't need any additional logic.
An alternate solution would have been to implement and call Functions to fetch the required subset of
Userdata 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:
For efficiency, I open the
ChatsterRealm when the user logs in and don't close it until the user logs out.
The 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
ChatsterRealm for reads:
It's never possible to open a synced
ChatsterRealm for writes (the Trigger is the only place that needs to make changes):
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
Conversationobject. 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
ChatMessageincludes photos, and so the size of the objects/documents could snowball, possibly exhausting MongoDB's 16MB limit. Unbounded document growth is a major and should be avoided.
This is how the iOS app opens a
There is a different partition for each group of
ChatMessagesthat form a conversation, and so every opened conversation requires its own synced Realm. If the app kept many
ChatMessageRealms open simultaneously, it could quickly hit device resource limits. To keep things efficient, I only open
ChatMessageRealms when a chat room's view is opened, and then I close them (set to
nil) when the conversation view is closed.
The Sync rules to determine whether a user can open a synced Realm of ChatMessage objects are a little more complicated than for
Chatsterobjects. A user can only open a synced
ChatMessageRealm if their conversation list contains the value component of the partition key:
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 Atlas App Services application. The data architecture for the Android app will also be the same. By the magic of MongoDB Atlas Device 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.