Skip to content

Realtime Database

Chuck edited this page Apr 19, 2024 · 17 revisions

The Realtime Database functionality includes dynamic updates via the server-sent events protocol as implemented by Firebase. As such, database updates can happen and everyone connected to any path that's updated will be updated directly without the need to do a get at that path. This will store the data automatically for you as well and send a signal with the latest info.

Contents on this page:


Database

Firebase.Database

Functions Description
get_database_reference(path: String, filter: Dictionary) -> FirebaseDatabaseReference

Create a reference to a specific path inside the Realtime Database.

FirebaseDatabaseReference

FirebaseDatabaseReference

Signals Description
new_data_update(data) Emitted when new data is added to the path.
patch_data_update(data) Emitted when data is updated within the path.
delete_data_update(data) Emitted when data is deleted within the path.
push_successful() Emitted when data has been successfully pushed to the path.
push_failed() Emitted when a request to push data to the path has failed.

***

Connecting

Note you need to be authenticated for this to work

The below will get you a basic reference to a path in the database; this will be updated automatically as the path is updated in the realtime database.

It is important to note that when you connect to a database in this manner, Godot will see this as a NEW reference and will get all of the data from the database at this reference point automaticlly. There is no need to tell Godot to do this.

var db_ref = Firebase.Database.get_database_reference("path_to_position_in_database", {})

Connecting Signals

Note you need to be authenticated for this to work

The below will connect to the signals emmited by the database where there are data updates, and connect them to a function _on_db_data_update

db_ref.connect("new_data_update", self, "_on_db_data_update")
db_ref.connect("patch_data_update", self, "_on_db_data_update")
db_ref.connect("delete_data_update", self, "_on_db_data_update")

Filter

Note you need to be authenticated for this to work

The below will get you a filtered database reference to a path in your database. Note the constants supported for filtering are contained in FirebaseDatabaseReference, near the top of the file.

var db_ref = Firebase.Database.get_database_reference("path_to_position_in_database", { FirebaseDatabaseReference.LIMIT_TO_LAST : 10 })

Note that any data which is filtered is returned UNORDERED, regardless of which order you try to set on it. The reason for this can be found here: https://firebase.google.com/docs/database/rest/retrieve-data

Push data

db_ref.push({'username': username}) # new entry created in list at path_to_position_in_database; # listen to new_data_update to get the key and data that was pushed

Update data

db_ref.update(data_key, {'user_name': new_username}) # patch_data_update will fire with the appropriate data; can get data_key from a successful new_data_update after a push

Delete data

db_ref.delete(reference) # delete data with whatever refernece you pass to this. If you are connected to /data/users, and run delete(mickeymouse), then /data/users/mickeymouse and everything under that point will be removed

Examples

List of examples:

ex. Print The Database Reference

Note you need to be authenticated for this to work

# In 3.x

extends Node

# Variables
var db_ref

func _ready():
	Firebase.Auth.connect("login_succeeded", self, "_on_FirebaseAuth_login_succeeded")
	Firebase.Auth.login_with_email_and_password(email, password)

func _on_FirebaseAuth_login_succeeded(auth):
	db_ref = Firebase.Database.get_database_reference("data", {})
	db_ref.connect("new_data_update", self, "_on_db_data_update")
	db_ref.connect("patch_data_update", self, "_on_db_data_update")
    db_ref.connect("delete_data_update", self, "_on_db_data_update")

func _on_db_data_update(resource):
	print(resource)

Top // Back