firebase how many current logged in user in my app - android

So i have app with Firebase email Auth
I want to keep track of how many users are actively logged in right now
A number in firebase database that can keep changing or list of active users right now!
They are removed moment user logs out
We cant exactly do addition in firebase database so how to do that.. any help?
PS: I dont want to use Analytics, i want a database solution

You need to use firebase StreamView : RealTime Analytics
Below are the details
https://support.google.com/firebase/answer/7229836?hl=en
http://firebase.googleblog.com/2017/03/realtime-analytics-for-everyone.html

For real hardcore database solution you can do the following:
You can add a last_login field in your database users collection. Update that field whenever a user resumes your app. Then you will be able to query once or Live Stream about how many users are logged in the last several minutes.
If you are looking for something much more accurate you can update the user last_login every several seconds from your application and Live Stream the last results.
Note that this may cost you a lot of money since firestore is a pay per action database, Which means that you pay for each update and each read from your database.
You can also checkout #Frank van Puffelen comment bellow your answer.
Enjoy!

Related

Get data entry count using firebase analytics

I am new to Firebase Analytics and I went through some online tutorials to learn about it. I have a simple 'Notes' app (Android) in market, which allow users to create and save notes offline on their device.
It would be great, if I could get a total number of notes that have been created till date with my app. I know, I can add an event which will notify Firebase when user creates a new note, but what about the existing notes that are present in the database?
Is there any way to get such statistics through Firebase Analytics?
UPDATE:
I have included a user property as notes_count, but I am not able to find a way to see the total. How to do it?
You could set a user property with the number of notes the user has created instead of or in addition to logging the event. You are limited to 25 different user properties total though, so use them with care.

Firebase database structure - storing user activity in order to customize views and permission

Working with Firebase for the first time and looking for advice of setting up the right structure for my project which is basically an "offers/coupon" type starter project.
The scenario is this:
I have a node containing a list of all offers available to users
This list of offers is displayed to users after successful Firebase authentication
When a user redeems an offer, I want to be able to count/record that activity in their child node under user and hide that offer so that they cannot see it again once used.
My question is what would be the best way to do this given that offers may be added, may expire, or may change at some point in the future. So, in effect, the user should receive the list of most updated offers, minus the ones he/she have used in the past.
a) would it be more effective to have a master list of offers, and then run a cloud/server function to clone this list for each new user an track that way
Firebase Structure 1
or
b) Keep a master list of offers in one node, then track user specific offer usage
Firebase Structure 2
Appreciate your guidance
The second solution is better because you'll save bandwith. This practice is called denormalization and is a common practice when it comes to Firebase. The first solution is not good becase every time you want to display the users you donwload unnecessary data. If you want to read more details about how you can structure a Firebase database in a efficient way, please read this post, Structuring your Firebase Data correctly for a Complex App. Also, you can take a look a this tutorial, Denormalization is normal with the Firebase Database, for a better understanding.
Second solution is much good. Because in first one we are having redundancy of data in our database. And second one obviously removing that cause.
But instead of using true or false because it is only showing you, "it's available or not", so you can use a string type parameter as "expired", "going to expire" and "updated" or whatever sooo. So it. Will be able to trace all you information related to offer for particular user. I think this is your requirement also.
Happy coding.

How to get the number of users logged in in the firebase database (Without addValueEventListener() )?

I have been looking for this quite some time now and i haven't found any answer related to my problem, so before you tag my question duplicate, at least read it first.
First i have just started firebase and only know basic things about it, i am trying to make a simple single user (1 to 1) chat app.
I want to get the number of users logged in in the database.
I have a child to the root of my database called users which have the list of the users who have logged in.
I know about the datashot.getchildrencount() but that works when some update/event happens, but i want it to give me the number of users whenever i want, not only on some event (For example in messenger number of current active users are shown continuously,i dont want active user tho, i just want total logged in users).
I thought of the another way to make a child called NumberofUsers so i store number of users there but then firebase dont allow to getvalue of the child,only set value (it does allow to get value but only on some event). Any idea what should i do?
UPDATE
i thought of another way to do this, though its not working at the moment but i think solving it is easier than thinking of another method to solve my original problem.
so I made one more child of NumberofUsers with key "02" and random value. now everytime i want data of "01" i change the value "02" after enabling the addChildEventListener for "NumberofUsers". Code that does this. Error its giving me. .new database.
Apparently I cant access child data using datasnapshot, any solution?
Any idea how to solve either of this?
Thank you.
Unfortunately, there is no way in which you can read data from a Firebase Database without attaching a listener. Regarding Firebase, everything it's about listeners. In your case, i think you want a callback to be called once and then immediately removed. If so, i recomand you using addListenerForSingleValueEvent() method to simplify this scenario. It triggers once and then does not trigger again.
This is useful for data that only needs to be loaded once and isn't expected to change frequently or require active listening.
Hope it helps.

Firebase Realtime Database Limit Exceed

I want to know what will happen when the limit is exceeded for Firebase Realtime Database. The thing is I know the maximum number of connections I can have is 100 only. Now, suppose my android app has active 1,000 users and I implement Realtime Database. Then the first 100 users will get connect to the database. I want to know what will happen to the user number 101, how I know the connection is unsuccessful, is there any Exception that is thrown or some function isSuccessful(). How to handle that kind of thing in my app.
I have already seen: Link Link
I want to know how to tell the app that it is not connected and when any user disconnects, do I need to run init statement again and when?
Thanks in Advance :)
As found here from a Firebase developer
There is no current way to detect this programmatically. An error
message will be asynchronously thrown in the developer console when
the 51st connection is made.
But this post is from 2014 so maybe that changed.

Querying Firebase is becoming painful day by day Android

Working with Firebase from past 3 months made my data "fan-out" and managing redundant data day by day I am feeling as if I am over coding now.
I am typically making logging app. Logging Hours minutes. Here what it does.
Every user signup trough Google authentication.Data is saved in "/users/[users]"
Then they further create some Helper or workers who's time is been logged.
Workers are saved as "/workers/userID/[workers]
Logs : A worker is selected and his logs are added in two places
/logs/userId/[logs] (this is for calculating monthly stats)
/workder_logs/workerId/[logs] (this is for calculating per worker based stats)
This works fine for me..
But now I am taking this app from productivity app to collaborative app.
Where users can connect with each other and request or approve logs
Logs added by one users is reviewed and approved/rejected by other user.
This is what happens when a log in this new logic
User select a worker again which is now linked to a userid (typically linkedUser field in Worker class)
Log is added to /logs/userid/
Log is added to /worker_logs/workderid/
Log is added to /logs/otherUserId/
And once he approve it is added to /worker_log/otherUserWorker/
But before that I need a notification screen. This is where I am getting frustrated by firebase after so many days of work(I am also working on some other project with Firebase).
I want to query this for a single recyclerview.
Select logs from /logs/myUserid/ where logs.date is between 1dec16 to
31dec16 and { (logs.createdby="not me" and logs.status=unapproved) or
(logs.createdby="me" and logs.status=rejected) }
Point is I do not want to maintain further logs redundancies(putting in different buckets namely /log/approved etc and manage them). I am not getting any breakthrough when it comes to querying data in Firebase.
I also feel documentation is not so much helpful.

Categories

Resources