Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - Count online users

Tags:

firebase

I tried playing about with presence to make it display the total connected users in an element. I couldn't really figure out where to look. I did something similar to this:

var dataUlist = new Firebase('https://<url>.firebaseio.com/.info/connected');
dataUlist.on('value', function(snap) {
    console.log(snap);
});

To try and see if I could find anything useful in there, but (I kinda expected it) I couldn't make any sense of the data.

Is there any way to accomplice what I am after? Fetch the total number of connected users and eg. echo it out in the console or to an element? Or maybe how to fetch a list of authorised users and non-authorised?

EDIT: I would like to keep my spelling in English, I am not American. Thank you.

like image 655
MrE Avatar asked Apr 12 '13 23:04

MrE


People also ask

Can firebase handle 10 million users?

The limit you're referring to is the limit for the number of concurrently connected users to Firebase Realtime Database on the free Spark plan. Once you upgrade to a payment plan, your project will allow 200,000 simultaneously connected users.

How many users can use firebase?

There's a limit of around 30 client IDs that can be created within a single project. You should ensure that all Firebase Apps within a single Firebase project are platform variants of the same application from an end-user perspective.

How do I check my firebase usage?

To see your current Realtime Database connections and data usage, check the Usage tab in the Firebase console. You can check usage over the current billing period, the last 30 days, or the last 24 hours.

Is firebase free for lifetime?

Firebase offers a no-cost tier pricing plan for all its products. For some products, usage continues at no cost no matter your level of use. For other products, if you need high levels of use, you'll need to switch your project to a paid-tier pricing plan.


2 Answers

.info/connected will only return information about whether the current client is connected or not. In order to maintain a presence count, you'll need to create a counter by storing presence information for each user and utilizing setOnDisconnect(). For example:

var listRef = new Firebase("https://<url>.firebaseio.com/presence/");
var userRef = listRef.push();

// Add ourselves to presence list when online.
var presenceRef = new Firebase("https://<url>.firebaseio.com/.info/connected");
presenceRef.on("value", function(snap) {
  if (snap.val()) {
    // Remove ourselves when we disconnect.
    userRef.onDisconnect().remove();

    userRef.set(true);
  }
});

// Number of online users is the number of objects in the presence list.
listRef.on("value", function(snap) {
  console.log("# of online users = " + snap.numChildren());
});    
like image 75
Anant Avatar answered Nov 02 '22 06:11

Anant


Here is the the code from Anant formatted for Android

public void getDbCount() {

    Firebase listRef = new Firebase("https://<your-firebase-database>.firebaseio.com/presence/");
    final Firebase userRef = listRef.push();

    // Add ourselves to presence list when online.
    Firebase presenceRef = new Firebase("https://<your-firebase-database>.firebaseio.com/.info/connected");

    ValueEventListener myPresence = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            // Remove ourselves when we disconnect.
            userRef.onDisconnect().removeValue();
            userRef.setValue(true);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Log.e("DBCount", "The read failed: " + firebaseError.getMessage());
        }
    };

    presenceRef.addValueEventListener(myPresence);

    // Number of online users is the number of objects in the presence list.
    ValueEventListener myList = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            // Remove ourselves when we disconnect.
            Log.i("DBCount", "# of online users = " + String.valueOf(snapshot.getChildrenCount()));
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Log.e("DBCount", "The read failed: " + firebaseError.getMessage());
        }
    };

    listRef.addValueEventListener(myList);
}
like image 24
Tony C Avatar answered Nov 02 '22 04:11

Tony C