Monday, May 20, 2024
1
rated 0 times [  8] [ 7]  / answers: 1 / hits: 18485  / 6 Years ago, mon, november 5, 2018, 12:00:00

I am following this Google Cloud Firestore example on YouTube and getting real time updates successfully. However, I don't know how to unsubscribe to updates because it's not explained in the video. I read the documentation to create an unsubscribe() function but it doesn't work for me.





	getRealtimeUpdates = function(document) {
firestore.collection(collection_name)
.onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
if (doc && doc.exists) {
const myData = doc.data();
// DO SOMETHING
}
});
});
}




More From » google-cloud-firestore

 Answers
53

The firestore.collection().onSnapshot() function returns a unsubscribe function. Just call it and you should be guchi.



You can also find another example here: How to remove listener for DocumentSnapshot events (Google Cloud FireStore)



Here is a snippet I created that should work:





let unsubscribe;

getRealtimeUpdates = function(document) {
unsubscribe = firestore.collection(collection_name)
.onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
if (doc && doc.exists) {
const myData = doc.data();
// DO SOMETHING
}
});
});
}

// unsubscribe:

unsubscribe();





Path to the corresponding Firebase Documentation:



https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference#onSnapshot




Returns
An unsubscribe function that can be called to cancel the snapshot listener.



[#53174] Wednesday, October 31, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elaine

Total Points: 628
Total Questions: 111
Total Answers: 104

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
;