📖 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
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
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
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
Hint
Call firebase.initializeApp with the config, then create the database references.
Practice
(1/5)
1. Which Firebase database is best suited for simple real-time syncing of data as a JSON tree?
easy
A. Realtime Database
B. Firestore
C. Cloud Storage
D. Firebase Hosting
Solution
Step 1: Understand data structure of Realtime Database
Realtime Database stores data as a JSON tree, which is simple and fast for syncing.
Step 2: Compare with Firestore
Firestore uses collections and documents, better for complex queries but not just simple JSON tree syncing.
Final Answer:
Realtime Database -> Option A
Quick Check:
Simple JSON tree sync = Realtime Database [OK]
Hint: Simple JSON tree and fast sync? Choose Realtime Database [OK]
Common Mistakes:
Confusing Firestore with Realtime Database
Choosing Cloud Storage for database needs
Thinking Firebase Hosting stores data
2. Which syntax correctly references a document in Firestore?