Beginner’s Guide: How to Use Firebase Realtime Database
Welcome to this beginner-friendly tutorial on using Firebase Realtime Database! I’m excited to walk you through setting up and integrating Firebase’s cloud-hosted NoSQL database into your app or website. Whether you’re building the next big thing or just tinkering around, Firebase makes it all manageable. Let’s dive right in!
What is Firebase Realtime Database?
Firebase Realtime Database is a powerful tool that allows you to store and synchronize data in real-time across multiple clients. This makes it ideal for applications where you need instant updates, like chat apps, collaborative tools, or live data dashboards. The data is stored in a JSON format and synced across all clients in real-time, making it incredibly dynamic.
Setting Up Firebase
1. Create a Firebase Project
First things first, let’s set up a Firebase project:
- Head over to the [Firebase Console](https://console.firebase.google.com/).
- Click on “Add project” to create a new project.
- Enter your project’s name and follow the prompts to complete the setup.
- In your Firebase project console, find “Realtime Database” in the left-hand menu and click “Create Database.”
- Opt for the test mode to make things easy for now. Don’t worry, you can switch to locked mode after testing.
- Click “Next” and follow any additional prompts to complete the setup.
- Go back to your Firebase project overview.
- Click on the “Web” icon to register your app.
- Follow the on-screen instructions to add Firebase SDKs to your HTML file:

2. Add Firebase Realtime Database to Your Project
Integrating with Your App
3. Set Up Your Project
#### For Web
<!-- Add this inside your <head> tag -->
<script src="https://www.gstatic.com/firebasejs/9.6.10/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.6.10/firebase-database.js"></script>
<script>
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
databaseURL: "https://YOUR_DATABASE_NAME.firebaseio.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID"
};
firebase.initializeApp(firebaseConfig);
</script>
#### For Android
build.gradle file:dependencies {
implementation 'com.google.firebase:firebase-database:20.0.3'
}
MainActivity:import com.google.firebase.database.FirebaseDatabase;
// Add to onCreate method
FirebaseDatabase database = FirebaseDatabase.getInstance();
Reading and Writing Data
4. Write to the Database
#### Web
const database = firebase.database();
function writeUserData(userId, name, email) {
firebase.database().ref('users/' + userId).set({
username: name,
email: email
});
}
#### Android
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
public void writeNewUser(String userId, String name, String email) {
User user = new User(name, email);
database.child("users").child(userId).setValue(user);
}
5. Read from the Database
#### Web
const userId = 'some-user-id';
firebase.database().ref('/users/' + userId).once('value').then((snapshot) => {
const username = snapshot.val().username;
console.log(username);
});
#### Android
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users/some-user-id");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
System.out.println(user.getName());
}
@Override
public void onCancelled(DatabaseError databaseError) {
// handle error
}
});
Real-time Data Updates
One of the fantastic features of Firebase Realtime Database is its ability to provide real-time updates. This means that whenever the data changes in the database, all connected clients receive the update immediately.
Listening for Real-time Updates
#### Web
firebase.database().ref('users/' + userId).on('value', (snapshot) => {
const data = snapshot.val();
console.log(data.username);
});
#### Android
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
System.out.println(user.getName());
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Handle possible errors.
}
});
Securing Your Database
While testing, we used the test mode for ease, but it’s crucial to secure your database. Head back to your database and click on “Rules” to update who can read/write to your database. For beginners, starting with basic authentication using Firebase Auth is a solid choice.
Conclusion
And there you have it! With Firebase Realtime Database, you can effortlessly manage data in real-time across clients, whether you’re working on a web app or an Android app. We’ve covered setup, integration, and basic read/write operations.
Now, it’s your turn! Try adding more complex data structures or enable Firebase Authentication to enhance security. Happy coding, and feel free to reach out with any questions or share what you’re building!
If you’re ready for more, check out Firebase’s extensive documentation or dive into other Firebase services like Cloud Functions to expand your app’s capabilities. Happy building!