Region ID
The REGION_ID
is an abbreviated code that Google assigns
based on the region you select when you create your app. The code does not
correspond to a country or province, even though some region IDs may appear
similar to commonly used country and province codes. For apps created after
February 2020, REGION_ID.r
is included in
App Engine URLs. For existing apps created before this date, the
region ID is optional in the URL.
Learn more about region IDs.
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 Cloud Client Libraries to send and receive Pub/Sub messages in an App Engine app.Prerequisites
- Follow the instructions to build an app for your selected runtime to set up your environment and project, and to understand how App Engine apps are structured.
- Write down and save your project ID, because you will need it to run the sample application described in this document.
- Create a service account and service account key to use with your application.
-
Enable the Google Cloud Pub/Sub API.
Cloning the sample app
Copy the sample apps to your local machine, and navigate to the pubsub
directory:
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
cd golang-samples/appengine/go11x/pubsub/authenicated_push
Java
No example available for this runtime.
Note that Java demo apps are available in the flexible environment.
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/appengine/pubsub
PHP
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
cd php-docs-samples/pubsub
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/appengine/standard_python3/pubsub
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples
cd ruby-docs-samples/appengine/pubsub
Create a topic and subscription
Create a topic and subscription, which includes specifying the endpoint to which the Pub/Sub server should send requests:
Go
# Configure the topic gcloud pubsub topics create YOUR_TOPIC_NAME # Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10
Replace YOUR_TOKEN
with a secret random token. The push endpoint uses this
to verify requests.
To use Pub/Sub with authentication, create another subscription:
# Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\ --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10 # Your service agent # `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the # `iam.serviceAccountTokenCreator` role. PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com" gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\ --role='roles/iam.serviceAccountTokenCreator'
Replace YOUR-SERVICE-ACCOUNT-EMAIL
with your service account email.
Java
# Configure the topic gcloud pubsub topics create YOUR_TOPIC_NAME # Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10
Replace YOUR_TOKEN
with a secret random token. The push endpoint uses this
to verify requests.
To use Pub/Sub with authentication, create another subscription:
# Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\ --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10 # Your service agent # `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the # `iam.serviceAccountTokenCreator` role. PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com" gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\ --role='roles/iam.serviceAccountTokenCreator'
Replace YOUR-SERVICE-ACCOUNT-EMAIL
with your service account email.
Node.js
# Configure the topic gcloud pubsub topics create YOUR_TOPIC_NAME # Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10
Replace YOUR_TOKEN
with a secret random token. The push endpoint uses this
to verify requests.
To use Pub/Sub with authentication, create another subscription:
# Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\ --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10 # Your service agent # `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the # `iam.serviceAccountTokenCreator` role. PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com" gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\ --role='roles/iam.serviceAccountTokenCreator'
Replace YOUR-SERVICE-ACCOUNT-EMAIL
with your service account email.
PHP
# Configure the topic gcloud pubsub topics create YOUR_TOPIC_NAME # Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10
Replace YOUR_TOKEN
with a secret random token. The push endpoint uses this
to verify requests.
To use Pub/Sub with authentication, create another subscription:
# Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\ --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10 # Your service agent # `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the # `iam.serviceAccountTokenCreator` role. PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com" gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\ --role='roles/iam.serviceAccountTokenCreator'
Replace YOUR-SERVICE-ACCOUNT-EMAIL
with your service account email.
Python
# Configure the topic gcloud pubsub topics create YOUR_TOPIC_NAME # Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10
Replace YOUR_TOKEN
with a secret random token. The push endpoint uses this
to verify requests.
To use Pub/Sub with authentication, create another subscription:
# Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\ --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10 # Your service agent # `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the # `iam.serviceAccountTokenCreator` role. PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com" gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\ --role='roles/iam.serviceAccountTokenCreator'
Replace YOUR-SERVICE-ACCOUNT-EMAIL
with your service account email.
Ruby
# Configure the topic gcloud pubsub topics create YOUR_TOPIC_NAME # Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10
Replace YOUR_TOKEN
with a secret random token. The push endpoint uses this
to verify requests.
To use Pub/Sub with authentication, create another subscription:
# Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\ --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10 # Your service agent # `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the # `iam.serviceAccountTokenCreator` role. PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com" gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\ --role='roles/iam.serviceAccountTokenCreator'
Replace YOUR-SERVICE-ACCOUNT-EMAIL
with your service account email.
Setting environment variables
Go
Edit the app.yaml
file to set the environment variables for your topic and
verification token:
Java
Edit the app.yaml
file to set the environment variables for your topic and
verification token:
env_variables:
PUBSUB_TOPIC: <your-topic-name>
PUBSUB_VERIFICATION_TOKEN: <your-verification-token>
Node.js
Edit your app.yaml
file to set the environment variables for your topic and
verification token:
PHP
Edit your index.php
file to set the environment variables for your topic and
subscription:
Python
Edit the app.yaml
file to set the environment variables for your project ID,
topic, and verification token:
Ruby
Edit the app.standard.yaml
file to set the environment variables for your project ID,
topic, and verification token:
Code review
The sample app uses the Pub/Sub Client Library.
Go
The sample app uses the environment variables you set in the app.yaml
file
(PUBSUB_TOPIC
and PUBSUB_VERIFICATION_TOKEN
) for configuration.
The messages received by this instance are stored in a slice:
messages []string
The receiveMessagesHandler
function receives pushed messages, verifies the token, and
adds the message to the messages
slice:
Java
No example available for this runtime.
Note that a Java demo app is available in the flexible environment.
Node.js
The sample app uses the values you set in your 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:
// The following environment variables are set by the `app.yaml` file when
// running on App Engine, but will need to be manually set when running locally.
var PUBSUB_VERIFICATION_TOKEN = process.env.PUBSUB_VERIFICATION_TOKEN;
var pubsub = gcloud.pubsub({
projectId: process.env.GOOGLE_CLOUD_PROJECT
});
var topic = pubsub.topic(process.env.PUBSUB_TOPIC);
The sample app maintains a global list to store messages received by this instance:
// List of all messages received by this instance
var messages = [];
This method receives pushed messages and adds them to the messages
global list:
This method interacts with the App Engine web app to publish new messages and display received messages:
PHP
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:
The sample app maintains a global list to store messages received by this instance:
$messages = [];
The pull
method retrieves messages from the topic you created and adds them
to the messages list:
The publish
method publishes new messages to the topic:
Python
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:
app.config['PUBSUB_VERIFICATION_TOKEN'] = \
os.environ['PUBSUB_VERIFICATION_TOKEN']
app.config['PUBSUB_TOPIC'] = os.environ['PUBSUB_TOPIC']
The sample app maintains a global list to store messages received by this instance:
MESSAGES = []
The receive_messages_handler()
method receives pushed messages and adds them to the MESSAGES
global list:
The index()
method interacts with the App Engine web app to publish
new messages and display received messages:
Ruby
The sample app uses the values you set in the app.standard.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:
The sample app maintains a global list to store messages received by this instance:
This method receives pushed messages and adds them to the messages
global list:
This method interacts with the App Engine web app to publish new messages and display received messages:
Running the sample locally
When running locally, you can use the Google Cloud CLI 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.
Go
Set environment variables before starting your application:
export GOOGLE_CLOUD_PROJECT=[your-project-id]
export PUBSUB_VERIFICATION_TOKEN=[your-token]
export PUBSUB_TOPIC=[your-topic]
go run pubsub.go
Java
Set environment variables before starting your application:
export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]
To run your application locally, use the development tools that you usually use.
Node.js
Set environment variables before starting your application:
export GOOGLE_CLOUD_PROJECT=[your-project-id]
export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]
npm install
npm start
PHP
Install dependencies using Composer:
composer install
Then set environment variables before starting your application:
export GOOGLE_CLOUD_PROJECT=[your-project-id]
export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]
php -S localhost:8080
Python
Install dependencies, preferably in a virtual environment.
Mac OS / Linux
- Create an
isolated Python environment:
python3 -m venv env
source env/bin/activate
- If you're not in the directory that contains the sample code, navigate
to the directory that contains the
hello_world
sample code. Then install dependencies:cd YOUR_SAMPLE_CODE_DIR
pip install -r requirements.txt
Windows
Use PowerShell to run your Python packages.
- Locate your installation of PowerShell.
- Right-click on the shortcut to PowerShell and start it as an administrator.
- Create an
isolated Python environment.
python -m venv env
.\env\Scripts\activate
- Navigate to your project directory and install dependencies. If you're not in the directory
that contains the sample code, navigate to the directory that contains the
hello_world
sample code. Then, install dependencies:cd YOUR_SAMPLE_CODE_DIR
pip install -r requirements.txt
Then set environment variables before starting your application:
export GOOGLE_CLOUD_PROJECT=[your-project-id]
export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]
python main.py
Ruby
Install dependencies:
bundle install
Then set environment variables before starting your application:
export GOOGLE_CLOUD_PROJECT=[your-project-id]
export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]
bundle exec ruby app.rb -p 8080
Simulating 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
.
Go
You can use curl
or
a httpie
client to
send an HTTP POST
request:
curl -H "Content-Type: application/json" -i --data @sample_message.json "localhost:8080/push-handlers/receive_messages?token=[your-token]"
Or
http POST ":8080/push-handlers/receive_messages?token=[your-token]" < sample_message.json
Response:
HTTP/1.1 200 OK
Date: Tue, 13 Nov 2018 16:04:18 GMT
Content-Length: 0
After the request completes, you can refresh localhost:8080
and see the
message in the list of received messages.
Java
You can use curl
or
a httpie
client to
send an HTTP POST
request:
curl -H "Content-Type: application/json" -i --data @sample_message.json "localhost:8080/push-handlers/receive_messages?token=[your-token]"
Or
http POST ":8080/push-handlers/receive_messages?token=[your-token]" < sample_message.json
After the request completes, you can refresh localhost:8080
and see the
message in the list of received messages.
Node.js
You can use curl
or
a httpie
client to
send an HTTP POST
request:
curl -H "Content-Type: application/json" -i --data @sample_message.json "localhost:8080/push-handlers/receive_messages?token=[your-token]"
Or
http POST ":8080/push-handlers/receive_messages?token=[your-token]" < sample_message.json
Response:
HTTP/1.1 200 OK
Connection: keep-alive
Date: Mon, 31 Aug 2015 22:19:50 GMT
Transfer-Encoding: chunked
X-Powered-By: Express
After the request completes, you can refresh localhost:8080
and see the
message in the list of received messages.
PHP
You can use curl
or
a httpie
client to
send an HTTP POST
request:
curl -i --data @sample_message.json "localhost:4567/push-handlers/receive_messages?token=[your-token]"
Or
http POST ":4567/push-handlers/receive_messages?token=[your-token]" < sample_message.json
After the request completes, you can refresh localhost:8080
and see the
message in the list of received messages.
Python
You can use curl
or
a httpie
client to
send an HTTP POST
request:
curl -H "Content-Type: application/json" -i --data @sample_message.json "localhost:8080/pubsub/push?token=[your-token]"
Or
http POST ":8080/pubsub/push?token=[your-token]" < sample_message.json
Response:
HTTP/1.0 200 OK
Content-Length: 2
Content-Type: text/html; charset=utf-8
Date: Mon, 10 Aug 2015 17:52:03 GMT
Server: Werkzeug/0.10.4 Python/2.7.10
OK
After the request completes, you can refresh localhost:8080
and see the
message in the list of received messages.
Ruby
You can use curl
or
a httpie
client to
send an HTTP POST
request:
curl -i --data @sample_message.json "localhost:4567/push-handlers/receive_messages?token=[your-token]"
Or
http POST ":4567/push-handlers/receive_messages?token=[your-token]" < sample_message.json
Response:
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
Content-Length: 13
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Server: WEBrick/1.3.1 (Ruby/2.3.0/2015-12-25)
Date: Wed, 20 Apr 2016 20:56:23 GMT
Connection: Keep-Alive
Hello, World!
After the request completes, you can refresh localhost:8080
and see the
message in the list of received messages.
Running on App Engine
To deploy the demo app to App Engine using the gcloud
command-line
tool:
Go
Run the following command from the directory where your app.yaml
file is located:
gcloud app deploy
Java
Run the gcloud
command from the directory where your app.yaml
file is
located:
gcloud app deploy
To deploy your app using Maven, run the following:
mvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID
Replace PROJECT_ID with the ID of your Google Cloud project. If
your pom.xml
file already
specifies your
project ID
, you don't need to include the -Dapp.deploy.projectId
property in the
command you run.
Node.js
Run the following command from the directory where your app.yaml
file
is located:
gcloud app deploy
PHP
Run the following command from the directory where your app.yaml
file is located:
gcloud app deploy
Python
Run the following command from the directory where your app.yaml
file is located:
gcloud app deploy
Ruby
Run the following command from the directory where your app.yaml
file is located:
gcloud app deploy app.standard.yaml
You can now access the application at
https://PROJECT_ID.REGION_ID.r.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.