Bird
Raised Fist0
GCPcloud~10 mins

Real-time updates with listeners in GCP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize a Firestore listener for real-time updates.

GCP
db.collection('users').[1](snapshot => {
  snapshot.docChanges().forEach(change => {
    if (change.type === 'added') {
      console.log('New user: ', change.doc.data());
    }
  });
});
Drag options to blanks, or click blank then click option'
AaddListener
Bget
Clisten
DonSnapshot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which fetches data once instead of listening.
Using 'addListener' which is not a Firestore method.
2fill in blank
medium

Complete the code to stop the Firestore listener when no longer needed.

GCP
const unsubscribe = db.collection('messages').onSnapshot(snapshot => {
  // handle updates
});

// Later in code
[1]();
Drag options to blanks, or click blank then click option'
Astop
Bclose
Cunsubscribe
DremoveListener
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call 'stop' or 'close' which are not Firestore listener methods.
Using 'removeListener' which is not valid here.
3fill in blank
hard

Fix the error in the listener callback to correctly handle added documents.

GCP
db.collection('orders').onSnapshot(snapshot => {
  snapshot.docChanges().forEach(change => {
    if (change.type === [1]) {
      console.log('Order added:', change.doc.data());
    }
  });
});
Drag options to blanks, or click blank then click option'
A'added'
Badded
C'add'
D'new'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word without quotes causing a reference error.
Using incorrect strings like 'add' or 'new'.
4fill in blank
hard

Fill both blanks to filter Firestore documents and listen for real-time updates.

GCP
db.collection('tasks').where('status', [1], 'completed').[2](snapshot => {
  snapshot.forEach(doc => {
    console.log(doc.id, '=>', doc.data());
  });
});
Drag options to blanks, or click blank then click option'
A'=='
B'!='
C'<='
DonSnapshot
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which filters out 'completed' instead of selecting it.
Using 'get' instead of 'onSnapshot' which does not listen for updates.
5fill in blank
hard

Fill all three blanks to set up a listener that logs only modified documents with priority above 5.

GCP
db.collection('alerts').where('priority', [1], 5).[2](snapshot => {
  snapshot.docChanges().forEach(change => {
    if (change.type === [3]) {
      console.log('Modified alert:', change.doc.data());
    }
  });
});
Drag options to blanks, or click blank then click option'
A'>'
BonSnapshot
C'modified'
D'<'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' which filters the wrong documents.
Comparing change.type to 'added' or 'removed' instead of 'modified'.

Practice

(1/5)
1. What is the main purpose of using a listener like onSnapshot in Google Firestore?
easy
A. To get real-time updates automatically when data changes
B. To manually refresh the data in the app
C. To delete documents from the database
D. To backup data to another storage

Solution

  1. Step 1: Understand what onSnapshot does

    onSnapshot listens to changes in Firestore data and updates the app instantly.
  2. Step 2: Compare options with onSnapshot purpose

    Only To get real-time updates automatically when data changes describes getting real-time updates automatically, which matches onSnapshot.
  3. Final Answer:

    To get real-time updates automatically when data changes -> Option A
  4. Quick Check:

    Real-time updates = To get real-time updates automatically when data changes [OK]
Hint: Remember: listeners auto-update data without refresh [OK]
Common Mistakes:
  • Thinking listeners require manual refresh
  • Confusing listeners with data deletion
  • Assuming listeners backup data
2. Which of the following is the correct syntax to start listening to a Firestore collection named users using onSnapshot?
easy
A. db.onSnapshot('users', snapshot => { /* handle data */ });
B. db.listen('users').onSnapshot(snapshot => { /* handle data */ });
C. db.collection('users').listen(snapshot => { /* handle data */ });
D. db.collection('users').onSnapshot(snapshot => { /* handle data */ });

Solution

  1. Step 1: Recall Firestore listener syntax

    The correct method to listen to a collection is db.collection('name').onSnapshot(callback).
  2. Step 2: Match options with correct syntax

    Only db.collection('users').onSnapshot(snapshot => { /* handle data */ }); matches the correct Firestore syntax for listening to a collection.
  3. Final Answer:

    db.collection('users').onSnapshot(snapshot => { /* handle data */ }); -> Option D
  4. Quick Check:

    Correct Firestore listener syntax = db.collection('users').onSnapshot(snapshot => { /* handle data */ }); [OK]
Hint: Use db.collection('name').onSnapshot(callback) for listeners [OK]
Common Mistakes:
  • Using .listen instead of .onSnapshot
  • Calling onSnapshot directly on db
  • Passing collection name as first argument to onSnapshot
3. Consider this code snippet listening to a Firestore document:
const unsubscribe = db.collection('tasks').doc('task1').onSnapshot(doc => {
  console.log(doc.data().status);
});
If the document's status field changes from 'pending' to 'done', what will be printed?
medium
A. 'pending' then 'done' printed once each
B. Only 'pending' printed once
C. Only 'done' printed once
D. No output printed

Solution

  1. Step 1: Understand onSnapshot behavior on document changes

    onSnapshot triggers immediately with current data, then again on any change.
  2. Step 2: Trace output for status change

    Initially, 'pending' is printed. When status changes to 'done', listener triggers again and prints 'done'.
  3. Final Answer:

    'pending' then 'done' printed once each -> Option A
  4. Quick Check:

    Listener prints on initial and update = 'pending' then 'done' printed once each [OK]
Hint: Listeners print initial and updated data automatically [OK]
Common Mistakes:
  • Thinking listener triggers only on updates, not initially
  • Expecting no output if data changes
  • Confusing document and collection listeners
4. You wrote this code to listen to a Firestore collection but notice your app slows down over time:
db.collection('messages').onSnapshot(snapshot => {
  // update UI
});
What is the most likely cause and how to fix it?
medium
A. You used the wrong collection name; fix by correcting the name.
B. You need to refresh the page to update data; fix by adding a refresh button.
C. You forgot to unsubscribe; fix by saving and calling the unsubscribe function when done.
D. You should use setTimeout to delay the listener; fix by adding delay.

Solution

  1. Step 1: Identify cause of app slowdown with listeners

    Not unsubscribing causes multiple active listeners, slowing app.
  2. Step 2: Apply best practice to unsubscribe

    Save the unsubscribe function returned by onSnapshot and call it when listener is no longer needed.
  3. Final Answer:

    You forgot to unsubscribe; fix by saving and calling the unsubscribe function when done. -> Option C
  4. Quick Check:

    Unsubscribe to avoid slowdowns = You forgot to unsubscribe; fix by saving and calling the unsubscribe function when done. [OK]
Hint: Always unsubscribe listeners to keep app fast [OK]
Common Mistakes:
  • Ignoring unsubscribe causing memory leaks
  • Trying to refresh page instead of fixing listener
  • Adding delays instead of proper cleanup
5. You want to build a chat app that shows new messages instantly using Firestore listeners. Which approach best ensures efficient real-time updates and resource management?
hard
A. Load all messages once on app start and never update again.
B. Use onSnapshot on the messages collection, update UI on changes, and unsubscribe when user leaves chat.
C. Use periodic polling every 5 seconds to fetch messages and update UI.
D. Use onSnapshot but never unsubscribe to keep updates always.

Solution

  1. Step 1: Choose real-time update method

    onSnapshot provides instant updates, better than polling or one-time load.
  2. Step 2: Manage resources properly

    Unsubscribing when user leaves prevents unnecessary listeners and saves resources.
  3. Final Answer:

    Use onSnapshot on the messages collection, update UI on changes, and unsubscribe when user leaves chat. -> Option B
  4. Quick Check:

    Real-time + unsubscribe = Use onSnapshot on the messages collection, update UI on changes, and unsubscribe when user leaves chat. [OK]
Hint: Real-time updates + unsubscribe = efficient app [OK]
Common Mistakes:
  • Using polling instead of listeners
  • Never unsubscribing causing leaks
  • Loading data only once without updates
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard