Photo by Yaoqi LAI on Unsplash

If you missed my previous post, Firebase 101: Authentication, I recommend starting there for a thorough explanation of what exactly Firebase is. We also covered, as you may have guessed from the title, authentication with Firebase and how to implement it.

As far as databases go, Firebase has two options to select from — Cloud Firestore and Realtime Database. On comparing the two, Google offers the below:

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.

If you’d like to learn more about Cloud Firestore, you can find the documentation here and check out the below video. However, today we will be focusing on setting up and configuring a Realtime Database.

Realtime Database

Realtime Database is a NoSQL cloud database that prides itself on its realtime updates, scalability, and ability to operate offline. The realtime component is one of its most unique aspects. Data updates and database updates usually only happen when an HTTP request is made, a GET request updating the former and a POST or PATCH request updating the later. Inherently, this process relies on the client making that request, and updates will not happen otherwise.

Realtime Database uses something other than HTTP requests though for getting new data, which negates the need for client requests and facilitates this real-time updating. It uses data synchronization, meaning that whenever data is updated, all connected devices are automatically updated nearly instantaneously without the need for a new request. Think about that for a second. All connected devices anywhere are updated — that’s incredible.

Updates, of course, require a connection, but conveniently Realtime Database utilizes disk storage for the times that a user is offline. Users can still access the latest data from before they went offline, and as soon as they are reconnected, the data synchronization picks up where it left off and updates automatically.

In addition, Realtime Database can be used seamlessly with mobile applications and has you covered when it comes to securing your application’s data. It offers Firebase Realtime Database Security Rules, allowing you to easily maintain control of permissions and rules as they pertain to your database. You can read more about these rules here.

Setting Up Realtime Database

From the Develop menu on the left-hand side of your Firebase application, select “Database”. Scroll down to the “Or choose Realtime Database” section, and click on “Create database”.

You will see a prompt to start in locked mode or test mode. I am starting in test mode for this example, but depending on your needs you can select either one. And just like that, you have a Realtime Database and will be redirected to the dashboard for it.

Connecting to Realtime Database

To keep things simple, we will be covering connecting with a web application using JavaScript. iOS, Android, and languages other than JavaScript will have their own guides to connection.

First, set up your config folder and files, however, you typically structure these. In this config file, use the below code to initialize your connection:

// Set the configuration for your app

var config = {

apiKey: "apiKey",

authDomain: "projectId.firebaseapp.com",

databaseURL: "https://databaseName.firebaseio.com",

storageBucket: "bucket.appspot.com"

};

firebase.initializeApp(config);



// Get a reference to the database service

var database = firebase.database();

You will need to find your apiKey, projectId, databaseName, and bucket. Your database name can be found on the dashboard for your database, under the data tab:

To get your bucket, you will first need to configure a default storage bucket for your database. To do this, select “Storage” from the Develop menu, and then select “Get Started”.

You will be prompted to set the rules for your storage bucket, and then the location for your default storage bucket. Hit “Done” once completed and your default storage bucket will be created. You will be redirected to the dashboard for your new storage bucket, where you can find the bucket name for your database config file:

The remaining pieces are specific to the project rather than the database, and can be found in Project Settings:

Under the “General” tab, you can find your projectID and Web API Key:

And just like that, your Realtime Database is set up within Firebase and properly configured from within your codebase. Complete documentation for this database can be found here. Check back for more on Firebase’s Crashlytics and Analytics tools.