Let's say that I wanted to make a massive, realtime, 2d open-world style game that runs entirely in the browser and I wanted to use Firebase to do it.
Let's also leave aside questions about security; I'll ask those later. ( =
Players start at (0,0) and can move in any direction. I spatially hash the world so that any given x,y coordinate becomes a single (or nested) key that I can use to generate a Firebase ref:
var getKey = function(x, y) {
return Math.floor(x / 100) + ':' + Math.floor(y / 100);
}
var key = getKey(currX, currY);
var ref = new Firebase('https://whatever.firebaseio.com/world/' + key);
// ...
Something like that. As the player moves around in the world I'm pretty sure I'd need to keep somewhere between four and nine refs to Firebase alive to get changes. This could double or triple depending on how I structure the data, too: are other players stored in the world/ tree, or elsewhere? That kind of thing.
I don't want the player's browser to get updates it doesn't care about. I'd like to "expire" old refs as the player moves around so the browser isn't spending resources chatting about faraway slices of the world.
Is it enough to let the ref instance go out of scope and get GCed? Or is there something else I'd have to do to notify the ref that I don't want to use it anymore?
Another thing I thought about was not worrying about how many refs there are but tracking on callbacks instead. As the player moves out of the range of a particular section of the world I could off any callbacks I'd set up. Would that be sufficient to get my refs to stop talking to Firebase?
Firebase keeps only one open connection (using web sockets) to handle the communication to a single Firebase instance, even if you create multiple refs for it. So just like you said - as long as you 'off' any events on the inactive refs, you should be good.
You may wish to consider enabling Firebase debug logging during development. It will let you see all the communication that takes place between the browser and their server. This way, you will be able to verify that the client is getting only the necessary updates. To enable debug logging, add the following line anywhere before you create the first Firebase ref:
Firebase.enableLogging(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With