在与Firestore的群聊中计算看不见的消息 [英] Count unseen messages in group chat with firestore

查看:100
本文介绍了在与Firestore的群聊中计算看不见的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪群聊中每个成员看不到的消息数.我将聊天元数据存储在 chats 集合中,并将每个聊天的消息存储在 messages/{chatId} 中.

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.

我考虑过要有一个新的集合 seenMsgTimestamps ,在其中存储某个用户在每次群聊中看到的最后一条消息的时间戳.在我的应用程序中,我将从该聊天的 seenMsgTimestamps 中找到的时间戳开始听 messages 的更改,并计算有多少新消息.

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?

谢谢.

推荐答案

我认为您可以继续使用此解决方案.为什么这个解决方案好?

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

我曾考虑过要有一个新的seenMsgTimestamps集合,在其中存储某个用户在每次群聊中看到的最后一条消息的时间戳.

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.

您可以通过创建一个新的集合来对数据进行非规范化,这对于NoSQL数据库来说是很常见的做法.对于您的特殊情况,我认为这是最好的解决方案.

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.

在我的应用中,我将收听针对该聊天的,从seenMsgTimestamps中找到的时间戳开始的消息更改

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.

这篇关于在与Firestore的群聊中计算看不见的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆