I am trying to understand the cost of using Firestore realtime listeners. In my case it is for an editor (realtime collaborating).
Words in an editor may correspond to documents in Firestore. And a document in an editor may be a collection in Firestore.
There may be thousands of words in a document in the editor.
Each time a user opens a document in the edtor a snapshot for the corresponding collection must be set up, something like this (https://firebase.google.com/docs/firestore/query-data/listen#web_3):
db.collection("cities").where("state", "==", "CA")
.onSnapshot(function(querySnapshot) {
var cities = [];
querySnapshot.forEach(function(doc) {
cities.push(doc.data().name);
});
console.log("Current cities in CA: ", cities.join(", "));
});
I am a bit worried about the cost for this call. And I can see no way to charge users for this. (I think I read some years ago that the cost for this call was very low, but if I remember correctly now it has changed.)
Can someone clarify this for me, please?
EDIT: Trying to make my question more clear.
- Let us say that there a 5000 words in a document in the editor.
- This will be 5000 Firestore documents.
- Opening the document in the editor will call the Firestore to get a snapshot and setup a listener (see code example above).
- As I understand it this will now be charged as reading 5000 Firestore documents.
My question is about point 4 above. Do I understand that correctly?
And what happens when the users reopens this editor document? Will there be a charge for 5000 Firestore documents each time?
(I am expecting the users to open each documents many times due to the nature of the app I am writing.)
EDIT 2: Since I believe it is now clear that the code above actually will be charged for 5000 reads (see point 4 above) I think I now can ask the real question without raising too much confusion:
How is this handled when firebase.firestore().enablePersistence()
has been done?
Is there any point where this can be alleviated? It looks to me like Firestore collection
might be such a point. (Last change time could be stored for a collection, perhaps. I am not sure. But I think this must be done on the Firestore side, not in my code, or?) Is something like this implemented in Firestore?