Count unseen messages in group chat with firestore

余生长醉 提交于 2021-02-08 03:31:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!