This is a performance question regarding firebase. I hope an expert would help me and I will try to make it as simple as possible.
I am building a Web HTML5 cards game (Very similar to Spades) where there are four players on the table and each player has to bid (between 7 and 13), I am going to call them:
- player1
- player2
- player3
- player4
So, they look like that on Firebase Realtime Database:
Supposedly now, player3 has bid from his side. The bid should be displayed on:
- player1
- player2
- player4
Now, what I do is that for each player, I create three listeners for the remaining players, so if we are on player4's side. The code is similar to this (in my code is done via for loop, but here changed for simplicity):
var player1Ref = firebase.database().ref('/table/1/bid/player1');
player1Ref.on('value', function(snapshot) {
if (snapshot.val()) window.alert("Player 1 has bid!");
});
var player2Ref = firebase.database().ref('/table/1/bid/player2');
player2Ref.on('value', function(snapshot) {
if (snapshot.val()) window.alert("Player 2 has bid!");
});
var player3Ref = firebase.database().ref('/table/1/bid/player3');
player3Ref.on('value', function(snapshot) {
if (snapshot.val()) window.alert("Player 3 has bid!");
});
Now, this works really fine for me. But I wonder if this is the best way to do it. The alternative would be creating a reference spy on "bid" but it might be worse because it fetches all the data whereas individual references get less data but they have more listeners.
Which way is the best way?
Aucun commentaire:
Enregistrer un commentaire