2

A game based of firebase where every user push a request to play on the same waitingList so that a nodeJS server prepare a round game for every two waiting players separately. I am using Firebase JS v3 on nodeJS:

    var ref = firebase.database().ref();

//this is where we populate rounds
var gamesRef = ref.child('games');

//users path
var usersRef = ref.child('users');

//waiting queue
var waitingRef = ref.child('waitingList');

//listen for each new queued user
waitingRef.on('child_added', function(snap){ 
  waitingRef.transaction(function(waitingList){
    if(waitingList !== null){
      var keys = Object.keys(waitingList);
      //take two players
      if(keys.length >= 2){
        //prepare a new round for users
        var round = prepareNewRound(waitingList[keys[0]], waitingList[keys[1]]);

        //remove queued users from waitingList
        waitingList.child(keys[0]).remove();
        waitingList.child(keys[1]).remove();

        //create new game
        var roundKey = gamesRef.push(round).key;

        //notify users about the new game created
        users.child(waitingList[keys[0]]).child('current_round').set(roundKey);
        users.child(waitingList[keys[1]]).child('current_round').set(roundKey);
      }
    }
  });
});

The waiting list is highly requested by users that could exceed 10k simultaneous real time users. This way is performing faster than Firebase-queue but have repetitive slowness of 1 minute every 5 minutes where the waiting list become longer and longer before becoming normal later. Is there any optimisation that could be made to the concept?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
iouhammi
  • 1,108
  • 15
  • 29
  • You don't appear to be using `transaction` correctly. From the [docs](https://firebase.google.com/docs/reference/js/firebase.database.Reference#transaction): "If undefined is returned (i.e. you return with no arguments) the transaction will be aborted and the data at this location will not be modified." You are supposed to return the mutated data; you're not supposed to perform operations with other refs; such operations won't be part of said transaction anyway. – cartant Dec 17 '16 at 04:03
  • Hi cartant, yeah that s true. But the waitinglist is really high requested and it works smooth on traffic of 7k simultaneous users but once overpassing 10k and above it turn performance down and the slowness afffect the whole firebase operations – youhammi Dec 17 '16 at 18:22

0 Answers0