Writing and Responding to Pub/Sub Messages

Google Cloud Pub/Sub provides reliable, many-to-many,
asynchronous messaging between applications. Publisher applications can send
messages to a topic, and other applications can subscribe to that topic to
receive the messages.

This document describes how to use the Google Cloud Client
Library
to send and receive Google Cloud Pub/Sub messages in an app running in the
flexible environment.

Code review

The sample app uses the values you set in the app.yaml file to configure
environment variables. The push request handler uses these values to confirm
that the request came from Pub/Sub and originated from a trusted source:

Run the sample locally

When running locally, you can use the Google Cloud SDK to provide authentication
to use Google Cloud APIs. Assuming you set up your environment as described in
Prerequisites, you have already run the gcloud init command,
which provides this authentication.

Install dependencies using Composer:

composer install

Then set environment variables before starting your application:

Simulate push notifications

The application can send messages locally, but it is not able to receive push
messages locally. You can, however, simulate a push message by making an HTTP
request to the local push notification endpoint. The sample includes the file
sample_message.json.

After the request completes, you can refresh localhost:8080 and see the
message in the list of received messages.

Run on App Engine

To deploy the demo app to App Engine by using the gcloud command-line tool,
you run the following command from the directory where your app.yaml is
located:

gcloud app deploy

You can now access the application at https://[YOUR_PROJECT_ID].appspot.com. You
can use the form to submit messages, but there's no guarantee of which instance
of your application will receive the notification. You can send multiple
messages and refresh the page to see the received message.