如何与Firebase进行聊天 [英] How to structure chat with firebase

查看:67
本文介绍了如何与Firebase进行聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cloud Firestore(NoSQL)为以下用户存储个人资料信息:

I'm using Cloud Firestore (NoSQL) to store profile information for users like:

{
  "uid": "abc123",
  "name": "...",
  "friends": [
    "uid": "x234", 
  ]
  ...
}

现在,我想知道如何构建用户之间的直接聊天.我在想:

Now I'm wondering how to structure a direct chat from user to user. I'm thinking of either:

为每个用户文档添加一个附加字段,例如:

Adding an addional field for each user document like:

"chats": [
 {
    "from": "name",
    "message": "...",
    ...
 },
  ...
]

或者考虑不使用Firestore进行聊天,而是考虑使用具有类似结构的Firebase实时数据库.

Or instead of using Firestore for the chat, I consider using Firebase realtime database with a similar structure.

最后一种方法将带来好处,即用户文档不会因大量的聊天协议而膨胀".

The last approach would have the benefit, that the user-document would not be "bloated" with lots of chat protocols.

我需要一些建议,使其结构/实现最适合此用例.

I'd need some advice which would structure/implemention would suit this usecase best.

推荐答案

开始构建应用程序时,首先需要考虑最适合该应用程序的数据库.如果您考虑使用Firestore,则需要知道Cloud Firestore针对每个客户端每秒执行大量小读写操作的应用程序的定价模型可能比Realtime Database中类似性能的应用程序昂贵得多.

When you start building an app, first you need to think about the database which is most appropriate for it. If you think about Firestore, you need to know that Cloud Firestore's pricing model for applications that perform very large numbers of small reads and writes per second per client could be significantly more expensive than a similarly performing app in the Realtime Database.

这两个数据库之间也有一些区别.如果您想继续使用Firebase Realtime Database,则需要知道您无法查询多个属性,并且它通常涉及重复数据或客户端筛选,在某些情况下会有些混乱.Firestore不会自动缩放实时数据库.

There are also are a few differences between these two databases. If you want to go ahead with Firebase Realtime Database you need to know that you cannot query over multiple properties and it usually involves duplication data or client-side filtering, which in some cases is some kind of messy. Realtime Database does not scale automatically while Firestore, does.

关于如何为聊天应用程序构建数据库,您需要知道没有完美的结构可以做到这一点.您需要以一种允许您非常轻松高效地读取/写入数据的方式来构建数据库.Firebase 官方文档介绍了如何为聊天应用程序构建数据库.如果您想要更复杂的内容,请阅读这篇文章,构建Firebase数据对于复杂应用正确.

Regarding how to structure a database for a chat application, you need to know that there is no perfect structure for doing that. You need to structure your database in a way that allows you to read/write data very easily and in a very efficient way. Firebase official documentation explains how can you structure a database for a chat app. If you want something more complex, please read this post, Structuring your Firebase Data correctly for a Complex App.

为了更好地理解,我也建议您参加Firebase免费课程,

For a better understanding, I recommend you as well to take Firebase free courses, Firebase in a Weekend: Android.

因此,由您决定哪个更适合您.

So it's up to you to decide which one is better for you.

PS:如果您有兴趣,我也会在我的

P.S: If you are interested, I have also explained in one of my tutorials how you can create a Chat App using Cloud Firestore and Kotlin.

这篇关于如何与Firebase进行聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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