2
votes

If I start reading documents from a Firestore collection using a query.onSnapshot() call, under what circumstances does Firestore automatically unsubscribe and stop reading docs?

Specifically, if the user closes the browser, or his computer crashes, what happens then? I presume that at some point Firestore will understand that the user is no longer there to receive the data, but how does that work?

I'm creating a PWA, so aside from the browser closing, my user might navigate 'within' the app. Do I have to make sure I meticulously unsubscribe from all open listeners or risk reading unused docs? Or is there some clever mechanism at work to 'garbage collect' abandoned onSnapshot listeners?

Since there is money involved, I'd like to know the specifics of all this. I'd rather avoid paying for document reads that I didn't need anymore.

Thanks!

1

1 Answers

4
votes

Since web page code can't run after a tab is closed, the listener will be stopped at that time. The query isn't managed by something persistent like a service worker.

That said, it's never a bad idea to be explicit about when to stop listening. I would consider that a best practice. Leaving listeners active for longer than anticipated is a good way to get billed more than you'd expect.