Firebase Realtime Database Documentation
Firebase Realtime Database Documentation
Firebase Realtime Database Documentation
Overview
Setup
Before starting to use any Firebase extensions, you are required to follow some initial
configuration steps.
NOTE The SDK version is only available for the Android, iOS and Web targets; if you're
targeting other devices, please follow the steps for REST API.
Functions
The following functions are given for working with Firebase Firestore extension:
• FirebaseRealTime (entry point)
• <dbEntryRef>.Child
• <dbEntryRef>.Delete
• <dbEntryRef>.Exists
• <dbEntryRef>.Listener
• <dbEntryRef>.ListenerRemove
• <dbEntryRef>.ListenerRemoveAll
• <dbEntryRef>.Parent
• <dbEntryRef>.Path
• <dbEntryRef>.Push
• <dbEntryRef>.Set
• <dbEntryRef>.Read
• The functions given below should only be used for filtering data when
reading from the database using the Read() function given above.
• <dbEntryRef>.EndAt
• <dbEntryRef>.EqualTo
• <dbEntryRef>.LimitToFirst
• <dbEntryRef>.LimitToLast
• <dbEntryRef>.OrderByChild
• <dbEntryRef>.OrderByKey
• <dbEntryRef>.OrderByValue
• <dbEntryRef>.StartAt
Create
Project
Before working with any Firebase functions, you must set up your Firebase project:
4. On the next page, make sure that Enable Google Analytics for this project is
enabled and then click the Continue button:
5. Select your account and click the Create project button:
6. Wait a moment until you project is created; after a few moments you should see
the page shown below:
7. You will now be taken to your new project's home page:
Firebase
Console
Before being able to use the Firebase Realtime Database extension we need to configure a
new database to work with in the Firebase Console. Follow the steps below to get your
first database set up.
3. Select Start in test mode (otherwise you will need add your own rules for
production mode) and click on Next.
4. You are now ready to start using Firebase Realtime Database extension.
Platform
Setup
Firebase Realtime Database implementation uses both SDK (working on Android, iOS and
Web) and also REST API that allows it to work on any other platform. In this section we will
cover the steps necessary to start using the Firebase Realtime Database extension on your
game.
Select your target platform below and follow the simple steps to get your project up and
running:
Advanced Configuration
Android
Setup
This setup is necessary for all the Firebase modules using Android and needs to be done
once per project, and basically involves importing the google-services.json file into your
project.
You can get your package name from the Android Game Options, and your Debug
signing certificate SHA-1 from the Android Preferences (under Keystore):
7. Now go into GameMaker Studio 2, right click on your Firebase extension asset and
click on Open In Explorer / Show In Finder.
NOTE You only need to perform this set-up using one of your Firebase
extensions (per project); for this example we'll be using the Firebase
Analytics extension.
8.
Open the AndroidSource folder.
10.
Open ProjectFiles folder and place your downloaded google-services.json file
inside it.
11. You have now finished the main setup for all Firebase Android modules!
iOS Setup
This setup is necessary for all the Firebase modules using iOS and needs to be done once
per project, and basically involves importing the GoogleServices-Info.plist file into your
project.
NOTE You only need to perform this set-up using one of your Firebase
extensions (per project); for this example we'll be using the Firebase
Analytics extension.
11. Make sure to set up CocoaPods for your project unless you are only using the
REST API in an extension (if one is provided -- not all extensions provide a REST
API) or the Firebase Cloud Functions extension (which only uses a REST API).
12. You have now finished the main setup for all Firebase iOS modules!
Web
Setup
This setup is necessary for all the Firebase modules using Web export and needs to be
done once per project, and basically involves adding Firebase libraries and your Firebase
values to the index.html file in your project.
9. Open the index.html file in Notepad++ or Visual Studio Code (or any other text
editor you prefer).
10. Now we need to add the following code between the </head> and <body> tags (line
72 in the html.index image above):
<script src="https://www.gstatic.com/firebasejs/8.9.1/firebase-
app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.9.1/firebase-
analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.9.1/firebase-
auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.9.1/firebase-
database.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.9.1/firebase-
firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.9.1/firebase-remote-
config.js"></script>
<script>
const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
firebase.initializeApp(firebaseConfig);
</script>
14. Place your index.html file inside the folder that opens (/datafiles).
18. Press Apply and the main setup for all Firebase Web modules is finished!
REST
API Setup
This setup is necessary for syncing the Firebase Realtime Database console with the REST
API implementation.
3. You now have the Firebase Realtime extension configured to use the REST API.
FirebaseRealTime
This function is the entry point for database interactions using the Firebase Realtime
Database extension. It returns a <dbEntryRef> that can be further modified and
configured using chained methods.
Syntax:
FirebaseRealTime([database])
Argument Description
database OPTIONAL Overwrite the default database
Returns:
dbEntryRef Struct
Example:
Above we have two database interaction examples. In the first one we create a reference
to a database entry ( "players/hero/hp" , using the chained method Path) and afterwards we
set that entry to the value 100 (using the method Set). The second example is very similar
but
we pass in a specific database URL ( dbUrl ) to the main function call; this is very
powerful and allows the developer to have access as many different databases as needed.
<dbEntryRef>.Child
This method appends the given relatives path to the current path of a Realtime
Database <dbEntryRef> struct.
Syntax:
<dbEntryRef>.Child(path)
Argument Description
path Relative child path
Returns:
dbEntryRef Struct
Example:
var parentNode = "characters/player";
var nodeRef = FirebaseRealTime().Path(parentNode).Child("heath");
The code sample above will create a <dbNodeRef> to the "characters/player" path (using
the Path method) and afterwards append the relative path "health" to it, meaning the
resulting reference ( nodeRef ) now points to the "characters/player/health" path.
<dbEntryRef>.Delete
This method deletes a reference on the database and returns a listener identifier for
tracking purposes.
This is an asynchronous function that will trigger the Async Social event when the task
is finished.
Syntax:
<dbEntryRef>.Delete()
Returns:
Triggers:
Example:
listenerId = FirebaseRealTime().Path("myCollection/oldDoc").Delete();
I n the code above we first get the reference to a database path ( "myCollection/oldDoc" ,
using the method Path) and then delete that entry. The function call will then return a
listener ID ( listenerId ) that can be used inside an Async Social event.
The code above matches the response against the correct event type, providing a
success message if status is valid.
<dbEntryRef>.Exists
This method checks if a reference exists on the database and returns a listener identifier
for tracking purposes.
This is an asynchronous function that will trigger the Async Social event when the task
is finished.
Syntax:
<dbEntryRef>.Exists()
Returns:
Triggers:
Example:
listenerId = FirebaseRealTime().Path("myCollection/oldDoc").Exists()
I n the code above we first get the reference to a database path ( "myCollection/oldDoc" ,
using the function Path) and check if that entry exists in our database. The function call
will then return a listener ID ( listenerId ) that can be used inside an Async Social event.
The code above matches the response against the correct event type, providing a
success message if the entry exists in the database.
<dbEntryRef>.Listener
The method listens for changes to a given entry in the database. If you need to stop
listening to changes you can use ListenerRemove for a specific database reference
or ListenerRemoveAll to remove all listeners.
This is an asynchronous function that will trigger the Async Social event each time there is
a change to the document.
IMPORTANT When you no longer need to listen to changes make sure to remove the
listeners, as not doing so can lead to memory leaks.
Syntax:
<dbEntryRef>.Listener()
Returns:
Triggers:
Example:
listenerId = FirebaseRealTime("players/hero").Listener();
The code above starts listening for updates on the database entry ( "player/hero" ). If this
reference gets updated, set or deleted an async event will be triggered. The function call
will return a listener ID ( listenerId ) that can be used inside an Async Social event.
The code above matches the response against the correct event type and logs when
changes are made to the database reference being listened to.
<dbEntryRef>.ListenerRemove
This method removes a previously created listener (that was created using the function
Listener).
Syntax:
<dbEntryRef>.ListenerRemove()
Returns:
N/A
Example:
listenerId = FirebaseRealTime("players/hero").Listener();
FirebaseRealTime().ListenerRemove(listenerId);
The code sample above starts by creating a listener to a database entry reference (using
the Listener method). This function returns a listener ID ( listenerId ) and at a later stage
we use that ID to remove the listener.
<dbEntryRef>.ListenerRemoveAll
This method removes all previously created listeners (that were created using the function
Listener).
Syntax:
<dbEntryRef>.ListenerRemoveAll()
Returns:
N/A
Example:
listenerId1 = FirebaseRealTime().Path("players/hero").Listener();
listenerId2 = FirebaseRealTime().Path("players/knight").Listener();
The code above creates two listeners (using the Listener function) on different database
entries (using the Path method) and after some time it removes them all.
<dbEntryRef>.Parent
This method goes up the hierarchy to the parent path of the current <dbEntryRef> struct.
Syntax:
<dbEntryRef>.Parent()
Returns:
dbEntryRef Struct
Example:
var dbPath = "players/hero/name";
var dbEntryRef = FirebaseRealTime().Path(dbPath).Parent().Child("magic").Set(99);
The code above creates a <dbEntryRef> to the "players/hero/name" path (using the
Path function) and afterwards goes up to the parent path meaning the resulting reference
( dbEntryRef ) now points to the "players/hero" path. We then go into the child database
entry (using the Child method) and finally set this new reference ( "players/hero/magic" ) to
the value 99 (using the Set method).
<dbEntryRef>.Path
NOTE Paths are a representation of references in the database (i.e.: the path to get
"Opera X YoYoGames" is "Testing/String" ).
Syntax:
<dbEntryRef>.Path(path)
Argument Description
path The path reference to the database entry.
Returns:
dbEntryRef Struct
Example:
FirebaseRealTime().Path("Testing/String").Read();
he code above first creates the reference to a database entry (
T "Testing/String" ) and then
reads that entry (using the Read method).
<dbEntryRef>.Push
This method is useful for storing arrays inside the database. Database elements can be of
type string, real or struct, meaning that arrays are not a valid type within a Firebase
Realtime Database. This method attaches an auto-generated child location to the current
database entry reference.
Syntax:
<dbEntryRef>.Push()
Returns:
dbEntryRef Struct
Example:
var data = { health: 100, magic: 99 };
var jsonData = json_stringify(data);
FirebaseRealTime().Path("Enemies/List").Push().Set(jsonData);
<dbEntryRef>.Set
This is an asynchronous function that will trigger the Async Social event when the task
is finished.
TIP It's also possible to add a time-stamp value to the database entry if you set it to
{ .sv: "timestamp" } (example below).
Syntax:
<dbReference>.Set(value)
Returns:
Triggers:
Example:
The code sample above provides three example for setting values in a database entry
using a real, a string and a struct (a timestamp example is also provided). The function call
will then return a listener ID ( listenerId ) that can be used inside an Async Social event.
The code above matches the response against the correct event type and logs the success
of the task.
<dbEntryRef>.Read
This is an asynchronous function that will trigger the Async Social event when the task
is finished.
Syntax:
<dbEntryRef>.Read()
Returns:
Triggers:
Example:
listenerId = FirebaseRealTime().Path("players/hero").Read()
he code above first gets the reference to a database entry ( "players/hero" , using the
T
Path method) and then reads that entry. The function call will then return a listener ID
( listenerId ) that can be used inside an Async Social event.
The code above matches the response against the correct event type, providing the
document data if the task succeeded.
<dbEntryRef>.EndAt
Syntax:
<dbEntryRef>.EndAt(value)
Returns:
dbEntryRef Struct
Example:
listenerId = FirebaseRealTime().Path("players/
healthList").OrderByValue().EndAt(99).Read();
<dbEntryRef>.EqualTo
Syntax:
<dbEntryRef>.EqualTo(value)
Returns:
dbEntryRef Struct
Example:
listenerId = FirebaseRealTime().Path("players/
healthList").OrderByValue().EqualTo(77).Read();
INFO This method will filter elements from the start of the ordered group; if you want to
read from the end, use LimitToLast instead.
Syntax:
<dbEntryRef>.LimitToFirst(amount)
Returns:
dbEntryRef Struct
Example:
listenerId = FirebaseRealTime().Path("players/
healthList").OrderByValue().LimitToFirst(5).Read();
<dbEntryRef>.LimitToLast
INFO This method will filter elements from the end of the ordered group; if you want to
read from the start, use LimitToFirst instead.
Syntax:
<dbEntryRef>.LimitToLast(amount)
Returns:
dbEntryRef Struct
Example:
listenerId = FirebaseRealTime().Path("players/
healthList").OrderByValue().LimitToLast(5).Read()
<dbEntryRef>.OrderByChild
NOTE The final result from the Read() call will not be sorted; the sort order is only used
by any filter methods chained after this.
Syntax:
<dbEntryRef>.OrderByChild(path)
Returns:
dbEntryRef Struct
Example:
listenerId =
FirebaseRealTime().Path("enemies").OrderByChild("points").LimitToLast(5).Read();
<dbEntryRef>.OrderByKey
NOTE The final result from the Read() call will not be sorted; the sort order is only used
by any filter methods chained after this.
Syntax:
<dbEntryRef>.OrderByKey()
Returns:
dbEntryRef Struct
Example:
listenerId = FirebaseRealTime().Path("enemies/
names").OrderByKey().LimitToFirst(5).Read();
<dbEntryRef>.OrderByValue
NOTE The final result from the Read() call will not be sorted; the sort order is only used
by any filter methods chained after this.
Syntax:
<dbEntryRef>.OrderByValue()
Returns:
dbEntryRef Struct
Example:
listenerId = FirebaseRealTime().Path("enemies/
names").OrderByValue().LimitToFirst(5).Read();
<dbEntryRef>.StartAt
This function is a filter that should be used when preforming a Read operation or creating
a Listener that is ordered in some way (see OrderByChild,
OrderByKey and OrderByValue). The filter assures that the returned results start at a
specific value.
Syntax:
<dbEntryRef>.StartAt(value)
Returns:
dbEntryRef Struct
Example:
listenerId = FirebaseRealTime().Path("players/
healthList").OrderByValue().StartAt(10).Read();
REST
API Setup
This setup is necessary for syncing the Firebase console with the REST API implementation
of the extension.
4. The extension should now work on all your REST API exports!