0
0
Firebasecloud~30 mins

Realtime Database vs Firestore decision in Firebase - Hands-On Comparison

Choose your learning style9 modes available
Realtime Database vs Firestore Decision
📖 Scenario: You are building a simple chat app that stores messages and user info. You want to choose between Firebase Realtime Database and Firestore for your backend database.Both databases store data in real time but have different structures and features.
🎯 Goal: Create a small Firebase configuration that sets up a database reference and writes a message. Then add a config variable to choose between Realtime Database and Firestore. Finally, write the code to save a message to the selected database.
📋 What You'll Learn
Create a Firebase config object with apiKey and projectId
Add a variable useFirestore to select the database type
Write a function saveMessage that saves a message to the chosen database
Complete the Firebase initialization with the correct database reference
💡 Why This Matters
🌍 Real World
Choosing between Firebase Realtime Database and Firestore is common when building apps that need real-time data syncing, like chat apps or collaborative tools.
💼 Career
Understanding how to configure and use Firebase databases helps in roles like frontend developer, mobile app developer, and cloud engineer working with real-time applications.
Progress0 / 4 steps
1
Setup Firebase config object
Create a constant called firebaseConfig with these exact properties: apiKey set to "ABC123" and projectId set to "chat-app".
Firebase
Need a hint?

Use const to create an object with two properties.

2
Add database selection variable
Add a constant called useFirestore and set it to true to select Firestore as the database.
Firebase
Need a hint?

Use const useFirestore = true; to choose Firestore.

3
Write saveMessage function
Write a function called saveMessage that takes a message string parameter. Inside, use if (useFirestore) to check the database type. If true, call firestore.collection('messages').add({ text: message }). Otherwise, call realtimeDb.ref('messages').push({ text: message }).
Firebase
Need a hint?

Use an if statement to choose the database and call the correct method to save the message.

4
Initialize Firebase and database references
Add code to initialize Firebase with firebase.initializeApp(firebaseConfig). Then create constants firestore as firebase.firestore() and realtimeDb as firebase.database().
Firebase
Need a hint?

Call firebase.initializeApp with the config, then create the database references.