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?