Firebase Tutorial with Realtime Database: Step-by-Step Guide
Firebase by Google lets developers build web and mobile apps easily. Its Realtime Database syncs data instantly across clients. This tutorial covers setup, integration, and using Realtime Database effectively."
Introduction
What is Firebase Realtime Database?
Firebase is a powerful platform by Google that allows developers to build web and mobile applications with ease. One of its most popular features is Realtime Database, which stores and syncs data in real-time across all connected clients. In this tutorial, we will guide you through setting up Firebase, integrating it with your application, and using Realtime Database effectively.
Firebase Realtime Database is a cloud-hosted NoSQL database that allows you to store and sync data in real-time. Unlike traditional databases, any changes made to the database are instantly reflected in all connected clients, making it perfect for chat apps, live dashboards, or location tracking applications.
Key Features:
- Real-time synchronization across all clients
- Offline support for mobile devices
- Secure data handling with Firebase Authentication
- Easy integration with Android, iOS, and Web applications
How does Firebase work?
1Firebase Realtime Database
Google Firebase can collect data from sensors and inputs while client apps access it in real time. It also offers file storage, machine learning, messaging, and server-side functions.
All data is stored as JSON, synced instantly across all clients, and transmitted securely via SSL. If the network goes down, local changes are saved and updated to the server once connectivity returns, keeping all clients up-to-date automatically.
2Freebase Authentication
Firebase excels at creating user authentication through Email, Google, Facebook, Twitter, GitHub, and even anonymous sign-ins. This ensures users’ personal information stays secure and protects accounts from unauthorized access.
3Firebase Hosting
The final way Firebase works covered in this article is to provide hosting. Hosting is delivered via SSL security technology standard.
What kind of apps can be developed with Firebase?
Firebase is a fairly comprehensive and flexible platform. It allows users to develop the following types of applications:
- iOS.
- Android.
- Web.
User Guide: Initialize Firebase Realtime Database and Connect to Node-RED
Firebase Realtime Database is ideal for real-time IoT projects, and integrating it with Node-RED allows you to visualize, control, and automate workflows easily. This guide will take you step by step.
1Create a Firebase Project
- Go to the Firebase Console.
- Click Add project and provide a name.
- Choose No for Google Analytics (optional).
- Click Create project.
Note:note :Disable the Enable Google Analytics for this project option — this will not be needed this for this project. Finally, select Create project.
Firebase will begin setting up your project. Once completed select Continue, and you will be taken to your projects overview page on the Firebase console.
2Set Authentication Methods
Next, You need to set authentication methods for your app. “Most apps need to know the identity of a user. In other words, it takes care of logging in and identify the users (in this case, the ESP32). Knowing a user’s identity allows an app to securely save user data in the cloud and provide the same personalized experience across all of the user’s devices.” To learn more about the authentication methods, you can read the documentation.
There are several authentication methods like email and password, Google Account, Facebook account, and others. For testing purposes, we can select the Anonymous user (require authentication without requiring users to sign in first by creating temporary anonymous accounts). Enable that option and click Save.
3Set Up Realtime Database
The next step is creating a Realtime Database for your project. Follow the next steps to create the database. On the left sidebar click on Realtime Database and then, click on Create Database.
Select your database location. It should be the closest to your location. we are in Asian country so select that one. Set up security rules for your database. For testing purposes, select Start in test mode. In later tutorials you’ll learn how to secure your database using database rules.
Your database is now created. You need to copy and save the database URL—highlighted in the following image—because you’ll need it later in your ESP32 code.
4Set Up Realtime Database
The Realtime Database is all set. Now, you also need to get your project API key.
To get your project’s API key, on the left sidebar click on Project Settings. Copy the API Key to a safe place because you’ll need it later. Now, you have everything ready to interface the ESP32 with the database.
Go to Project Settings → General → Your apps → Web App (or create one). Than Copy the Firebase config object:
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
databaseURL: "https://YOUR_PROJECT_ID-default-rtdb.firebaseio.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID"
};
NOTE:
You will need this to connect Node-RED to Firebase.
5Set Up Node-RED
- Install Node-RED locally or on a server.
- Launch Node-RED by running:
- Open the Node-RED editor in your browser at http://localhost:1880.
6Install Firebase Node
- In Node-RED, go to Menu → Manage palette → Install.
- Search for node-red-contrib-firebase and install it.
- After installation, you will see Firebase nodes in your palette..
7Connect Node-RED to Firebase
- Drag the Firebase out node (for writing data) and Firebase in node (for reading data) onto the canvas.
- Double-click the node to configure:
- Enter the Database URL from your Firebase config (https://YOUR_PROJECT_ID-default-rtdb.firebaseio.com).
- Upload your service account credentials (download JSON from Firebase → Project Settings → Service Accounts → Generate new private key).
- Set the Firebase path to the node in your Realtime Database, e.g., /sensors/temperature.
8Test Data Flow
- Connect an inject node to the Firebase out node.
- Set the payload to a JSON object:{"temperature": 25, "humidity": 60}
- Click Deploy. The data will be sent to your Firebase Realtime Database.
- Use the Firebase in node to listen for changes and output to a debug node to verify real-time updates.
9Optional – Use Node-RED Dashboard
You can use the Node-RED Dashboard to visualize Firebase data in real-time:
- Install node-red-dashboard.
- Add chart, gauge, or text nodes.
- Connect Firebase in nodes to dashboard nodes for live visualization.
Conclusion
By following these steps, you have:
- Initialized Firebase Realtime Database
- Connected Node-RED to Firebase
- Sent and received real-time data
- Visualized IoT or sensor data through Node-RED Dashboard
This setup is ideal for IoT projects, home automation, and live dashboards.
0 comments:
Post a Comment