Observable backend in Firebase?

Viewed 355

We are using Firebase Firestore and Cloud functions as a backend for a Flutter app. The app gets most of the data directly from Firestore (with security rules), while some data moves through cloud functions. We are considering creating an API for our product, i.e. making the app interact with the cloud functions only.

One problem we are facing is observing object state. The Firestore library allows for easy streaming of documents, i.e reference.document(id).snapshots() returns a Stream, but cloud functions are not observable, and also have a lifetime of up to 9 minutes. Is there a good practice to transmit Firestore document changes from the backend into the app?

1 Answers

You'll have to choose:

  • Build your custom API on Cloud Functions, but without the long-term realtime listening nature of Firebase.
  • Build your custom API on a back-end infrastructure that supports longtime connections, such as pretty much any stateful/serverfull platform. To still maintain auto-scaling, you can consider something like Kubernetes.
  • Build your custom API as being part of Cloud Firestore (or Realtime Database), using Cloud Functions to prepare the data that your clients then consume.

This last approach is common amongst experienced serverless developers, as it allows you to have quite common, secure backend code in your Cloud Functions (or other serverless infrastructure, such as Data Flow, Big Query and many others), but still then use Firestore as the endpoint that your clients listen and write to.

Related