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.
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.
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.
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.
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.
.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());
});
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With