All Things IoT | Losant Blog

Getting Started with Google Pub/Sub and Losant

Written by Taron Foxworth | Fri, May 26, 2017

The Google Cloud Platform (GCP) contains an array of robust tools to aid in building scalable applications. For IoT, Google Pub/Sub really shines. Google Pub/Sub is a complete publish/subscribe solution that allows you to quickly integrate systems into GCP and vice versa.

We just released a direct integration into Google Pub/Sub. Losant users can now integrate their applications and data into the GCP family of tools. For example, one could integrate Losant and Google Machine Learning to enable predictive analysis into their IoT applications.

Google Pub/Sub users can now send data to Losant to do a variety of things: react to data using our powerful workflow engine, build dashboards using our visualization tools, and easily build APIs on top of their data.

This tutorial will cover how to send messages from Google Pub/Sub to Losant and vice versa. 

Understanding Google Pub/Sub

Google Pub/Sub uses common pub/sub models. You have the ability to publish to a topic, and build subscribers that listen to a topic. If you've used MQTT before, these concepts should be familiar, but there are some major differences between the two.

 

 

To begin, you'll want to have a topic set up in Google Pub/Sub. If you are new to Google Pub/Sub, I recommend doing the Pub/Sub Quickstart. The quickstart walks through creating your first topic.

Create a Topic in Google Pub/Sub

If you are already using Google Pub/Sub, feel free to use an existing topic, or create a new one for this purpose.

For this demo, I have a project called "My Project". I enabled Google Pub/Sub and created a topic called "MyTopic".

 

Losant

You'll need to set up an integration in Losant. If you haven't already, sign up for a Losant account and create an application. Then, select Integrations to add a new integration.

Next, select Google Pub/Sub as your "Integration Type".

As you can see, we need to provide Losant with more information: Project ID, Configuration, and Topics.

The project ID is the ID of the project we want to work with in GCP. We can obtain the project ID in the Google Console.

The configuration object is a JSON object given to a GCP user after creating a GCP Service Account. This configuration gives Losant the ability to publish and subscribe to Google Pub/Sub on behalf of your project. To obtain the configuration object, we'll need to create a new service account in Google Cloud.

After you create a new service account, you'll be prompted to download a JSON file with the proper credentials. Copy and paste this into the “Configuration” field.

The last value needed in Losant is a topic. Earlier, I created a topic called "MyTopic". For you, this would be the topic you created in the first step. Be mindful that topics are case sensitive.

Once all done, the final output should look something like this:

You have successfully integrated Google Pub/Sub and Losant. Let's go through two simple examples of what this integration looks like in both directions.

Google Pub/Sub to Losant

As I mentioned before, Google Pub/Sub users can send data to Losant and trigger our workflow engine. First, we need to create a workflow in Losant.

Once you have a workflow, you can select the Google Pub/Sub Trigger. When this trigger is selected, you can configure Losant to listen to a certain topic and fire a workflow when a message comes in.

Once you have access to the workflow engine, you can build complex rules and/or logic for your data, and even react to it in real-time.

LOSANT to Google Pub/Sub

Create another workflow in Losant. Now, for simplicity we can use the Virtual Button Trigger to tigger this workflow on demand.

Next, drag-and-drop the Google Pub/Sub Output Node. Using this workflow, everytime we press the virtual button, we publish a message directly from Losant to Google Pub/Sub. 

In this case, we used the virtual button for testing. One could easily use a Device Trigger, or even an Endpoint Trigger a publish to Google Pub/Sub from an endpoint. 

What's Next?

Now with the power of Losant and Google Pub/Sub, it's possible to architect and create a complete powerful IoT solution. Here are some helpful resources: 

Make sure you follow us on Twitter for updates about future integrations between Losant and Google Pub/Sub.