Skip to content

Commit 8ab3632

Browse files
committed
added http and baclground function
1 parent 80cbdbc commit 8ab3632

File tree

1 file changed

+60
-0
lines changed
  • module7-cloud-computing/r4.1-cloud-functions

1 file changed

+60
-0
lines changed

module7-cloud-computing/r4.1-cloud-functions/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,69 @@ Cloud events are things that happen in your cloud environment. These might be th
2525

2626
Events occur whether or not you choose to respond to them. You create a response to an event with a trigger. A trigger is a declaration that you are interested in a certain event or set of events. Binding a function to a trigger allows you to capture and act on events.
2727

28+
## Types of Cloud Functions
29+
30+
There are two distinct types of Cloud Functions: HTTP functions and event-driven functions. Event-driven functions can be either background functions or CloudEvent functions, depending on which Cloud Functions runtime they are written for.
31+
32+
## HTTP Functions
33+
34+
You invoke HTTP functions from standard HTTP requests. These HTTP requests wait for the response and support handling of common HTTP request methods like GET, PUT, POST, DELETE and OPTIONS. When you use Cloud Functions, a TLS certificate is automatically provisioned for you, so all HTTP functions can be invoked via a secure connection.
35+
36+
```js
37+
const escapeHtml = require("escape-html");
38+
39+
/**
40+
* HTTP Cloud Function.
41+
*
42+
* @param {Object} req Cloud Function request context.
43+
* More info: https://expressjs.com/en/api.html#req
44+
* @param {Object} res Cloud Function response context.
45+
* More info: https://expressjs.com/en/api.html#res
46+
*/
47+
exports.helloHttp = (req, res) => {
48+
res.send(`Hello ${escapeHtml(req.query.name || req.body.name || "World")}!`);
49+
};
50+
```
51+
52+
## Background Functions
53+
54+
Before moving into background functions, we need to understand this term, Pub/Sub.
55+
56+
Pub/Sub, which stands for Publisher/Subscriber, allows services to communicate asynchronously, with latencies on the order of 100 milliseconds.
57+
58+
Pub/Sub is used for streaming analytics and data integration pipelines to ingest and distribute data. It is equally effective as messaging-oriented middleware for service integration or as a queue to parallelize tasks.
59+
60+
<b>What is background functions?</b>
61+
62+
A background function is one type of event-driven function.
63+
You use background functions when you want to have your Cloud Function invoked indirectly in response to an event, such as a message on a Pub/Sub topic, a change in a Cloud Storage bucket, or a Firebase event.
64+
65+
Pub/Sub example
66+
67+
This example shows a Cloud Function triggered by Pub/Sub events. Every time a message is published to a Pub/Sub topic, the function is invoked, and a greeting using data derived from the message is written to the log
68+
69+
```js
70+
/**
71+
* Background Cloud Function to be triggered by Pub/Sub.
72+
* This function is exported by index.js, and executed when
73+
* the trigger topic receives a message.
74+
*
75+
* @param {object} message The Pub/Sub message.
76+
* @param {object} context The event metadata.
77+
*/
78+
exports.helloPubSub = (message, context) => {
79+
const name = message.data
80+
? Buffer.from(message.data, "base64").toString()
81+
: "World";
82+
83+
console.log(`Hello, ${name}!`);
84+
};
85+
```
86+
2887
---
2988

3089
## References
3190

3291
- https://developers.google.com/learn/topics/functions
3392
- https://cloud.google.com/functions
93+
- https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/

0 commit comments

Comments
 (0)