问题
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.
回答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.
来源:https://stackoverflow.com/questions/53359694/count-unseen-messages-in-group-chat-with-firestore