Integrating Firebase Real-Time Database with ReactJS: A Step-by-Step Guide

Hadi Soufan
Tech Blog
Published in
5 min readFeb 7, 2023
Image Reference: gitconnected.com

Firebase is a Backend-as-a-Service (BaaS) platform that offers developers real-time databases and other services like hosting, storage, and authentication. This article demonstrates how to use Firebase to build a database for a ReactJS application and the best approach for setting up your database rules.

Step 1: Setting up a Firebase project

To get started, you need to create a Firebase project. You can do this by visiting the Firebase website and clicking on the “Go to Console” button.

Firebase website

Once you are in the Firebase Console, click on the “Add project” button and follow the steps to set up your project. After you have created your project, you will be taken to the project dashboard.

Adding a project

Step 2: Installing the Firebase library

Next, you will need to install the Firebase library in your ReactJS app. You can do that using the node package manager as follows:

npm i firebase

Step 3: Initializing Firebase in your app

Once you installed the Firebase library, you need to initialize Firebase in your ReactJS app. You can do this by importing the Firebase library and calling the initializeApp() method, passing in your Firebase credentials.

Add a web App for your project.
Configuring the web app
import firebase from 'firebase/app';
import 'firebase/database';
const firebaseConfig = {
apiKey: 'API_KEY',
authDomain: 'AUTH_DOMAIN',
databaseURL: 'DATABASE_URL',
projectId: 'PROJECT_ID',
storageBucket: 'STORAGE_BUCKET',
messagingSenderId: 'MESSAGING_SENDER_ID',
appId: 'APP_ID',
};

firebase.initializeApp(firebaseConfig);

You can find your Firebase credentials in the Firebase Console, under the “Project settings” section, or directly after clicking the Register app.

Firebase credentials

Step 4: Creating a reference to the database

Once you have initialized Firebase in your app, you can create a reference to the database by calling the database() method on the Firebase instance.

const database = firebase.database();

Developers can use Firebase to access the database and carry out a number of actions, including reading, writing, updating, and deleting data.

Step 5: Reading data from the database

You can read data from the Firebase database by calling the once() method on a reference to the data you want to retrieve.

database.ref('/data').once('value')
.then(function(snapshot) {
console.log(snapshot.val());
});

Step 6: Writing data into the database

You can write data to the Firebase database by calling the set() method on a reference to the data you want to write.

database.ref('/data').set({
name: 'Hadi Soufan',
age: 24,
});

Step 7: Updating data in the database

You can update data in the Firebase database by calling the update() method on a reference to the data you want to update.

database.ref('/data').update({
age: 31,
});

Step 8: Deleting data from the database

You can delete data from the Firebase database by calling the remove() method on a reference to the data you want to delete.

database.ref('/data').remove();

Step 9: Listening for real-time changes

Firebase allows you to listen for real-time changes to the data in your database by calling the on() method on a reference to the data you want to listen to.

database.ref('/data').on('value', function(snapshot) {
console.log(snapshot.val());
});

By following these easy steps, you can use Firebase to create a real-time database for your ReactJS app. More complex real-time applications can be made using the concepts discussed in this article.

It is important to secure your Firebase database by setting up proper rules and authentication.

Your Firebase database should be secured by establishing the appropriate rules and authentication. To guarantee that your data is secure and only accessible by authorized individuals, this is essential.

T protect your data, Firebase offers several security capabilities, such as database rules and authentication. These features are available in the “Database” and “Authentication” sections of the Firebase Console.

Firebase Authentication section

Database Rules

The rules for the Firebase database specify how your data should be organized and what operations are permitted on it. For instance, you can use database rules to guarantee that only authorized users can view or write data. You can navigate to the “Database” section to configure database rules.

Database rules
Setting up a database
Adding a security rule
Modifying the rules

Here is an example of a set of database rules that only allow authenticated users to read and write data:

{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
Publishing the security rule

Numerous authentication options are offered by Firebase, including email and password, phone number, Google, Facebook, and others. You can activate authentication in the “Authentication” section of the Firebase Console.

Setting up authentication
Selecting the authentication option

After enabling authentication, your ReactJS app has to incorporate authentication. The Firebase authentication API, which offers ways for establishing and maintaining users, logging in and out, and more, can be used to accomplish this.

Here is an example of how you can sign up a new user using the email and password authentication method:

firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function(user) {
console.log('User created', user);
})
.catch(function(error) {
console.error(error);
});

Conclusion

To sum up, using Firebase to create a real-time database in a ReactJS project is straightforward. You may quickly add data capabilities to your app, such as reading, writing, and listening to changes in the database. Data protection is essential if you want to guarantee the confidentiality and security of your data.

--

--

Hadi Soufan
Tech Blog

Full Stack Web Developer | Computing and Programming