Home > database >  Get the number of Firestore snapshot listeners on a document
Get the number of Firestore snapshot listeners on a document

Time:01-06

I want to know how many active snapshot listeners are on a document at any given time. Ideally, a cloud function that can monitor the count, and update the document with a counter. This counter could then be displayed on the client to show how many people are currently active.

Example: Chat messaging app where you can see how many people are actively in the chat room.

CodePudding user response:

Firestore does not have any built-in functionality for this, so you'll have to implement your own.

A simple way would be to increment a counter when you attach a listener, and decrement it when you detach it. The main concern I have there is that the number won't get decremented if the app crashes or if you lose the network connection, as in that case your code to decrement it won't run or be able to write. Because of this, you could consider storing the counters in Firebase's Realtime Database, and then use onDisconnect handlers to deal with the loss of connection.

  • Related