Hello All,
I'm working on an app where I have to display how many users are online at the same time. I'm using firebase on-screen open I increase value by one and save this value to firebase. but I'm not getting the expected result. Please help. Here is my code
Could you please describe the unexpected result You are getting.
Mostly nothing seems wrong with blocks , One place you must check is The FirebaseUrl is set correctly or not.
Probably will get sorted , As what you have done here is you have called get value and store value one after another , so basically what is happening is the value is being stored even before it is retrieved from the realtime database , as the initialise global count = 0 The value which is getting stored is 0 + 1 instead of retrieved get value + 1
Keeping the count centrally is hard, because users fall all over themselves trying to update it.
Instead, devote a Firebase DB to just keeping tags of all the unique identifiers assigned to users when they run the first time. SystemTime in ms is a good candidate, just store it in TinyDB the first time.
When entering the app, add the user's unique tag to the Firebase DB.
On exitting the app, remove the tag from the Firebase DB.
To get the online count, get the tag list from that FireBaseDB, and take length of list on that.
This should work pretty well, except for users who manage to leave the app without hitting the tag delete code. Maybe flush the DB nightly?
P.S. Database programmers call this an atomic update
.
I know this is an old thread .. I found it because I am wanting to add the same function in my app. Reading the above method made me think, what about somehow using the FirebaseDB1.DataChanged block to write a users unique tag somewhere.. and then after X amount of time remove it. This way it would seemingly be semi-real time.
If a user is active then the data will change and write his tag.. if he lurks and someone else changes the data it still writes his tag. Then after lets say 5 minutes purge the user and start over.. something like that? Would that work?
Different approaches would scale differently as the number of users and their activity levels increase. The wrong approach could slow down your app and raise your Firebase bills.
I have heard Google offers services to track usage of apps, if you want to play in the big leagues.