Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

MongoDB Realm partition strategy for a chat app

I wanted to enquire as to how to partion my chat data into realms in mongoDB realm. Users can be part of of any number of chatrooms.

Right now my chat database has _partition as the partitionValue, and the following collections-

export const chatroomSchema = {
  name: 'chatroom',
  properties: {
    _id: 'objectId',
    _partition: 'string?',
    date: 'date',
    last_message: 'string',
    title: 'string',
  },
  primaryKey: '_id',
};

export const userSchema = {
  name: 'user',
  properties: {
    _id: 'objectId',
    _partition: 'string?',
    chatrooms: 'chatroom[]',
    friends: 'user[]',
    name: 'string',
    photoUrl: 'string',
    username: 'string',
  },
  primaryKey: '_id',
};

export const messageSchema = {
  name: 'message',
  properties: {
    _id: 'objectId',
    _partition: 'string?',
    content: 'string',
    chatroom_id: 'string',
    date: 'date',
    sender: 'user?',
  },
  primaryKey: '_id',
};

export const memberSchema = {
  name: 'member',
  properties: {
    _id: 'objectId?',
    _partition: 'string?',
    chatroom_id: 'string?',
    participants: 'user[]',
  },
  primaryKey: '_id',
};

Thank You In Advance. Stay Safe!

@Aryaman_Shrey So chat applications are pretty complex but one approach I have seen is to have a partition per chat room. Generally you only want to hold about 10 partitions open at any one time for a mobile app. I’m not sure how many chatRooms you are expecting a user to join but what you can do to get around this limitation is to have a per-user notification partition which would contain a list of potential chatRooms the user could join as well as a Notification object which would say that you have a chat message pending from a chatRoom the user had joined which was closed on the mobile at that time since realm only syncs when the realm reference is active. You would use Realm Database trigger to detect that a new message was pending on the backend and insert a new Notification object into the per-user notification realm which contained a partitionKey value for the chatRoom where the message was pending.