Pier
Pier

Reputation: 10817

How to send a response before ending the Firebase Cloud Function execution?

I have an HTTP Firebase Cloud Function that occasionally can take very long to finish. For this particular use case, I don't want my users to wait until the function has finished to receive a 200 response.

So I do res.status(200).send(); as usual, but instead of doing it at the end of execution I've tried doing it at the start of the function. The problem is that this stops execution.

As a workaround I'm considering moving the heavy load to a different function and trigger it from a write in the Realtime Database, but it would be much easier to just have this logic in a single function.

Is there a way around this limitation?

Upvotes: 1

Views: 1414

Answers (1)

Michael Bleigh
Michael Bleigh

Reputation: 26313

There is no way to continue execution after the response has been sent. Your instinct to trigger a second, background function via a write to the Realtime Database is a good one (using Cloud Pub/Sub is also a good option).

While this may feel like extra steps, it's also generally a good practice -- you can control the retry and timing semantics of your extended processing independently of the user-critical request path. In general, this kind of thing is a textbook case for a message queue. :)

Upvotes: 9

Related Questions