How to remove listener for DocumentSnapshot events (Google Cloud FireStore)

24,378

In case of the web and node.js SDK, calling onSnapshot returns a function that you need to save in a variable and call when you want to remove the listener.

var unsubscribe = db.collection("cities").onSnapshot(function (querySnaphot) {
  // do something with the data.
});


// Stop listening to changes
unsubscribe();

The other SDKs offer similar functionality.

See https://firebase.google.com/docs/firestore/query-data/listen#detach_a_listener for reference.

Share:
24,378
Minh Nguyen
Author by

Minh Nguyen

Updated on October 22, 2020

Comments

  • Minh Nguyen
    Minh Nguyen over 3 years

    I'm new in Google Cloud FireStore.

    The Document object has a function call onSnapshot to attaches a listener for DocumentSnapshot events.

    Is there a function to remove that listener (like offSnapshot)? If not how can I implement it?