7
votes

I need to place an approximately 20 Vue component on the page. Each component, when mounted, creates an onSnapshot for itself and, accordingly, unsubscribe() when it is deleted.

const ref = firebase.firestore().collection('components').doc('comp1')

var unsubscribe = ref.onSnapshot(querySnapshot => {
  querySnapshot.forEach(doc => {
     ...
  })
}

What is the cost of each onSnapshot / unsubscribe :

  1. in the sense of the execution time
  2. in the sense of money (according to the price of Firebase)

How much will I pay, for example, if each of 1000 users update a page with 20 components 100 times in half an hour?

Perhaps it would be better to make onSnapshot on the whole collection of components?

1
If your concern is about reading/writing too many operations, you can switch to Firebase Realtime Database, as it only charges for bandwith and storage, contrary to Firestore whose cost depends on crud operations. See documentation here - Rodrigo Mata
Thanks, but I just recently switched to firestore from Realtime Database! Realtime Database does not suit me for some reason. I need to know if my decision is right or not. I would also like to understand the pricing of this particular solution, in order to determine whether it will be necessary to connect a paid plan or not. - Alexander
Do all of your 20 components need to be updated realtime? You could have a mixed solution alternating to fetching data once and only enabling realtime updates on components that really need to - Rodrigo Mata
That is, my decision is "expensive" and should not be done this way? Is it worth doing one onSnapshot for the whole collection of components? That's better? - Alexander
If all your data lives inside one collection, it would be a good approach - Rodrigo Mata

1 Answers

7
votes

Found the information you need in Firestore documentation

Listening to query results

Cloud Firestore allows you to listen to the results of a query and get realtime updates when the query results change.

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. (In contrast, when a document is deleted, you are not charged for a read.)

Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query

So every time you update a record in your collection, all the suscribed users will perform a read. You should take your time in estimating the amount of active users you are going to have and the amount/frequency of your data, that will help in taking the cheapest and best solution that suits for your app.

I'm also adding the default Firebase pricing calculator which is at the bottom at the page and will definitely help you to take a decision.