0
votes

Going through this blog, I could see that my app would be better off using the Firebase cloud functions for Firebase Firestore rather than directly accessing Firestore using client-side SDK.

I can implement the Firestore READ operation using get(), WRITE operation using set() or update() & DELETE using delete() methods. All these one-shot operations are fine.

Is possible to implement addSnapshotListener to fetch real-time updates? If yes, how?

2

2 Answers

1
votes

While it technically is possible to use addSnapshotListener, there are not a lot of use-cases where this makes sense.

Cloud Functions are short-lived, a maximum of 9 minutes, so not well suited for scenarios where you need to listen to the database for longer times.

Typically, you'll want to instead define a separate Cloud Function that responds to the thing you'd otherwise want to listen to.

1
votes

Realtime listeners are not compatible with the way Cloud Functions works. Functions can only run for a limited amount of time and are shut down after that timeout expires. Database listeners are indefinite, and will continue listening until they are removed. If you add a listener inside a function, the system will still shut it down after the function timeout expires.

It's unclear to me why you need a listener in a Cloud Functions, but it's almost certainly not the right thing to do, given the way functions work.