Setup of a Firebase Realtime Database tutorial (step by step)

AndroidCrypto
5 min readDec 24, 2023

--

The Firebase environment needs a setup for each product, and in this article I’m setting up the Realtime Database product. It is part 3 of an article series about the setup of the most used Firebase products. It is based on the first tutorial “Setup of a Firebase project tutorial (step by step)” — please check that you are ready to add a product to a project. There is no code for an Android or Web app (see the links to my other articles for this topic. As most apps require a user authentication you should read the tutorial about the “Authentication” product as well.

What is Firebase ? Firebase is a set of backend cloud computing services and application development platforms provided by Google. It hosts databases, services, authentication, and integration for a variety of applications, including Android, iOS, JavaScript, Node.js, Java, Unity, PHP, and C++. (source: Wikipedia).

You may have noticed that this is the tutorial for Firebase Realtime Database and another for Cloud Firestore Database, is that just a typo error ? No, it isn’t. The Firebase RT Database is the older one, Firestore Database is younger, but both of them are still under development. This is the “official” answer by Google’s Firebase team (see complete answer on https://firebase.google.com/docs/database/rtdb-vs-firestore):

Firebase offers two cloud-based, client-accessible database solutions that support realtime data syncing:

  • Cloud Firestore is Firebase’s newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales further than the Realtime Database.
  • Realtime Database is Firebase’s original database. It’s an efficient, low-latency solution for mobile apps that require synced states across clients in realtime.

Although Firebase products seem to be “free” there a (generous) limits for all products (e.g. on Storage size) and they differ between the Firebase RT Database and the Cloud Firestore Database, for details see https://firebase.google.com/pricing.

If you want to setup Firebase Realtime Database, you are right here. Choose the product:

Click on the button to proceed:

Click on “Create Database” to proceed or click on “Compare Databases” for more information about the two databases:

We need to choose at what server location the database should run — chose carefully as you cannot change this setting at a later time. As I’m located in Germany I have chosen “Belgium”. Click on “Next”:

The second step is about Security rules and I don’t really like the two possible options:

  • start in start in locked mode: means create an “unusable database” because no data can get appended, deleted or even read. Choose this option if you know what individual rules need to get setup in a following step.
  • start in test mode: means the database will allow an unlimited read and write access without any limitations. As this setup is insecure the database has a time limit of 1 month. This option is good for testing purposes but there is a high risk that you forget about the time limit and
    are surprised when your application stops to work properly.

I’m choosing the locked mode here as the sample app provides some prepared database rules in the “readme.md” document of the sample app:

{
"rules": {
// User profiles are only readable/writable by the user who owns it
"users": {
"$UID": {
".read": "auth.uid == $UID",
".write": "auth.uid == $UID"
}
},

// Posts can be read by anyone but only written by logged-in users.
"posts": {
".read": true,
".write": "auth.uid != null",

"$POSTID": {
// UID must match logged in user and is fixed once set
"uid": {
".validate": "(data.exists() && data.val() == newData.val()) || newData.val() == auth.uid"
},

// User can only update own stars
"stars": {
"$UID": {
".validate": "auth.uid == $UID"
}
}
}
},

// User posts can be read by anyone but only written by the user that owns it,
// and with a matching UID
"user-posts": {
".read": true,

"$UID": {
"$POSTID": {
".write": "auth.uid == $UID",
".validate": "data.exists() || newData.child('uid').val() == auth.uid"
}
}
},

// Comments can be read by anyone but only written by a logged in user
"post-comments": {
".read": true,
".write": "auth.uid != null",

"$POSTID": {
"$COMMENTID": {
// UID must match logged in user and is fixed once set
"uid": {
".validate": "(data.exists() && data.val() == newData.val()) || newData.val() == auth.uid"
}
}
}
}
}
}

Use “Locked mode” and click on “Enable”:

At this time the database is empty, but you should click on the “Rules” tab to edit the “locked mode”. Change the existing (locking) rule from:

{
"rules": {
".read": false,
".write": false
}
}

to this values, meaning that only authenticated users are allowed to read and write to documents. This means on the other hand: every authenticated user can edit every document; a better solution could be that all user can read every document, but only the user who had created a document is allowed to edit the data. For more information about “rules” see https://firebase.google.com/docs/database/security. If you want to “play” with rules visit the “Rules Playground”.

{
"rules": {
".read": "auth.uid != null",
".write": "auth.uid != null"
}
}

At the bottom you find the information where your database is located:

And that’s it, your new database is ready to use.

If you just needed an authentication service and the database for your project you are ready to download the “google-services.json” file, move it to your app folder and start developing your app.

Article series about Firebase:

  1. Setup of a Firebase project
  2. Setup of Firebase Authentication
  3. Setup of a Firebase Realtime Database (this article)
  4. Setup of a Cloud Firestore Database
  5. Setup of Firebase Storage
  6. Finish the setup of the Firebase project for an Android app tutorial (step by step)
  7. Finish the setup of the Firebase project for a Web app tutorial (step by step): will follow soon
  8. Firebase tutorial for an Android app (Java): will follow soon
  9. Firebase tutorial for a Web app (Javascript): will follow soon

--

--