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
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