1

I want to create scalable chat structure in realtime database to support both private and group chat
This is my current structure

.chats
  .chat1
    .type: private
    .lastMessage
    .users
      .uid1: true
      .uid2: true
  .chat2
    .type: group
    .groupName
    .groupImage
    .lastMessage
    .users
      .uid1: true
      .uid2: true
      .uid2: true
      ... other users
.userChats
  .uid1
    .chat1:
      .lastMessage
      .lastMessageTime
      ...
    .chat2:
      .lastMessage
      .lastMessageTime
      .image
      .groupName
      ...

When a user login to the app, I want to listen to his/her chat list and show them in a list. That is why I have userChats.
The problem here is that in a group chat we can have (for example) 50 users, and when any user send new message in the group, we need to update lastMessage field (in userChats) for every 50 users, so they can update their list immediately.
But this does not seem to be the right way to do it.
Is there any better structure to achieve this goal?

1 Answer 1

3

That structure looks good for me. I have an open source project that uses a very similar structure:

Group chats data: enter image description here

Private and group chats of the users: enter image description here

The Firebase nonSQL databases require to save some data twice or even more often. When you start with it it feels sometimes strange and wrong but belive me it's not :)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your attention. But still I think this is not a scalable solution. Think of 100 group with 100 member on each, and in each group a user send message in exact same time. In this case we need to update chats info for 10,000 users in a moment!
When using the Realtime Database writing is more or less FREE beside the bandwidth. If we where using the Firestore database I would be concerned but those 10.000 writes would cost you only the Cloud functions time and processing. If you do it in batch updates it would be not so bad because you need to send the notifications anyway so the amount of running functions would be the same to. They would just run a little bit longer to execute those changes.
What I want to say if you want to get those last messages by a query or from other locations than the chats list: those would probably cost your more on the client side than doing this from the backend (cloud functions). And that would also be more complicated on the client side.
I'm thinking about keeping only the updatedAt field for userChats, and when a chat updates, I can only change that field and client can be notified of the change, then client can execute get query for the relevant chat and store that in the client side until next update. In this case we have less duplicated fields with the cost of extra get query from client side. What do you think about this?
If you update only updatedAt or with it lastMessge the cost will be the same because you update the doc anyway. You would have just more costs because of the additional get but no benefit of it at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.