Search code examples
firebasegoogle-cloud-firestoregoogle-cloud-functionsfirebase-cloud-messaging

Firebase: How do I inform the client that the CloudFunction executed


When a CloudFunction is started by an EventTrigger:

onDocumentCreated, onDocumentUpdated, onDocumentDeleted, onDocumentWritten

... the code execution may be successful .then(...) or unsuccessful .catch(err)

QUESTION:
Is there a way to inform the client (browser or mobile app) that the CloudFunction executed (un)successfully?

(Just trying to figure out that if there's a computation that takes a while, how wouold the client know it's finished?)


Solution

  • With background Cloud Functions like the ones which handle events in Cloud Firestore, there is no out-of-the-box mechanism to inform the client app that such a Cloud Function has terminated (with success or error).

    An easy way to implement such a mechanism is to set, in your web or mobile app, a real time listener on a document (in a specific Firestore collection) that you create at the end of your Cloud Function code (or in the catch block of the code in case of error).

    Note that you can re-use the ID of the document that triggered the Cloud Function to create this document in this specific collection.


    Another option is to use the Firebase Cloud Messaging (FCM) platform, to notify the client app. You send the message directly from your bakground Cloud Function.

    With this option you avoid paying for the reads and writes of the Firestore documents mentioned in the above option.