Firestore聊天应用程序-构建消息主页 [英] Firestore chat app - build messages home page

查看:63
本文介绍了Firestore聊天应用程序-构建消息主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Firestore RTDB后端的Flutter聊天应用程序:

I have a Flutter chat app with Firestore RTDB backend:

messages (collection)
    message_1 (document)
        chat (collection)
            chat_1 (document)
            chat_2 (document)
        users (array, document field)
            user_id_1 (String)
            user_id_2 (String)
        user_info (map to store user info, like name, avatar etc)
    message_2 (document)
        chat (collection)
            chat_1 (document)
            chat_2 (document)
        users (array, document field)
            user_id_1 (String)
            user_id_2 (String)
        user_info (map to store user info, like name, avatar etc)

我想创建一个主页,在其中显示用户参与的所有聊天,并按最新排序,就像任何普通的聊天应用一样:

I want to create a home page where it shows all the chats a user is involved in, sorted by most recent, just like any normal chat app:

我知道如何显示用户参与的聊天.问题是,我不知道如何处理排序.有一种简单的方法可以执行此操作:每次发送新消息时,请使用云功能并更新消息文档中名为 lastSent 的字段,然后执行 orderBy('lastSent',降序:true)在您的查询中.问题是,每次发送消息时,您都必须执行两次写入操作,而不仅仅是一次更新该字段.有没有更好的方法来解决这个问题?

I know how to show the chats the user is involved in. Problem is, I don't know how to handle the sorting. There is one simple way to do this: each time a new message is sent, use a cloud function and update a field in the message document called lastSent, then do orderBy('lastSent', descending: true) in your query. The problem is, each time you send a message, you have to do two writes instead of one just to update this field. Is there a better way to handle this?

注意:我的应用程序不仅是聊天应用程序,还只是主应用程序的一部分.想象一下类似于Airbnb的聊天功能,因此聊天消息的数量或频率可能不会像Facebook Messenger那样大.

Note: My app is not solely a chat app, this is only part of the main app. Imagine a chat functionality similar to Airbnb, so the volume or frequency of chat messages may not be as large as Facebook messenger for example

推荐答案

常见的解决方案是按照您的建议进行操作:将 latest_updated 时间戳存储在每个聊天室"的文档中.

The common solution is to do what you propose: store a latest_updated timestamp in the document of each "chat room".

这确实意味着您需要执行两次写入而不是一次写入.但是,另一方面,您现在只需阅读聊天室"文档,而不用阅读其中的单个消息即可确定正确的排序.

That indeed means that you'll need to do two writes instead of one. But on the other hand, you can now determine the correct ordering with by just reading the "chat room" documents, instead of having read individual messages under it.

请注意,尽管当然可以使用Cloud Functions进行此操作,但这也可以直接从客户端完成.您甚至可以满足安全规则中的要求,即如果客户端还将相应的聊天室"文档的 latest_updated 时间戳设置为与客户端的时间戳相同的值,则该客户端只能写一条新消息.信息,尽管这会增加您为每条添加的信息再阅读一份文档的费用.

Note that, while it is certainly possible to do this with Cloud Functions, this can also be done directly from the client. You can even catch the requirement in security rules that a client can only write a new message, if they also set the latest_updated timestamp of the corresponding "chat room" document to the same value as the timestamp of the message, although this will incur the cost of one additional document read for each message you add.

这篇关于Firestore聊天应用程序-构建消息主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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