Question

Can anyone help with analyzing how well this code work distributed across many people while being realtime? I want all the users to be able to update a realtime graph and send values to it.

Each client has a local counter variable:

var count = 0;

Sending a Value:

 $('#addValue').click(function(){
        fbValues.push({ x: count, y: randomNum(30,60)});
 });

Listening for a Child to be addded to firebase:

 fbValues.on("child_added", function(snapshot) {

        count += 1;

        var x_coor = snapshot.val().x;
        var y_coor = snapshot.val().y;

        data.values.push({ x: x_coor, y: y_coor });
        updateGraph();

    });

The issue is probably with the local variables of count and currently the graph works fine for most use cases, but when lots of people start sending data sometimes the count will become out of sequence

I looked up transactions for firebase and I can keep a total count on firebase accurate but I don't see how this will help clients increment their counters correctly.

Was it helpful?

Solution

Clients may call the transaction method directly.

var fb = new Firebase(URL);
var counterRef = fb.child('counter');
var valsRef = fb.child('values');

$('#addValue').click(function(){
    counterRef.transaction(function(currentVal) {
       return (currentVal||0)+1;
    },
    function(err, success, snap) {
       fbValues.push({ x: snap.val(), y: randomNum(30,60)});
    });
});

Locally, just listen on the counter path for changes:

var counter = 0;
counterRef.on('value', function(snap) {
   counter = snap.val() || 0;
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top