GCP Pub/Sub
Pub/Sub is an increasingly popular component of many cloud-driven solutions. Producers publish messages to a topic, and subscribers consume them typically triggering some type of work. Pub/Sub queues are also very important when it comes to high-throughput data ingress, typically synonymous with IoT solutions. I will show you how to publish to and consume messages on the Google Cloud Platform using the REST-based API, authenticated with OAuth and using C#.

Getting Started

If you haven’t already, create a Google Cloud account (they offer a 60 day free trial with a $300 credit). You will then need to create a project. A project is exactly what it sounds like, it’s an encapsulation of all the technologies organized by project, I’ve named mine CSharpPubSub. From the upper left hamburger menu in the console select Pub/Sub, enable the API (if you haven’t done this yet), then create a topic that will be used throughout this article.
Creating a Pub/Sub Topic

Creating a Pub/Sub Topic

 

Next you will need to create a service account to authenticate to Pub/Sub with. Please review this <LINK>post</LINK> for instructions on how to do so. The service account will require the Pub/Sub Editor role, as we will be creating a subscription to our topic. Be sure to make a copy of the credential helper class located at the bottom of the post for our use in this example. The scope we will be authenticating to will be https://www.googleapis.com/auth/pubsub .

Pub/Sub Topic Subscriptions

There are two types of consumers of a GCP topic, one is a Push subscription, the other is a Pull subscription. Both are alike in the sense that they are subscriptions to a Pub/Sub topic, but where they differ is in how they behave. When a Push subscription is created, it is supplied with an http endpoint that works as a callback when a message is published onto the topic. A Pull subscription is used when an http endpoint is not available, this is also the typical model used when implementing a polling based-solution on the topic. In this example, I will be providing a helper class that will allow you to publish to a GCP Pub/Sub topic, as well as use a pull subscription to consume messages from a topic, this way a callback endpoint is not required.
GCP Pub/Sub

GCP Pub/Sub

The Pub/Sub Message

A Pub/Sub message takes the form of a Base64 encoded message, as well as a key/value pair collection of attributes. These attributes can be anything, in our case we will be using the properties of a POCO object as our attributes. Pub/Sub messages from the REST-ful API also contain fields for a MessageId and PublishTime, both of which are populated by the server upon receiving the message.

Pull Subscription Helper Class

Find below the listing of a helper class that will make publishing and consuming to Google Cloud Platform Pub/Sub topics easy. This class makes use of the GCP_OAuth_ServiceCredential class to handle the OAuth authentication of the REST-ful API calls. This class also handles the creation of a topic subscription on the Google Cloud Platform (hence needing the service acccount to have the Pub/Sub editor role). This class uses generics to take any serializable type T to publish and consume as attributes in a Pub/Sub message. As we are using a Pull subscription, you will also notice code acknowledging the receipt of messages from the topic.

Example Usage

Create a new console application, and bring in both the GCP_OAuth_ServiceCredential and GCP_OAuth_Retful_Pull_PubSub classes. Don’t forget the P12 key file for the Service account credentials! Now we need a class to be used as the key/value attributes of the message. In my case, I created the following simple class:
Next add the following static method to the Program.cs file, this method instantiates the service credentials, as well as publishes messages, and pulls them (and in turn acknowledges) from the topic:

Finally, call this method from your main method:

Restful Pub/Sub Console

Restful Pub/Sub Console

The following two tabs change content below.
Carey Payette is a Senior Software Engineer with Falafel Software as well as an ASPInsider. She has interests in IoT and is a member of the Maker community. Carey is also a wife, and mom to 3 fabulous boys. She is a 2nd degree black belt in TaeKwonDo and enjoys coding for fun!