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.
- Follow the instructions in "Hello, World!" for Ruby on App Engine to set up your environment and project, and to understand how App Engine Ruby apps are structured.
- Write down and save your project ID, because you will need it to run the
sample application described in this document.
Clone the sample app
Copy the sample apps to your local machine, and navigate to the
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:
gcloud beta pubsub topics create YOUR_TOPIC_NAME gcloud beta pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic YOUR_TOPIC_NAME \ --push-endpoint \ https://YOUR_PROJECT_ID.appspot.com/pubsub/push?token=YOUR_TOKEN \ --ack-deadline 10
YOUR_TOKENwith a secret random token. The push endpoint uses this to verify requests.
app.yamlto set the environment variables for your project ID, topic, and verification token:
env_variables: PUBSUB_TOPIC: <your-topic-name> # This token is used to verify that requests originate from your # application. It can be any sufficiently random string. PUBSUB_VERIFICATION_TOKEN: <your-token>
The sample app uses the Google Cloud Client Libraries.
The sample app uses the values you set in the
app.yamlfile 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:
topic = pubsub.topic ENV["PUBSUB_TOPIC"] PUBSUB_VERIFICATION_TOKEN = ENV["PUBSUB_VERIFICATION_TOKEN"]
The sample app maintains a global list to store messages received by this instance:
# List of all messages received by this instance messages = 
This method receives pushed messages and adds them to the
post "/pubsub/push" do halt 400 if params[:token] != PUBSUB_VERIFICATION_TOKEN message = JSON.parse request.body.read payload = Base64.decode64 message["message"]["data"] messages.push payload end
This method interacts with the App Engine web app to publish new messages and display received messages:
get "/" do @messages = messages slim :index end post "/publish" do topic.publish params[:payload] redirect "/" end
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 initcommand, which provides this authentication.
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
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
You can use
curlor httpie to send an HTTP
curl -i --data @sample_message.json ":8080/pubsub/push?token=[your-token]"
http POST ":8080/pubsub/push?token=[your-token]" < sample_message.json
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:8080and see the message in the list of received messages.
Run on App Engine
To deploy the demo app to App Engine by using the
gcloudcommand-line tool, you run the following command from the directory where your
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.