Skip to content

Commit e79fc74

Browse files
authored
docs: Update pub/sub quickstart to use background event (#144)
* docs: Update pub/sub quickstart to use events * Use (event, context) in examples * Add link to Pub/Sub docs * Update ports elsewhere as well
1 parent 0205c85 commit e79fc74

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

README.md

+45-10
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ response instead.
134134
1. Create a `main.py` file with the following contents:
135135

136136
```python
137-
def hello(request):
138-
return "Hello world!"
137+
def hello(event, context):
138+
print("Received", context.event_id)
139139
```
140140

141141
1. Start the Functions Framework on port 8080:
142142

143143
```sh
144-
functions-framework --target=hello --debug --port=8080
144+
functions-framework --target=hello --signature-type=event --debug --port=8080
145145
```
146146

147147
1. In a second terminal, start the Pub/Sub emulator on port 8085.
@@ -159,7 +159,7 @@ response instead.
159159
[pubsub] INFO: Server started, listening on 8085
160160
```
161161

162-
1. In a third terminal, create a Pub/Sub topic and attach a push subscription to the topic, using `http://localhost:8085` as its push endpoint. [Publish](https://cloud.google.com/pubsub/docs/quickstart-client-libraries#publish_messages) some messages to the topic. Observe your function getting triggered by the Pub/Sub messages.
162+
1. In a third terminal, create a Pub/Sub topic and attach a push subscription to the topic, using `http://localhost:8080` as its push endpoint. [Publish](https://cloud.google.com/pubsub/docs/quickstart-client-libraries#publish_messages) some messages to the topic. Observe your function getting triggered by the Pub/Sub messages.
163163

164164
```sh
165165
export PUBSUB_PROJECT_ID=my-project
@@ -172,7 +172,7 @@ response instead.
172172
pip install -r requirements.txt
173173

174174
python publisher.py $PUBSUB_PROJECT_ID create $TOPIC_ID
175-
python subscriber.py $PUBSUB_PROJECT_ID create-push $TOPIC_ID $PUSH_SUBSCRIPTION_ID http://localhost:8085
175+
python subscriber.py $PUBSUB_PROJECT_ID create-push $TOPIC_ID $PUSH_SUBSCRIPTION_ID http://localhost:8080
176176
python publisher.py $PUBSUB_PROJECT_ID publish $TOPIC_ID
177177
```
178178

@@ -183,14 +183,14 @@ response instead.
183183
184184
topic: "projects/my-project/topics/my-topic"
185185
push_config {
186-
push_endpoint: "http://localhost:8085"
186+
push_endpoint: "http://localhost:8080"
187187
}
188188
ack_deadline_seconds: 10
189189
message_retention_duration {
190190
seconds: 604800
191191
}
192192
.
193-
Endpoint for subscription is: http://localhost:8085
193+
Endpoint for subscription is: http://localhost:8080
194194
195195
1
196196
2
@@ -204,6 +204,41 @@ response instead.
204204
Published messages to projects/my-project/topics/my-topic.
205205
```
206206

207+
And in the terminal where the Functions Framework is running:
208+
209+
```none
210+
* Serving Flask app "hello" (lazy loading)
211+
* Environment: production
212+
WARNING: This is a development server. Do not use it in a production deployment.
213+
Use a production WSGI server instead.
214+
* Debug mode: on
215+
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
216+
* Restarting with fsevents reloader
217+
* Debugger is active!
218+
* Debugger PIN: 911-794-046
219+
Received 1
220+
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
221+
Received 2
222+
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
223+
Received 5
224+
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
225+
Received 6
226+
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
227+
Received 7
228+
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
229+
Received 8
230+
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
231+
Received 9
232+
127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
233+
Received 3
234+
127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
235+
Received 4
236+
127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
237+
```
238+
239+
For more details on extracting data from a Pub/Sub event, see
240+
https://cloud.google.com/functions/docs/tutorials/pubsub#functions_helloworld_pubsub_tutorial-python
241+
207242
### Quickstart: Build a Deployable Container
208243

209244
1. Install [Docker](https://store.docker.com/search?type=edition&offering=community) and the [`pack` tool](https://buildpacks.io/docs/install-pack/).
@@ -263,13 +298,13 @@ You can configure the Functions Framework using command-line flags or environmen
263298
## Enable Google Cloud Functions Events
264299

265300
The Functions Framework can unmarshall incoming
266-
Google Cloud Functions [event](https://cloud.google.com/functions/docs/concepts/events-triggers#events) payloads to `data` and `context` objects.
301+
Google Cloud Functions [event](https://cloud.google.com/functions/docs/concepts/events-triggers#events) payloads to `event` and `context` objects.
267302
These will be passed as arguments to your function when it receives a request.
268303
Note that your function must use the `event`-style function signature:
269304

270305
```python
271-
def hello(data, context):
272-
print(data)
306+
def hello(event, context):
307+
print(event)
273308
print(context)
274309
```
275310

examples/cloud_run_event/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
# limitations under the License.
1414

1515

16-
def hello(data, context):
16+
def hello(event, context):
1717
pass

0 commit comments

Comments
 (0)