Skip to content

docs: update pub sub docs with instructions that work #109

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 1 commit into from
Nov 25, 2019
Merged
Changes from all 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
62 changes: 51 additions & 11 deletions docs/events.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
## Local Testing of Cloud Events
# Events

The setup for cloud functions that accept events is very similar to the instructions in the quickstart, with the following adjustments:
This guide shows you how to use the Functions Framework for local testing with:

In your `package.json`, add a signature type (in bold) to your start command:
- CloudEvents
- The Pub/Sub Emulator

<pre>
## Local Testing of CloudEvents

In your `package.json`, specify `--signature-type=event"` for the `functions-framework`:

```sh
{
"scripts": {
"start": "functions-framework --target=helloWorld <b>--signature-type=event"</b>
"start": "functions-framework --target=helloWorld --signature-type=event"
}
</pre>
}
```

Upon running ```sh npm start ```, you'll see the function is still being served at `http://localhost:8080/`. However, it is no longer accessible via GET requests from the browser. Instead, send a POST request where the request body conforms to the API defined by [push subscriptions](https://cloud.google.com/pubsub/docs/push).
Create an `index.js` file:

### Submitting POST Request to Simulate Pub/Sub messages
```js
exports.helloPubSub = (data, context) => {
console.log(data);
}
```

Start the Functions Framework:

```sh
npm start
```

Create a `mockPubsub.json` file with the following contents:
Your function will be serving at `http://localhost:8080/`, however,
it is no longer accessible via `HTTP GET` requests from the browser.
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if you send it a GET request?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You get a standard HTTP 404 response with Express' default Cannot GET ${PATH} error. I don't think this is worth additionally documenting/reading.


Instead, send a POST request where the request body conforms to the API defined by [push subscriptions](https://cloud.google.com/pubsub/docs/push).

### Send POST Request to Simulate Pub/Sub messages

Create a new file `mockPubsub.json` with the following contents:

```json
{
Expand All @@ -29,7 +53,9 @@ Create a `mockPubsub.json` file with the following contents:
}
```

The file can be in any folder on your computer. From the terminal, go to the directory where `mockPubsub.json` is located, and run the following command (assuming your cloud function is hosted locally on port 8080):
In the same directory as `mockPubsub.json`,
use `curl` to invoke the Functions Framework including
the Pub/Sub JSON in the POST data:

```sh
curl -d "@mockPubsub.json" \
Expand All @@ -38,10 +64,24 @@ curl -d "@mockPubsub.json" \
-H "Ce-Specversion: true" \
-H "Ce-Source: true" \
-H "Ce-Id: true" \
-H "Content-Type: application/json" \
http://localhost:8080
```

In the shell that is running the Functions Framework, you'll see the message logged:

```js
{
message: {
attributes: { key: 'value' },
data: 'SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==',
messageId: '136969346945'
},
subscription: 'projects/myproject/subscriptions/mysubscription'
}
```

### Using the Pub/Sub emulator
## Using the Pub/Sub emulator

Another way to test your cloud function Pub/Sub endpoint is to use the [Pub/Sub Emulator](https://cloud.google.com/pubsub/docs/emulator). This allows you to use the Pub/Sub notification from another service to trigger your cloud function.

Expand Down