Firebase Realtime Database: The Best Way to Sync Your Data

JetRuby Agency
JetRuby Agency
Published in
5 min readJul 30, 2018

--

Hey, everyone! We can’t help telling you about new incredible tools in the magical world of software development. In our last article, we discussed a simple formula for saving both a good performance and user-friendly UX. Take a look at it and your mobile users will thank you for it.

This time, we’ll focus on a cloud-hosted database. Firebase Realtime Database allows syncing data across all the clients in real time. Additionally, all the data remains available even if your app goes offline. Firebase Realtime Database provides a set of SDK (such as Android, iOS, JavaScript). They will help you sync data for various clients when developing hybrid applications.

The main benefits of Realtime Database are:

  • It uses synchronization. As soon as the stored information is being changed, all the connected devices will be updated within the shortest period of time (within a few milliseconds). This allows taking a break from working with http-requests and networking code.
  • Realtime Database works in Offline mode. Since all the data is stored on the devices, client applications keep working even if the connection was lost. As soon the connection was established, a client application will be synced with the server’s data.
  • Realtime Database is available from various client devices, such as mobile device or web browser. Besides, you won’t need any server application for interoperability. Both the data security and the validation are provided with the help of Firebase Realtime Database Security Rules that are being implemented when reading or recording the data.

Chose DataBase

Firebase offers a few types of cloud databases that support the data synchronization in real time.

  1. Realtime Database is an effective solution with a low delay for mobile applications. For this purpose should be synced the client conditions in a real time.
  2. Cloud Firestore (beta) is a new solution for mobile developers. It provides a more understandable model of data storing as well as more possibilities for creating various requests with a faster implementation.

If you don’t mind working with a product in beta-version, you may use Cloud Firestore in your projects as it offers additional functionality. Moreover, it provides much better productivity.

Firebase Realtime Database Android SDK

First of all, we need to register an application in Firebase Console (https://firebase.google.com/docs/android/setup). Don’t forget to add in the application the generated file google-services.json.

Let’s create a developer’s console in the Realtime Database. Go to the division Rules and access the rights for database record/reading. In future, you need to limit the rights only for authorized users as well as provide an authorization for Android-client. (https://firebase.google.com/docs/auth/)

Let’s add a dependency in build.gradle(Project) to activate Google services:

To start using Firebase realtime SDK in android-application, we still need to add a dependency in build.gradle(Module):

Let’s initiate Firebase Database:

Realtime Database can be considered as NoSQL database, where all the information is stored in JSON-format and looks like JSON tree.

When adding new data into the database, will be created a new node (key-value).

Let’s create a new class Message that will have the following fields:

  • message time (it will be stored in milliseconds, though we’ll be sending on the server ServerValue.TIMESTAMP; all the amount we get, will be in milliseconds);
  • message text;
  • user;

We’ll create a message on Android client and save it in Realtime. Using a child node “messages,” let’s create a new sub-object of the message using a key (UUID.randomUUID().toString()). All the attributes will be written there as HashMap.

To request all the messages from the base, let’s create a listener. Having got the result, we’ll be addressing to the child nodes of messages node:

Let’s make sure that all the messages were created in Realtime Database

Tools for working with Firestore cloud storage for Android

First of all, you need to register an application in FirebaseConsole. Add into the application a file google-services.json.

We need to access rights on reading/record in Firebase Console. In future, you need to limit the rights only for authorized users as well as provide an authorization for Android-client.

To add Firebase Firestore SDK in Android application, we need to add a dependency in build.gradle(Module):

And don’t forget to add Google services to build.gradle (Project):

Now we need to initialize Firebase Firestore:

Firestore looks like NoSQL storage, which data is kept in terms of Collection that consists of Documents. Let’s create Collection “messages” in Firebase Console (there we’ll be sending messages).

Now let’s Message class with the following fields:

  • date of the message
  • text of the message
  • user

A message, which was written by the user, will be stored In the Firestore of the application. For this purpose, we need to address the collection “messages” and add a new message in terms of HashMap. It’ll have the following attributes:

To read a list of the messages from the Firestore, we need to address to Collection “message” and call a method get(). Let’s sort them by time orderBy(“date”). This will allow us displaying the messages in chronological order.

In order to get “messages” updates, let’s set a message listener. We’ll request there Collection “messages” in a chronological order orderBy(“date”):

Let’s write a few messages:

In Firebase Console, we’ll see all the saved messages

The Bottom Line

In the right hands, Firebase is an effective solution for creating a cloud databases both for small and big projects.

Firebase allows creating simple cloud databases. It’s incredibly easy to use and can be easily integrated with client applications. As a result, you won’t need much time for developing a backend database.

Firebase allows working with various platforms including Android, iOS, and Web. And it also provides fast data synchronization on all devices. Documentation.

Thank you for your time. If you want to find out about other solutions we found, visit our blog. For example, we managed to build an fully-featured Android app in only 12 days!

--

--

JetRuby is a Digital Agency that doesn’t stop moving. We expound on subjects as varied as developing a mobile app and through to disruptive technologies.