Reputation: 443
Im working on a group chat function in my react native app and therefore every person using my app or better said the chat function will have a listener attached to a firestore collection which holds all my chat messages.
The feature I want to implement is a counter of people who are currently in the chat, so basicly I want to count the numbers of listeners that are currently listening to the firebase collection.
One idea I had was to increment a counter in firestore and retrieve it as soon as you start to listen to the document and if you go back one screen, decrement it. If you join the chat, counter goes up, if you leave counter goes down. The problem with this method is that I dont handle any people loosing connection or closing the app while listening to the document. The increment would be done but the decrement not and therefore I would have a wrong number as my counter.
I wanted to ask therefore if anybody has any idea how to accomplish this task? Is there for example a possibility to execute a function on lost connection? I could imagine this beeing hard on the frontend so inside my app because as soon as I loose connection to the firestore... Im not able to retrieve or send and data anymore.
Upvotes: 1
Views: 288
Reputation: 83068
The feature I want to implement is a counter of people who are currently in the chat
I think you are looking for the presence mechanism that can be set with the Realtime Database and to which you can connect Cloud Firestore by using Cloud Functions.
Have a look at:
How To Build a Presence System (Firebase blog)
Upvotes: 1