使用附加的侦听器对Firebase RTDB或Firestore进行本地写入是否需要读取操作? [英] Do local writes to Firebase RTDB or Firestore with attached listeners cost a Read Op?

查看:56
本文介绍了使用附加的侦听器对Firebase RTDB或Firestore进行本地写入是否需要读取操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此Firestore官方指南中,它表示

In this firestore official guide , it says that

应用程序中的本地写入将调用快照侦听器 立即地.这是因为一个重要的功能称为延迟 补偿."执行写操作时,您的听众将 在将数据发送到后端之前收到新数据的通知

Local writes in your app will invoke snapshot listeners immediately. This is because of an important feature called "latency compensation." When you perform a write, your listeners will be notified with the new data before the data is sent to the backend

  1. 因此Firestore客户端(已发布此数据)将不会通过侦听器从服务器获取此数据?
  2. 这是否仍将作为读取操作"收取费用(如果未从服务器获取数据)?
  3. 这个概念是否也适用于实时数据库?

我用一个向/chats/chatRoomId/写入新对象的客户端以及在/chats/chatRoomId/上具有child_added侦听器的同一客户端对它进行了测试.我关闭了互联网,但仍然报告推送新数据正常(由于我认为是脱机功能),并且听众还报告接收到新数据(即使没有服务器访问权限)

I tested it with a client writing new object to /chats/chatRoomId/ and same client having listeners for child_added on /chats/chatRoomId/. I turned off the internet, still the pushing new data was reported OK (due to offline ability i guess), and listeners also reported receiving new data (even without server access)

我想构建我的聊天应用程序的数据库,以便在给定此处 由Firebase的@FrankVanPuffelen 推荐.

I want to structure my chat app's DB, such that i can cut cost on unnecessary reads (if there are any) in chat DB structure given here and here recommended by @FrankVanPuffelen from Firebase.

chats: {
  $roomId: {
    $messageId: {
      senderId: "..."
      message: "..."
    }
  }
}

示例:

请考虑进行1:1聊天,其中userA和userB(在不同的客户端上)位于聊天室中,并且都已向/chats/chatRoomId/添加了侦听器.现在,如果用户A在chatRoomId中推送了一条新消息,则用户A& B都将通过其侦听器接收到此新消息.这不是浪费userA的带宽和/或Read Op成本,因为他本人已将其推送,并且可能不需要从服务器获取它.

Consider a 1:1 chat, where userA and userB (on different clients) are in a chat room, and both have added a listener to /chats/chatRoomId/. Now if userA pushes a new message in chatRoomId, both users A&B will receive this new message through their listeners. Isn't this a waste of Bandwidth and/or a Read Op cost for userA, since he himself pushed it, and probably wont require fetching it from server.

如果本地写入不会使我为连接的侦听器读取服务器的费用,则我会将来自userA和userB的消息存储在与

If the local writes will NOT COST me a server read for attached listener, i will store messages from userA and userB in same path /chats/chatRoomId/ as recommended

否则,我想拥有一个数据库结构,以便userA仅侦听来自userB的消息.例如 用户A将用户B的新消息推送到/chats/uid_A/uid_B/,用户A将在/chats/uid_B/uid_A/上侦听,其中用户B正在为用户A推送新消息.客户将在本地合并按时间排序的两条路径,以获得一个聊天流.

Otherwise, i want to have a DB structure so that userA will only listen to messages from userB. e.g. userA will push new msg for userB to /chats/uid_A/uid_B/ and userA will listen on /chats/uid_B/uid_A/ where userB is pushing new msgs for userA. And client will locally merge the two paths sorted by time, to get one chat stream.

这将可能为我节省50%的读取,并节省大量的Firebase账单.

It will potentially save me 50% of reads, and Huge savings on firebase bill.

在03/05/2019进行

添加图片以使问题更清晰:

Adding a picture for clarity of question :

推荐答案

  1. 因此Firestore客户端(已发布此数据)将不会通过侦听器从服务器获取此数据?

在本地写入一些数据时,将立即调用快照侦听器.这意味着,由于您添加了一些新数据,因此将立即通知您的侦听器.但请记住,此操作是在将数据发送到Firebase服务器之前进行的.

When you write some data locally a snapshot listener will be invoked immediately. This means that your listeners will be notified right away because you have added some new data. But rememer, this operation is happening before the data is sent to the Firebase servers.

  1. 这是否仍将作为读取操作"收取费用(如果未从服务器获取数据)?

不.只要您已缓存数据并且服务器上没有任何更改的文档,Firestore中就不会向您收取任何读取操作"费用.

No. As long as you have cached data and there are no changed documents on the server, you won't be charged with any "Read Op" in Firestore.

  1. 这个概念也适用于实时数据库吗?

不,不是. Firebase reltime数据库对于定价具有不同的概念.

No, it's not. Firebase reltime database has different concept for pricing.

我想构建我的聊天应用程序的数据库,以便在给定此处,由Firebase的@FrankVanPuffelen推荐.

I want to structure my chat app's DB, such that i can cut cost on unnecessary reads (if there are any) in chat DB structure given here and here recommended by @FrankVanPuffelen from Firebase.

这两个示例都是针对Firebase实时数据库的,都说明了创建聊天应用程序的一种很好的方法.如果您考虑在某个时候尝试将Cloud Firestore用于聊天应用程序,则可以在此处找到有关如何创建完整且实用的

Both examples are for Firebase realtime database and both explain a very good way for creating a chat app. If you consider at some point to try using Cloud Firestore for a chat app, here you can find a tutorial on how to create a complete and functional Firestore Chat App.

这篇关于使用附加的侦听器对Firebase RTDB或Firestore进行本地写入是否需要读取操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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