[Open Source] FirebaseService

FirebaseService
If anyone is interested in reading/writing data into Firebase’s Realtime Database, this is a module for you. I made it a while back and have been using it in my projects ever since, decided to open source it when someone messaged me about it.

This module is made to mimic the DatastoreService. It’s made so it’s easily swapable between
Datastore and Firebase Realtime Database just by adding .GetDatastore().
Understand how to use DatastoreService and this should look really similar.

The benefits of using FirebaseService is that you can get specific data from within the database JSON Object hierarchy. E.g. Firebase:GetAsync("Profile/Stats/Money").
It is also useful if you want to work on an web-app which players can use to check their stats or to check live events happening in the servers.

This module also supports Documentation Reader by @ProbableAI. More info within the module.

FirebaseService FirebaseService
	Functions:
		void FirebaseService:SetUseFirebase(bool);
		Firebase FirebaseService:GetFirebase(string name, string scope);

Firebase Firebase
	Functions:
		GlobalDatastore Firebase.GetDatastore();
		Variant Firebase:GetAsync(string directory);
		void Firebase:SetAsync(string directory, variant value, table header);
		void Firebase:RemoveAsync(string directory);
		number Firebase:IncrementAsync(string directory, number delta);
		void Firebase:UpdateAsync(string directory, function callback);
Acquiring Authentication Token

Since the module currently only supports the deprecated legacy database secrets key, the module can become unusable if Google decided to no longer support the Database Secrets key. Use at your own discretion.

Within the FirebaseService module:

--== Configuration;
local defaultDatabase = "https://example.firebaseio.com/"; -- Realtime Database address
local authenticationToken = "thisIsWhereYouWillPasteTheKey"; -- Database Secrets Key
Project Example

image

local HttpService = game:GetService("HttpService");
local FirebaseService = require(script.FirebaseService);
local database = FirebaseService:GetFirebase("Saves");

-- Set table
local newPlayerData = {Items = 10; Money = 200;};
database:SetAsync("12345678", HttpService:JSONEncode(newPlayerData));
-- Firebase>> <JSON> "12345678" : {"Items" : 10 "Money" : 200}


-- Get table
local playerData = database:GetAsync("12345678");
-- string playerData = '{"Items" : 10 "Money" : 200}'


-- Get individual data
local playerMoney = database:GetAsync("12345678/Money");
-- number playerMoney = 200;


-- Set individual data
database:SetAsync("12345678/Money", 50);
-- Firebase>> <JSON> "12345678" : {"Items" : 10 "Money" : 50}

If there are any issue, feel free to inform me about it or correct it for me with a code snippet in the comments.

This module is no longer maintained. You may modify or rework it to your heart’s content.

Enjoy~
MXKhronos

132 Likes

This is awesome, started using Firebase recently.

For any of you want to get into using Firebase, I highly recommend it. Only a small learnign curve and it really awesome!

7 Likes

Hello, I was wondering why you use firebase over other products like MySQL/Mongo/Cassandra behind a web API, Roblox’s native datastore. I would think it would be cheaper for a server, than paying for their realtime db.

On the other hand, great job! Good luck in the future!

2 Likes

Cool library. I have one of my own that I made a few years back, but this one might be better, just need to make typings for it for roblox-ts. I really wish someone would make an API for the new Cloud Firestore system though!

I think the main advantage of this, in my opinion, is to have an actual way to view all of your data at once, from any location you want and make easy changes, which is something DataStores can’t do (they’re like a black hole of data, in my opinion, you can’t see anything that’s going on).

5 Likes

Yea, I do wish they made it easier to see what is stored. You can use a program like Hedi SQL, or a web application to view any of these databases i.e PHPMyAdmin,or something like that.

3 Likes

For one, its far easier than setting up a self-hosted database. Firebase does it all for you. As well as that, it scales 100% automatically, so no matter the size of your game, it’ll work.

Also, what @jackTabsCode said; control. You have hardly any control over datastores. Obviously you can add and delete data via scripts, but you have no visual control. You can’t easily make changes to multple things are once, you can’t delete mutliple documents at once and there is no easy way to just see your data. Heck, there isn’t even a way (as far as I am aware) to get a list of all the items in your datastores!

Firebase is also easier than a VPS, for exmaple. There is no setting up servers and things like Nginx. Once again, it is all done for you. All you have to do is write your code, go to your console and type firebase deploy and your app is live.

4 Likes

myCenter can run on a 5$ droplet, I doubt any Roblox game needs a database with over 100 gbs, even then it’s a far stretch.

A server, for example, has more control, I can do anything I need to do, or want to do, with little fear that something can go wrong, I love to be self-reliant, anything I can do to stay away from someone else messing up, or making a bad update I love to avoid. Google has been having a LOT of cloud issues lately, so I really don’t trust them to host a platform like myCenter or host my data.

My main thing that would hold me back from using Firebase in my project, at least for main player data, like money, is the fact that there is no socket connection/no way to get when data is changed (DataStore’s OnUpdate), and constantly querying all of the data would cost some decent money with this service

That’s a fair argument, and I’m not saying that Firebase is superior. However, in my opinion, Firebase is still far more reliable than a 5$ VPS for hosting your data. It’s not about how much data is there, either. What if your game explodes with players and your VPS gets a huge spike in traffic and can’t hadle it? With Googles infrastructure, that isn’t much of a concern. And, also, Firebase is incredibly cheap if you look at the pricing. With Google, your app can literally scale to run hundreds of instances of its self.

Lets try to keep this debate as on topic as possible, as well.

Yep! Good news though, if you use node-js there is an **AMAZING LIBARY made by @Relism ** https://github.com/Reselim/rbx-engine.io
So with my own server, if I need to poll data I can just use a socket, tell the server: “Hey, poll for this player: Roblox, right now I think his/her balance is 0R$”, the server responses with “I acknowledge”, and then it polls, until the balance !== 0R$, once it does the server replies “Hey, the user: Roblox’s, balance has changed, the new balance is: 500R$”. [easiest way to explain, no json needed for an example :", and of course you can send socket events if the player does something.

Okay, let’s approach it that way. I own myCenter, we see a ton of network traffic because well applications. We store well over 60k applications, and somewhere in the neighborhood of 5-8k users [haven’t looked in a bit]. We store a TON of data, and we only have 80mbs of data, so I doubt anyone is going to have an issue using a 5$ droplet. I doubt that a Web API can’t handle a bit of data, that’s literally what they are built for.

Basiaclly what I’m saying: it’s very unlikely that anything will go wrong, and if your worried some serices even backup your data.

Okay, but how long has myCenter been a thing for? I’m talking about games that grow in size overnight. Constant reading/writing to the database can use a lot of recourses. Like I said before, I’m not talking about the size of the data, I’m talking about the amount of requests. Yeah, APIs are made for it, but at the end of the day, they are only as powerful as the machine that runs them.

Yeah, a simple VPS will probably be enough for the adverage Roblox game, but keep in mind the sheer speed Roblox is growing. And, if you’ve looked at Adopt Me recently, they were botted to 300k concurrent users (not confirmed it ws a bot, just speculation). It got to that amount of players in just a few hours, which is more than enough to crash a basic VPS (obviously this is a very rare thing to happen, but its just an exmaple)

1 Like

myCenter: 5 months. It has been online for one month, with no issue [no downtime]. These constant read and writes is what servers were built for, literaly. You get more control with your own database and don’t have to pay costly fees for accessing your own database. We are on V4/5/3 [it’s hard to explain], and we have gotten a ton of data at one time when announcing a new version, V4 gained a ton of data in one night, heck we do every day. We send a whole lot more data than a regular API would, also we store data in a way that does not require mountains of data.

That isn’t enough to crash a droplet. You need to realize this data is overtime, and if the API is written correctly, you should not even get near the Roblox limits [http-service]. Also, define “basic-VPS”, as that can mean anything. Can? It can crash a server, that’s like saying requesting HTML from a server can crash it, this is based on a multitude of factors.

You make some good points that I do have to agree with. However if you do look at Firebase pricing, it isn’t that expensive. They have a fixed and pay as you go plan, and depending on how much your api is used, pay as you go is often cheaper. Personally, I prefer knowing that my app will be running 24/7 without having to maintain a VPS. Each method has it advantages and disadvantages, for sure.

I’m going to end this debate here, though. It’s starting to go a little off topic to the OP and I’ve got to go for the night. Feel free to DM if you want to continue it tomorrow, though.

I really don’t have to maintain my VPS, other than making sure networking is logged, and making sure the server its self is updated and secured. It is also 5$ a GB of data that can be insane. I get 50GBs free with a droplet, and adding more storage isn’t expensive. I personally think myCenter is reliable because it runs on things I have trust built in.

Would love to use this, except I’m not at all familiar with Firebase. There’s not really a lot of user-friendly information given either, so I’m at odds of what to do.

1 Like

If you’re looking to get into using Firebase, they have some amazing documentation.

I used it a long time ago, but it’s no where near what it is now. Primarily the module doesn’t give all that good of a tutorial, as I have a collection with data in it but trying to retrieve it just returns nil. It shows there was a read on the usage but idk.

I’ve been considering Firebase a lot, but strayed away from it due to the only real public script for it having not been updated since 2015.

Thank you for your contributions.

it’s for realtime database, not cloud firestore.