0
0
Firebasecloud~20 mins

Real-time listeners (onSnapshot) in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Real-time Listener Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
Understanding onSnapshot real-time updates

What happens when you attach an onSnapshot listener to a Firestore document?

Choose the correct behavior of the listener.

Firebase
const unsub = docRef.onSnapshot(doc => {
  console.log(doc.data());
});
AThe listener fetches the document once and then stops listening.
BThe listener triggers only if the document does not exist.
CThe listener only triggers when the document is deleted.
DThe listener fetches the document initially and then listens for real-time updates, triggering the callback on every change.
Attempts:
2 left
💡 Hint

Think about what 'real-time' means in Firestore listeners.

Configuration
intermediate
2:00remaining
Properly detaching an onSnapshot listener

Given the following code, what is the correct way to stop listening to real-time updates?

const unsubscribe = docRef.onSnapshot(snapshot => {
  console.log(snapshot.data());
});

Choose the correct way to detach the listener.

Firebase
const unsubscribe = docRef.onSnapshot(snapshot => {
  console.log(snapshot.data());
});
ACall <code>docRef.offSnapshot()</code> to stop listening.
BSet <code>unsubscribe = null</code> to stop listening.
CCall <code>unsubscribe()</code> when you want to stop listening.
DListeners stop automatically after 5 minutes; no action needed.
Attempts:
2 left
💡 Hint

Remember the function returned by onSnapshot.

Architecture
advanced
3:00remaining
Designing efficient real-time updates for a chat app

You are building a chat app using Firestore. You want to listen to new messages in a chat room in real-time but avoid unnecessary data usage and costs.

Which approach is best to achieve this?

AFetch all messages once with <code>get()</code> and poll every 10 seconds for new messages.
BAttach an <code>onSnapshot</code> listener to a query that fetches only the latest 50 messages ordered by timestamp.
CAttach an <code>onSnapshot</code> listener to the entire <code>messages</code> collection and filter messages client-side.
DAttach multiple <code>onSnapshot</code> listeners to each message document individually.
Attempts:
2 left
💡 Hint

Think about limiting data and using queries.

security
advanced
3:00remaining
Security rules impact on onSnapshot listeners

You have an onSnapshot listener on a Firestore collection. If a user does not have read permission for some documents, what will happen?

AThe listener will only receive documents the user has permission to read; others are filtered out.
BThe listener will receive all documents regardless of permissions.
CThe listener will fail with a permission error and stop listening.
DThe listener will receive documents but with empty data for unauthorized ones.
Attempts:
2 left
💡 Hint

Consider how Firestore security rules filter data.

🧠 Conceptual
expert
3:00remaining
Behavior of onSnapshot with offline persistence enabled

When Firestore offline persistence is enabled, what is the behavior of an onSnapshot listener when the device is offline?

AThe listener immediately triggers with cached data and waits for online updates.
BThe listener does not trigger until the device is back online.
CThe listener triggers only once and then stops.
DThe listener triggers with empty data and errors out.
Attempts:
2 left
💡 Hint

Think about offline support and cached data.