Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  100] [ 4]  / answers: 1 / hits: 22163  / 11 Years ago, fri, april 12, 2013, 12:00:00

I tried using presence to make it display the total connected users in an element. I can't really figure out how to make it work.


I also tried doing the following:


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

To tried to see if I could find anything useful in there, but I couldn't figure out how the data works.


Is there any way to accomplice what I am after? Fetch the total number of connected users and then echo it out in the console or to an element?


More From » firebase

 Answers
11

.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());
});

[#78940] Thursday, April 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
ignacio questions
;