To Realtime or Not? | Get to know Cloud Firestore #10
Why Stack Overflow recommends it
Stack Overflow users link it when explaining how Firestore snapshot listeners behave, especially that only changed documents are billed and how the fromCache metadata works.
Recommended by 5 people across 5 different months, 2020 to 2022.
What it is
Episode 10 of Google's Get to Know Cloud Firestore series, presented by Todd for the Firebase channel. It compares realtime snapshot listeners with one-time fetches, covering their effect on app performance, battery use and billing, and how to structure an app around a realtime listener.
What people said
Recommends TouchVàmadute Meelugsana · 2020-04 · Stack Overflow · on “How to Firebase Firestore realtime update Only new data added to collect?”
I'm looking for this problem also but now i find out onAddSnapshotListener // as real time update. only get the new or changed documents [this video] Try to watch the whole series
Recommends Jasdev Singh Moun · 2020-05 · Stack Overflow · on “Firestore Query Listener”
Query listener always sends ONLY the documents that have changed but the firebase client SDK combines it with other documents and delivers them to you so that you don't have to write the logic for write specific code for different conditions. You should check the following video by Firebase team in which they explain "how to implement the real-time feature with firestore" [this video]
Recommends Louis Coulet · 2020-10 · Stack Overflow · on “Firestore query reads all the documents again (from database) when only one is modified even with persistence enabled”
I noticed this behavior and did some research: actually all is well! When you are listening to a collection of N documents, you perform N reads for the initial state, then 1 read when a documents changes, as expected. This is described nicely in this [this video] , and in the documentation [LINK] for the billing aspect. The trap is the fromCache metadata. When you receive an update for a document change, all the other unchanged documents have fromCache: false, but they actually come from the cache , as described by a Firebase dev in this SO answer [LINK] ! The fromCache metadata is true only in the scope of offline persistence, when the initial N reads were done from the local cache. When a docum
Recommends MarketerInCoderClothes · 2021-01 · Stack Overflow · on “Angular + Firebase firestore - inner subscription to document returns two values on initial subscribe”
Here is what I would do in your situation: Do a onSnapshot() query, (the first read operation) --> But limit results to 10 or 20. (cheap!) On user scroll -> load more. ( Firestore pagination ref [LINK] ) When a document change --> update the displayed data When listening for changes, you're only charged again for the documents that did change , not the ones you already queried that didn't get any updates. For more details on pricing for listeners, watch this video (especially at 3:16) [this video]
Recommends Sam Ramezanli · 2021-04 · Stack Overflow · on “The best way to design an infinite scroll list on backend”
Beside the the great response which Mark shared, I also found out that Firebase does the exact thing for us very efficiently and it's explained in great detail in the following video: [this video]
Cites newKidCoderr33 · 2020-04 · Stack Overflow · on “Are active listeners a good solution to populate UI changes once a day?”
Using Cloud Firestore as a database for my application. The labels/images I use in the app are updated about once a day , if that, and are populated from the data saved onto my Firestore database. I'm trying to weigh the pros of having a one-stop solution to my having my UI updated automagically and not having to deal with unreliable network on the user's behalf, and the cons of my application being a drain on both, the user's battery life and their data connection. There are no documentations I can find on this, and the only source I have is this Firecast video [this video] where I can find some confirmation for the former use case. I'm trying to figure out if I should go with manual fetches or
Cites Ian · 2020-08 · Stack Overflow · on “onSnapshot fromCache always false when change is local”
According to the description here [this video] I'd expect snapshot.metadata.fromCache to be true when the document being listened to is modified in the same client as the listener, e.g. The local .update(...) immediately triggers the onSnapshot handler (and is handed a snapshot with fromCache set to true ) The data is sent to the db The firebase client receives the return message and does nothing (does not trigger a onSnapshot ) because the server data agrees with the cache. Ergo, fromCache should always be true when onSnapshot is trigged by a local change. However, this only appears to be the case on the first two to three onSnapshot responses, after-which fromCache appears to always be false .
Cites Ian · 2020-08 · Stack Overflow · on “Cost of Firestore snapshot listeners activated by local changes”
"as each write hits the server, then causes a callback to the listener with that change." This does exactly align what what I understand for this official description: [this video]
Cites Alexander George · 2022-02 · Stack Overflow · on “Does it make sense to use Firestore Realtime as a way to interact with functions?”
I have a good old fashioned piece of code that takes some inputs, runs some code and produces outputs for a variety of tasks. This code runs in a function, and it should be triggered by a UI action. It is relevant to store a history of these runs per user, to list them in a specific view of the UI, and, on returning users, put them back where they left. Since the Firestore documentation and videos [this video] strongly encourage us to default to it for data exchange with the backend, I of course decided to entertain the idea before just moving on to simply implementing a callable. First, it made sense to me to model the data as follows: /users id /taskRuns id inputs outputs /history id date ...ou
Cites Sipan Hazim · 2022-07 · Stack Overflow · on “does Cloud Firestore realtime listener get all document from server when only one document change?”
@AlexMamo [this video] see minute 1:25 firestore documentation says that all document return but you get paid for one read .. but all document meta data says from sever so its a strange !