1

I have a webapp where users can join a variable number of chatrooms. The firestore collection looks like this:

chatrooms: {
   "g4HL09vHfkaO3": {
     "name": "Chatroom 1",
     "messages": ...
   },
   "lpScgY74gHJ87": {
     "name": "Chatroom 2",
     "messages": ...
   }
}

When the user joins a chatroom, the webapp will attach a listener to the given document in order to get the messages and receive incoming ones, e.g.:

unsubscribe = firebase
  .firestore()
  .collection("chatrooms")
  .doc("g4HL09vHfkaO3")
  .onSnapshot( ... )

and when the user leave the chatroom, the app will detach the listener like this:

unsubscribe()

Now my goal is to display in the app the amount of users currently inside a given chatroom, so the question is:

  • is it possible to get from firebase the global number of listeners attached to e.g. chatroom g4HL09vHfkaO3?
  • if not, what is the most straightforward way to achive what I want?
etuardu
  • 5,066
  • 3
  • 46
  • 58

2 Answers2

1

Firestore has no public API that return the number of listeners to a collection. If you want to know the number of people in a chat room, you can either:

  1. Keep a counter in each room document that each user increments and decrements.
  2. Use the Realtime Database for building a presence system for each room. This is described in the documentation as the solution: https://firebase.google.com/docs/firestore/solutions/presence
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The problem with the counter, as far as I understand, is that it would not get decreased on client crashes or other failures. I was hoping to find something more easy to implement than the synchronization with the Realtime Database, but I see that it is for now the canonical way. Thank you for your reply! – etuardu Jun 19 '20 at 14:49
  • 1
    Indeed, the dirty disconnects can essentially not be handled well with Firestore's connectionless wire protocol. That's why the recommended solution uses Realtime Database, which uses Web Sockets under the hood where the detection is feasible. – Frank van Puffelen Jun 19 '20 at 15:28
0

There is the way where you can get the number of objects attached to the parent.

You listen to the collection chatrooms and you can use the method called numChildren() provided by the firebase to get the total size of the list of users that are using the chatroom.

The link for reference where it is explained in detail is: reference

Minal Shah
  • 1,402
  • 1
  • 5
  • 13
  • Thanks for your answer, could you elaborate more? I don't have list of users Inside my ```chatrooms``` collection, so I don't understand how your solution applies – etuardu Jun 19 '20 at 13:46
  • There is no need of having list. This function considers all your object under the parent hierarchy. – Minal Shah Jun 19 '20 at 13:52
  • From what I read in the documentation (https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#numchildren), this seems to be unrelated to my needs. Using that method I could get the properties of the chatroom document, i.e. 2 (name and messages), but not the number of users that are listening on snapshot changes. – etuardu Jun 19 '20 at 14:00
  • When you listen upto the user that is g4HL09vHfkaO3 and then use numChildren it will read name and message but if you read upto chatroom collection and use numChildren then you get the number of users. – Minal Shah Jun 19 '20 at 14:03
  • Hey Minal. The link you provide is to the documentation for the Realtime Database, while etuardo is using Cloud Firestore. While neither providers a built-in count operator, the API and docs are different. – Frank van Puffelen Jun 19 '20 at 14:37