1

I'm attempting to make a group chat app and I want to implement the feature where it detects the number of active users in a chat room. I was thinking of updating the database with the structure below, in the viewWillAppear and viewWillDisappear functions. For example, in viewWillAppear, I would update the current user value into the chatroom node, and in viewWillDisappear, I would delete the user value; however, I was wondering if this is the most efficient way to go about this since it would require a lot of database fetching.

active_chatroom_users
    - chatroomId
         - UserId:1
         - UserId:1
Eric
  • 199
  • 1
  • 11
  • Have a look at Firebase's connection state: https://firebase.google.com/docs/database/web/offline-capabilities#section-connection-state and the sample presence app: https://firebase.google.com/docs/database/web/offline-capabilities#section-sample – Frank van Puffelen Nov 06 '18 at 21:57
  • I checked out your answer in this post: https://stackoverflow.com/questions/37403251/how-can-i-track-the-current-number-of-viewers-of-an-item. I was wondering what the updated code would be for setting up a value listener – Eric Nov 07 '18 at 22:57

1 Answers1

0

Firstly, you may not want to use viewWill... because it doesn't always mean the following viewDid... will occur. Instead use viewDidAppear and viewDidDisappear. There isn't a problem with creating and deleting the user in the chat room every time the view appears and disappears, however, you're right in thinking that there's one in fetching the chat room users for the count.

You should maintain a count yourself by incrementing and decrementing each time a user is added or removed from the active_chatroom_users. This can be done on the client in several ways, one of which involves transactions. It will allow you to update the active user count in realtime and will ensure that any other users becoming active at the same time will also be counted.

However, I would prefer using a Cloud Function trigger that recognizes when a user becomes active or inactive in a chat room, and increments or decrements the total active user count for a chat room.

Callam
  • 11,409
  • 2
  • 34
  • 32
  • So just to clarify it is fine for me to update the child values inside the database within viewDidAppear/Disappear, but I should be using Firebase Transactions or Cloud Functions to observe the database for those values. Your answer was very helpful, thanks! – Eric Nov 07 '18 at 16:13
  • Yes, however transactions would also be used in the Cloud Functions. Using the Cloud Functions will make the logic on the client simpler as it only has to worry about adding and removing the current user from `active_chatroom_users`. Also means your data rules can be a lot simpler. – Callam Nov 07 '18 at 16:27
  • I'm fairly new to coding, so I haven't had any experience with FB Transactions or Cloud Functions. I'm wondering what the issue would be if I set up a firebase database observer that listens for values added – Eric Nov 07 '18 at 19:18
  • I just had the thought, you would have issues if you only used the view lifecycle methods to update the active user. If the application enters the background or foreground, `viewWillAppear` or `viewDidAppear` will not execute – https://stackoverflow.com/questions/15864364 – Callam Nov 08 '18 at 01:57
  • You don't want to observe the `active_chatroom_users` from a client so you can adjust the total active user count. Where ever you choose to keep the count that you must maintain, you should observe that location on the client to show a realtime active user count for example. – Callam Nov 08 '18 at 02:00