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 Librariesto send and receive Pub/Sub messages in an App Engine app.
PrerequisitesEnable the Google Cloud Pub/Sub API.
Copy the sample apps to your local machine, and navigate to the pubsub
directory:
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.jsgit 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.
# 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.
# 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.
# 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.
# 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.
# 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.
Edit the app.yaml
file to set the environment variables for your topic and verification token:
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:
Edit your index.php
file to set the environment variables for your topic and subscription:
Edit the app.yaml
file to set the environment variables for your project ID, topic, and verification token:
Edit the app.standard.yaml
file to set the environment variables for your project ID, topic, and verification token:
The sample app uses the Pub/Sub Client Library.
GoThe 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
Note: This tutorial is using an instance level global variable, messages
, to store messages received from Pub/Sub. In production, that should be converted to an external storage solution, such as Firestore.
The receiveMessagesHandler
function receives pushed messages, verifies the token, and adds the message to the messages
slice:
No example available for this runtime.
Note that a Java demo app is available in the flexible environment.
Node.jsThe 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 = [];
Note: This tutorial is using an instance level global variable, messages
, to store messages received from Pub/Sub. In production, that should be converted to an external storage solution, such as Firestore.
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:
PHPThe 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 = [];
Note: This tutorial is using an instance level global variable, $messages
, to store messages received from Pub/Sub. In production, that should be converted to an external storage solution, such as Firestore.
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:
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 = []
Note: This tutorial is using an instance level global variable, MESSAGES
, to store messages received from Pub/Sub. In production, that should be converted to an external storage solution, such as Firestore.
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:
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:
messages
, to store messages received from Pub/Sub. In production, that should be converted to an external storage solution, such as Firestore.
This method interacts with the App Engine web app to publish new messages and display received messages:
Running the sample locallyWhen 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.
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.jsSet 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 / Linuxpython3 -m venv env
source env/bin/activate
hello_world
sample code. Then install dependencies:
cd YOUR_SAMPLE_CODE_DIR
pip install -r requirements.txt
Use PowerShell to run your Python packages.
python -m venv env
.\env\Scripts\activate
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
.
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.
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.
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.
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.
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.
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.
To deploy the demo app to App Engine using the gcloud
command-line tool:
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.
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4