Reputation: 11
My Application is in development phase ... currently only have 5-8 users at a time. We have a few .onSnapshot listeners - total maybe 5 or 6. We are getting over the "free" 50K reads per day regularly. I am not sure what is driving the number so high. I am concerned that once we release the app and have 100's or hopefully 1000s or more users the reads will balloon and create significant costs. The underlying data is not changing that frequently.
What drives the number of reads? Is the onSnapshot listeners running every second, millisecond? How can we minimize the number of reads
Any help greatly appreciated.
Upvotes: 1
Views: 175
Reputation: 1420
OnSnapshot() listeners can represent a big chunck of your Firestore billing if not used properly and the more of them you have on your app the bigger the bill, so these should be used with caution, as you can see on this FAQ for Firestore:
When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated. You are also charged for a read when a document is removed from the result set because the document has changed.
Not to mention you would be charged with the initial set of your query every time you "start" to listen.
That being said, what could be happening in your particular case is that you are not detaching your listeners when you no longer need them, as you mention, you have 5 or 6 of them, I would reckon that they are likely not in the same "Page" of your app's execution cycle. As you can see in this community answer:
From the moment you call onSnapshot, Firebase will be listening for snapshot changes until you tell it to stop. Since your code never tells it to stop, the listener continues, even when you navigate away. Then when you navigate back to /Pie, you attach a second listener. So at that point your onSnapshot listener will be executed twice.
And you will have twice the cost, now let's multiply that by 5 or 6 times and this could be the reason you get as many reads as you do. I would advise you to detach your listener everytime you don't need them running anymore, you can find instructions on how to do that in this documentation
Upvotes: 3