Trigger Functions Directly
Discover ways to call and trigger Cloud Functions directly.
We'll cover the following
In the Introduction to Cloud Functions lesson, we mentioned that Cloud Functions could either be called directly or triggered in response to an event. Since we have a brief overview of how Cloud Functions work, let’s go deeper to see ways in which we can directly invoke our Cloud Functions.
There are three ways we can call Firebase Cloud Functions directly, each with its unique operation and usage. These methods are as follows:
- Schedule a Cloud Function to run at specific times or intervals.
- Trigger a function in response to an HTTP request.
- Calling a Cloud Function directly from the Firebase App. This means calling it from the client.
For this course, we’ll focus on triggering functions via HTTP requests and calling functions directly from an application.
Trigger via HTTP requests
To write an HTTP request function, we must use the onRequest
handler that exists on the functions.https
object. Just like in an Express application, it takes two object parameters, request
and response
. The request
object represents the properties of the request sent by the client, while the response
object provides a way to send a response back to the client.
We can terminate HTTP functions with either of three following methods on the response object— .send()
, .redirect()
, or .end()
. We already saw an example of a simple HTTP request function in the previous lesson. Now, let’s write something more complex:
Get hands-on with 1200+ tech skills courses.