0

I'm currently looking to take in a request (using Express/Firebase Cloud Functions)and do some calculations/make some requests, send a response, but then respond to a user before updating my database or doing some extra calculations.

Use case example: Occasionally, a user will follow another user. This means that I want to update all over my database so that my data is in the right place later on. I don't need my user to wait for me to do this, this is something I can do long after the server knows the user wishes to follow them.

Can I receive the user request, perform a server action, respond to the user, then continue on with my cloud function? I have read inn places that a function terminates on responses, but it's not clear how else you go about this or if it only terminates on specific responses.

Thingamajig
  • 4,107
  • 7
  • 33
  • 61

2 Answers2

1

The way to explain is not possible to achieve, I mean, after you return from a function the process is done.

However, there are several patterns you can implement, it just matters of playing with triggers, take a look.

You can save a state on a firestore collection and trigger a background function which fetches this state and do whatever you need.

If you don't feel like saving this on collection, you have PubSub available as well.

This should give you a good starting point to investigate what is the best solution for your use case.

Hope this helps!

andresmijares
  • 3,658
  • 4
  • 34
  • 40
  • This was super helpful because it led me to document triggers! I think that's the smarter approach so I change one thing and the trigger can change hundreds/thousands. Thank you – Thingamajig Jun 05 '19 at 14:49
  • glad it helped u, I would recommend going directly into the pubsub area, that's how i normally solve this kind of cases – andresmijares Jun 05 '19 at 15:07
  • @DanFein: You should mark this answer as a solution if it helped solving your problem. – Jason Saruulo Aug 01 '20 at 21:46
0

There are two kinds of cloud Functions for Firebase: the ones that you can call directly (HTTPS Callable functions, HTTPS "simple" functions and scheduled functions) and the one that respond to events generated by some of the Firebase services or Google Cloud features like for example Firestore, Authentication events or Cloud Storage (see https://firebase.google.com/docs/functions for a full list)

Classically, when you want to send a response to a user after he/she initiated a Cloud Function, you would choose an HTTPS Callable functions or an HTTPS "simple" functions. But this is not the only way to do it: you can very well send a feedback to the user with a background triggered Cloud Function, mainly by writing something in Firestore (or the Realtime database) to a location the front-end is listening to.

With this second way, you can very well "respond to the user, then continue on with [the] cloud function". While with a callable Cloud Function when you send back the response to the user the Function is terminated.

So let's describe this method in a bit more details for Firestore:

  1. You trigger the background function, for example by writing to a Firestore document/collection from your front-end
  2. At the same time, from the front-end, you set a listener to a specific document (not necessarily the same document than above). See here for setting a listener.
  3. The Cloud Function starts to do some work and at one moment writes to this specific document in Firestore
  4. In the front-end, the user is informed of the write through the listener
  5. In the background the Cloud Function can continue its other tasks.

The key point is that you have to correctly chain all the asynchronous tasks of the Cloud Function by chaining the promises.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • So the technique for this is to essentially make the change without expecting a response from the same place, but because you set up a listener, you prepare for the response. So you're essentially sending a "Follow" somewhere, and then if you see the document get updated, you treat that as your response? – Thingamajig Jun 05 '19 at 14:53
  • Yes, you know upfront the collection/document where you are going to write the feedback of the Cloud Function. You set a listener to this doc and when you write to this doc from the Cloud Function the user gets the update in his front-end. – Renaud Tarnec Jun 05 '19 at 14:54
  • For example you have a `status` field in the doc and you update it accordingly with e.g. `started`, `success`, `error`, etc values – Renaud Tarnec Jun 05 '19 at 14:56
  • 1
    Actually, the recommended way to continue work is using a pubsub trigger. This avoids having to write a document unnecessarily. – Doug Stevenson Jun 05 '19 at 15:01
  • Thanks for that clarification @DougStevenson! – Renaud Tarnec Jun 05 '19 at 15:04