Skip to content

docs: Update pub/sub quickstart to use background event #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ response instead.
1. Create a `main.py` file with the following contents:

```python
def hello(request):
return "Hello world!"
def hello(event, context):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would stick to data, context to update this Pub/Sub quickstart. See other comments for more details.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

print("Received", context.event_id)
```

1. Start the Functions Framework on port 8080:

```sh
functions-framework --target=hello --debug --port=8080
functions-framework --target=hello --signature-type=event --debug --port=8080
```

1. In a second terminal, start the Pub/Sub emulator on port 8085.
Expand Down Expand Up @@ -172,7 +172,7 @@ response instead.
pip install -r requirements.txt

python publisher.py $PUBSUB_PROJECT_ID create $TOPIC_ID
python subscriber.py $PUBSUB_PROJECT_ID create-push $TOPIC_ID $PUSH_SUBSCRIPTION_ID http://localhost:8085
python subscriber.py $PUBSUB_PROJECT_ID create-push $TOPIC_ID $PUSH_SUBSCRIPTION_ID http://localhost:8080
python publisher.py $PUBSUB_PROJECT_ID publish $TOPIC_ID
```

Expand Down Expand Up @@ -204,6 +204,41 @@ response instead.
Published messages to projects/my-project/topics/my-topic.
```

And in the terminal where the Functions Framework is running:

```none
* Serving Flask app "hello" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
* Restarting with fsevents reloader
* Debugger is active!
* Debugger PIN: 911-794-046
Received 1
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
Received 2
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
Received 5
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
Received 6
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
Received 7
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
Received 8
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
Received 9
127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
Received 3
127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
Received 4
127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
```

For more details on extracting data from a Pub/Sub event, see
https://cloud.google.com/functions/docs/tutorials/pubsub#functions_helloworld_pubsub_tutorial-python

### Quickstart: Build a Deployable Container

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

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

```python
def hello(data, context):
print(data)
def hello(event, context):
print(event)
print(context)
```
Comment on lines +301 to 309
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, we refer to the whole event as an event, (like event and cloudevent), not the first param.

The event we refer to usually has two properties, data, and context. I suggest we keep the same terms here. This is like the docs are currently, and the Node Functions Framework. Although Java uses event.

If we want to use the term event, context, I suggest we do so in a separate PR and have a plan to update the docs at the same time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW in most other language samples we use (message, context). I don't think it really matters much TBH


Expand Down
2 changes: 1 addition & 1 deletion examples/cloud_run_event/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
# limitations under the License.


def hello(data, context):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not update this identifier in this PR. Maybe a separate PR.

def hello(event, context):
pass