Skip to content

Commit 0765875

Browse files
docs: add quickstart for Pub/Sub emulator (GoogleCloudPlatform#126)
* docs: add quickstart for Pub/Sub emulator * nits * nits * address grant's comments * address grant's comment
1 parent 3f9e065 commit 0765875

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,78 @@ def function(request):
128128
This function will catch the `ZeroDivisionError` and return a different
129129
response instead.
130130

131+
### Quickstart: Pub/Sub emulator
132+
1. Create a `main.py` file with the following contents:
133+
134+
```python
135+
def hello(request):
136+
return "Hello world!"
137+
```
138+
139+
1. Start the Functions Framework on port 8080:
140+
141+
```sh
142+
functions-framework --target=hello --debug --port=8080
143+
```
144+
145+
1. Start the Pub/Sub emulator on port 8085.
146+
147+
```sh
148+
export PUBSUB_PROJECT_ID=my-project
149+
gcloud beta emulators pubsub start \
150+
--project=$PUBSUB_PROJECT_ID \
151+
--host-port=localhost:8085
152+
```
153+
154+
You should see the following after the Pub/Sub emulator has started successfully:
155+
156+
```none
157+
[pubsub] INFO: Server started, listening on 8085
158+
```
159+
160+
1. 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.
161+
162+
```sh
163+
export TOPIC_ID=my-topic
164+
export PUSH_SUBSCRIPTION_ID=my-subscription
165+
$(gcloud beta emulators pubsub env-init)
166+
167+
git clone https://github.com/googleapis/python-pubsub.git
168+
cd python-pubsub/samples/snippets/
169+
170+
python publisher.py $PUBSUB_PROJECT_ID create $TOPIC_ID
171+
python subscriber.py $PUBSUB_PROJECT_ID create-push $TOPIC_ID $PUSH_SUBSCRIPTION_ID http://localhost:8085
172+
python publisher.py $PUBSUB_PROJECT_ID publish $TOPIC_ID
173+
```
174+
175+
You should see the following after the commands have run successfully:
176+
177+
```none
178+
Created topic: projects/my-project/topics/my-topic
179+
180+
topic: "projects/my-project/topics/my-topic"
181+
push_config {
182+
push_endpoint: "http://localhost:8085"
183+
}
184+
ack_deadline_seconds: 10
185+
message_retention_duration {
186+
seconds: 604800
187+
}
188+
.
189+
Endpoint for subscription is: http://localhost:8085
190+
191+
1
192+
2
193+
3
194+
4
195+
5
196+
6
197+
7
198+
8
199+
9
200+
Published messages to projects/my-project/topics/my-topic.
201+
```
202+
131203
### Quickstart: Build a Deployable Container
132204

133205
1. Install [Docker](https://store.docker.com/search?type=edition&offering=community) and the [`pack` tool](https://buildpacks.io/docs/install-pack/).

0 commit comments

Comments
 (0)