Google Cloud Pub/Sub

cps_integration

During my time at Adobe, I used to work on Pub/Sub messaging part of Flex/Livecycle Data Services and I also worked on Java Message Service (JMS) integration of Flex apps to JEE backends. So, it’s not a surprise that I was pretty excited to learn about Google Cloud Pub/Sub.

What is Google Cloud Pub/Sub?

  • Messaging middleware for Google Cloud Platform. Similar to Java Message Service (JMS) in the Java world but in the cloud.
  • Provides many-to-many, async messaging for loosely coupled senders and receivers.
  • Low latency, durable messaging with at least once delivery guarantee (i.e. messages can be delivered more than once and out of order).
  • Both push and pull style delivery supported.
  • In a nutshell:
    • A publisher creates a topic.
    • A subscriber creates a subscription to that topic.
    • Publisher sends messages to that topic.
    • Subscriber receives the message via push or pull, depending on the configuration.
    • Subscriber ACKs the receipt and message is removed from the message queue.

Try Pub/Sub with Google APIs Explorer

Easiest way to try out Pub/Sub without writing a single line of code is via Google APIs Explorer, so let’s try that. First, we need to do some prep work which involves creating a Google Cloud Platform project and activating the Google Cloud Pub/Sub API. Both of these steps are explained here.

Once you have a project and activated Pub/Sub API, go to API Explorer Pub/Sub section, turn on “Authorize requests using OAuth 2.0” with both scopes. This basically enables the browser to issue Pub/Sub requests.

Now, we’re ready to try out Pub/Sub APIs. We’ll basically do the following:

  1. Create a topic.
  2. List topics to make sure it’s created.
  3. Create a subscription to the topic.
  4. Publish a message to the topic.
  5. Pull the message from the subscription.
  6. Acknowledge the message from step 5 to get it deleted from the queue.

In my case, the GCP project id meteatameldevcloud, make sure you change this to whatever your GCP project id is.

Create a topic

Let’s create a topic named testtopic under my project meteatameldevcloud.

List topics

Let’s list all topics, to make sure testtopic has been created.

Create a subscription to the topic

Let’s create a subscription named testsubscription to the topic, so we can receive messages.

  • Go to apis-explorer/#p/pubsub/v1/pubsub.projects.subscriptions.create
  • In name field, enter projects/meteatameldevcloud/subscriptions/testsubscription
  • In request body, add topic: projects/meteatameldevcloud/topics/testtopic as a field
  • In request body, add ackDeadlineSeconds: 100. This basically increases the acknowledgement window from default 10 seconds to 100 seconds. It’s useful to give us more time while demoing.
  • Execute and you should get 200 OK.

Publish a message to the topic

Now, we’re ready to publish a message to the topic.

{"messages":[
   {"attributes":{ 
      "foo": "bar"}
    } 
]}

Pull the message from a subscription

Time to receive the message.

  • Go to apis-explorer/#p/pubsub/v1/pubsub.projects.subscriptions.pull
  • In subscription field, enter projects/meteatameldevcloud/subscriptions/testsubscription
  • In request body, add maxMessages: 1. This basically tells how many messages to receive in a pull request.
  • In request body, add returnImmediately: true. This tells the request to return immediately (instead of waiting/long-polling) if there are no messages in the queue.
  • Execute and you should get 200 OK with the message. Make a note of the ackId of the message, as you’ll need it in the next step.

Acknowledge the message

Finally, let’s ack that we received the message, so it gets deleted from the message queue.

  • Go to apis-explorer/#p/pubsub/v1/pubsub.projects.subscriptions.acknowledge
  • In subscription field, enter projects/meteatameldevcloud/subscriptions/testsubscription
  • In request body, add ackIds and include the ackId from the previous step.
  • Execute and you should get 200 OK.
  • At this point, if you try to pull the message again, you should not get the message anymore, assuming that you acknowledged the message within 100 seconds 🙂

Manage Pub/Sub with GCP Console

You can also manage pub/sub topics and subscriptions from GCP Console Pub/Sub section directly. This is useful in case you want to create or delete a new topic or a subscription and you don’t want to deal with the API or APIs Explorer.

Google Cloud Pub/Sub Java API

There’s a Java API for Google Cloud Pub/Sub and Java Pub/Sub Samples as well. I will explain how to use Pub/Sub from Java in a future post.

Resources

Advertisements

One thought on “Google Cloud Pub/Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s