3

I want to track the number of unseen messages for each member of a group chat. I store chat metadata in a chats collection, and messages for each chat in messages/{chatId}.

I have checked other threads that ask about this, but in this scenario there's a group chat so it's more complicated. The threads that I have read suppose that it's a chat between two people.

I have thought about having a new collection seenMsgTimestamps where I store the timestamp of the last message that a certain user has seen for each group chat. In my app, I will listen to changes to messages starting from the the timestamp found in seenMsgTimestamps for that chat, and count how many new messages are there.

Is this a good approach or is there a better way of doing this?

Thanks in advance.

romin21
  • 1,532
  • 1
  • 14
  • 35

1 Answers1

1

In my opinion you an go ahead with this solution. Why is this solution good?

I have thought about having a new collection seenMsgTimestamps where I store the timestamp of the last message that a certain user has seen for each group chat.

You denormalize data by creating a new collection, which is a quite common practice when it comes to NoSQL databases. In your particulasr case, I think is the best solution.

In my app, I will listen to changes to messages starting from the the timestamp found in seenMsgTimestamps for that chat

That's also good because you are using a query on a limited data set and not on the entire collection, which means less reads, less money to pay but more perfomance.

Regarding the count, I recommend you also read the last part of my answer from this post. So you can also consider using Firebase realtime database for such conters.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Regarding the count part though, wouldn't it be ok to just count the number of messages starting from the `seenMsgTimestamp` of that chat in the client side, since I am already listening for changes starting from that timestamp? – romin21 Nov 18 '18 at 11:00
  • 1
    Yes it is but I also showed you an alternative. It's up to you to decide which one you want to use. – Alex Mamo Nov 18 '18 at 11:20