firebasegoogle-cloud-firestoregeofirestore

How exactly does the snapshot listener metric in Firestore work?


Here's what I see in my Firestore usage dashboard for snapshot listeners

I am building a flutter app, and I am using a stream to subscribe to Firestore query results. Unfortunately, I don't understand how the snapshot listener metric works, combined with this tidbit from the documentation:

https://firebase.google.com/docs/firestore/best-practices#realtime_updates

Does the 27 peak mean I've somehow created 27 listeners (when I should be making one)? I'm confused.

Also to note that I have made 0 interactions with my app, just left it running..


Solution

  • In Firestore, every time you add the onSnapshot method, you're creating a new Snapshot Listener for this user. So according to you print, you have 1 user connected to your app (1 Active Connection) and this user has 27 listeners attached to it. So it seems that either you're getting real time updates from 27 Firestore queries, or you're not closing your connections.

    From the 3rd reference, from Firebase docs:

    Let's say a user opens your app on their phone. The app then connects to Cloud Firestore and subscribes to 10 queries. This increases your metrics by 1 active connection and 10 snapshot listeners.

    Is it bad?

    Even though you're not paying for those active connection or the listeners, every time a doc is read / updated, this will count as a new read, and those are charged in the end. So using way too many listeners is not the best practice.

    What to do?

    The first thing I'd say is to double check if you're not keeping the listeners opened once the component / view is closed. If you find any, don't forget to detach them.

    References