---
title: "HiveMQ & Google Cloud - Enabling Bi-directional MQTT Data Movement"
description: The HiveMQ Enterprise Extension for Google Pub/Sub enables the bi-directional movement of MQTT data between HiveMQ and the Google Cloud at hyper-scale.
date: 2022-09-29
published: 2022-09-29
author: Florian Limpöck
tags:
  - Google Cloud
  - HiveMQ
  - HiveMQ Extension
  - IoT
source: https://www.hivemq.com/blog/hivemq-to-google-cloud-bidirectional-data-movement
---

# HiveMQ & Google Cloud - Enabling Bi-directional MQTT Data Movement

We’re excited to share that we have just released the HiveMQ Enterprise Extension for Google Cloud Pub/Sub.

This new extension enables the bi-directional movement of MQTT data between HiveMQ and the Google Cloud. There are two methods to accomplish this:

- Through **Mappings**, and
- Through **Transformers**.

Each method has its advantages and should be selected according to the use case requirements.

**Mappings** are helpful for more straightforward use cases. For instance, while setting up the configuration, the intended content and destination of the message is already known.

On the other hand, **Transformers** can dynamically decide the content and destination of a message at runtime.

In this article, you will learn how to transform incoming MQTT PUBLISH messages and forward them to Google Cloud Pub/Sub. From Google Cloud Pub/Sub, you can use backend data storage and analysis services to further process messages.

You will also learn how to read messages from Google Cloud Pub/Sub and forward them to subscribed MQTT clients.

The following example walks you through a hypothetical round-trip scenario involving **Transformers**.

The first **Transformer** takes the Quality of Service, Message Expiry, and the Retained Flag properties of an MQTT PUBLISH and converts them to Pub/Sub message attributes.

The second **Transformer** sets the attributes of the Pub/Sub message back to an MQTT PUBLISH with Quality of Service, Message Expiry, and the Retained Flag MQTT properties.

## Prerequisites

Before you can start implementing these transformers, check these prerequisites:

- Install or use Java 11 or higher
- Java IDE that supports Gradle (for this tutorial, we use [IntelliJ](https://www.jetbrains.com/idea/))
- [HiveMQ Professional or Enterprise Edition](https://app.hivemq.com/signup?target=software) version 4.9.0 or higher that is bundled with the HiveMQ Platform
- [HiveMQ Enterprise Extension for Google Cloud Pub/Sub](/products/extensions/google-pub-sub-extension) version 4.9.0 or higher
- Active Google Cloud Pub/Sub account with at least one topic and one subscription

The easiest way to develop transformers is via the [HiveMQ Google Cloud Pub/Sub Hello World Customization](https://github.com/hivemq/hivemq-google-cloud-pubsub-hello-world-customization) repository on GitHub:

1. To start, open your IDE and create a new project from version control.
2. ![Open your IDE and create a new project from version control](https://a.storyblok.com/f/243938/578x234/03fc91dbab/hivemq-to-google-cloud-bidirectional-data-movement-idea-project-from-version-control.webp)
3. Paste the Hello World Customization GitHub URL. `git@github.com:hivemq/hivemq-google-cloud-pubsub-hello-world-customization.git` into the URL field.
4. ![Paste the Hello World Customization GitHub URL](https://a.storyblok.com/f/243938/686x136/6252a798ce/hivemq-to-google-cloud-bidirectional-data-movement-idea-paste-github-url.webp)
5. Remove the Hello World code from the java packages of the `src/main` and `src/test` directories.
6. ![Remove the Hello World code from the java packages](https://a.storyblok.com/f/243938/347x144/5f55cc9f31/hivemq-to-google-cloud-bidirectional-data-movement-idea-clear-hello-world-code.webp)
7. (Optional) Change the group and description in `build.gradle.kts`.
8. ![Change the group and description in build.gradle.kts](https://a.storyblok.com/f/243938/802x70/c818bdca91/hivemq-to-google-cloud-bidirectional-data-movement-idea-build-change-group-description.webp)
9. (Optional) Adapt the manifest attributes in `build.gradle.kts`. (At least the vendor makes sense in most cases.)
10. ![Adapt the manifest attributes in build.gradle.kts](https://a.storyblok.com/f/243938/516x134/fc23e51844/hivemq-to-google-cloud-bidirectional-data-movement-idea-build-change-manifest.webp)
11. (Optional) Set the `rootProject.name` in `settings.gradle.kts`.
12. ![Set the rootProject.name in settings.gradle.kts](https://a.storyblok.com/f/243938/452x27/6e33278128/hivemq-to-google-cloud-bidirectional-data-movement-idea-change-module-name.webp)

Now we are all set to implement our first transformer.

## MQTT to Pub/Sub Transformer

Go to the `src/main/java` directory and create an `ExampleMqttToPubSubTransformer` class that implements the `MqttToPubSubTransformer` interface in the `example.pubsub.customizations` package.

Remember the package and the class name, we need them later in the configuration part of the extension.

### Override init()

The first questions we must ask ourselves are: “Do we need some information from the configuration file, and do we want to register some metrics?”

If the answer is yes to either of the above questions, then we need to override the `init` method of the transformer interface. In this example, we want a metric for every failed transformation, which will then be registered in the `init`.

To ensure that we can use the transformer even if the metric creation fails, we surround the creation of the metric with a `try-catch` block.

### Override transformMqttToPubSub()

1. To transform the message, we override the `transformMqttToPubSub` method,
2. Retrieve the `PublishPacket` from the `MqttToPubSubInput`, and
3. Call `newOutboundPubSubMessageBuilder()` on the `MqttToPubSubOutput` object.
4. Next, we check whether there is a message payload. If the payload doesn’t exceed the maximum length that Google Cloud Pub/Sub allows, we set it to the data field of the `OutboundPubSubMessageBuilder`.
5. If the bytes in the payload exceed the maximum length, we log an error, increase the failed metric, and do not send the message by providing an empty list to the `MqttToPubSubOutput`.
6. If the payload size is fine, we add the **Quality of Service (QoS)**, the **Message Expiry Interval**, and the **Retained Flag** to the `OutboundPubSubMessageBuilder` as attributes.
7. Finally, we set the desired destination topic and provide the built message to the `MqttToPubSubOutput`.

### Example Implementation

A complete implementation of the `ExampleMqttToPubSubTransformer` looks like this:

Now that we have a working transformer for messaging from HiveMQ to Google Cloud Pub/Sub, the next step is building a transformer for the opposite direction.

## Pub/Sub to MQTT Transformer

Go to the `src/main/java` directory and create an `ExamplePubSubToMqttTransformer` class implementing the `PubSubToMqttTransformer` interface in the `example.pubsub.customizations` package.

Remember the package and the class name, we need them later in the configuration part of the extension.

### Override init()

Again, we override the `init()` method to register a metric for every failed transformation in the same way we did in the `MqttToPubSubTransformer`.

### Override transformMqttToPubSub()

1. To transform the message, we override the `transformPubSubToMqtt` method,
2. Retrieve the `InboundPubSubMessage` from the `PubSubToMqttInput`, and
3. Call `newPublishBuilder()` on the `PubSubToMqttOutput` object.
4. Next, we convert **Quality of Service (QoS)**, **Message Expiry Interval**, and **Retained Flag** from the Pub/Sub message attributes to the respective MQTT properties and set them to the `PublishBuilder`.
5. Then, we convert the Pub/Sub message data to an MQTT payload and set it to the `PublishBuilder`.
6. Finally, we set the desired destination topic and provide the built message to the `PubSubToMqttOutput`.

### Example Implementation

A complete implementation of the `ExamplePubSubToMqttTransformer` looks like this:

Once we have a working transformer for messaging from Google Cloud Pub/Sub to HiveMQ, we are ready to install our transformers.

## Installation

1. Run the `./gradlew jar` Gradle task to build the custom transformers.
2.
3. Move the `build/libs/example-pubsub-customization-4.9.0.jar` file to the `HIVEMQ_HOME/extensions/hivemq-google-cloud-pubsub-extension/customizations` directory.
4.
5. Adapt the `google-cloud-pubsub-configuration.xml` configuration.

- Remove the mapping configurations.
- Set the `&lt;google-cloud-project-id&gt;` in the `&lt;pubsub-connection&gt;` tag to match your Google Cloud Pub/Sub project ID.
- Set the `&lt;file-path&gt;` in the `&lt;service-account&gt;` tag to the absolute or relative path of your `service-account-key.json` file.
- Create an `&lt;mqtt-to-pubsub-transformer&gt;` and a `&lt;pubsub-to-mqtt-transformer&gt;` like shown in the example below.
- Set the `&lt;pubsub-subscription&gt;` name in the `&lt;pubsub-to-mqtt-transformer&gt;` tag to match the correct Google Cloud Pub/Sub subscription.

## Testing

Now that we have a running HiveMQ with the Google Cloud Pub/Sub extension that uses two transformers for messaging between HiveMQ and Google Cloud, it’s time to do some tests.

The open-source HiveMQ MQTT CLI tool included in the `tools` directory of our HiveMQ installation makes it easy to test our deployment. We can use the MQTT CLI to send an MQTT PUBLISH message with the topic “to/pubsub” and subscribe an MQTT client to a “from/pubsub” topic. The PUBLISH message goes from HiveMQ to the Google Cloud Pub/Sub extension. In the extension, the message is processed by the transformer (`m2p-t-1`), converted to a Pub/Sub message, enriched with Pub/Sub attributes, and sent to the configured Pub/Sub topic (`my-pubsub-topic`). Google Cloud Pub/Sub then delivers the message to the subscription (`my-pubsub-subscription`). Because we subscribe to this Pub/Sub subscription with the other transformer, the extension pulls the message from Google Cloud Pub/Sub. The message is processed by the transformer (`p2m-t-1`), and an MQTT PUBLISH message is created with the given MQTT properties and sent to the topic “from/pubsub”.

![HiveMQ Platform, Google Cloud Pub/Sub and MQTT Diagram](https://a.storyblok.com/f/243938/1280x467/78fe495165/hivemq-to-google-cloud-bidirectional-data-movement-gcp-pub-sub-ext-architecture.webp)

Follow these steps to achieve and verify this message flow:

1. Open a bash console such as `Terminal`, connect an MQTT client to HiveMQ, and subscribe to the topic `from/pubsub`.

2) Open another bash console and send an MQTT PUBLISH message with Quality of Service level 1, a retained flag, and a message expiry interval of 1000 to the ‘to/pubsub” topic:

3. Send another MQTT PUBLISH message with Quality of Service level 0, a retained flag, and a message expiry interval of 1000 to the ‘to/pubsub” topic:

4) Check the output of the subscribed MQTT client. This client should have received two publish messages, one with QoS `0` and one with QoS `1`; both the retained message flag set to `false` and a message expiry setting of `1000`. Retain is `false` because if you don’t set the `retain-as-published` option in the subscribe, the message loses that information in the standard publish flow.

![Output of the client with two messages](https://a.storyblok.com/f/243938/954x103/06f5a747ad/hivemq-to-google-cloud-bidirectional-data-movement-hello-blog-post.webp)

5. Disconnect your subscriber and reconnect and subscribe again. This should print the following output with retain set to `true`😀

![Printed output when disconnecting your subscriber and reconnecting and subscribing again](https://a.storyblok.com/f/243938/954x51/30e9e1227e/hivemq-to-google-cloud-bidirectional-data-movement-retained-message.webp)

6. If you subscribe to “to/pubsub,” you should get the same output.

7. You can also check your retained messages in the [HiveMQ Control Center](https://docs.hivemq.com/hivemq/latest/control-center/information.html#retained). There should be one for the topic “to/pubsub” and the same for the topic “from/pubsub”.

![Checking your retained messages in the HiveMQ Control Center](https://a.storyblok.com/f/243938/1267x443/4b483055a8/hivemq-to-google-cloud-bidirectional-data-movement-control-center-retained-messages.webp)

## Useful Links

- [HiveMQ Google Cloud Pub/Sub Extension documentation](https://docs.hivemq.com/google-cloud-pubsub/latest/google-cloud-pubsub-extension/google-cloud-pubsub-extension.html)
- [HiveMQ Google Cloud Pub/Sub Extension SDK documentation](https://docs.hivemq.com/google-cloud-pubsub/latest/google-cloud-pubsub-extension/google-cloud-pubsub-customization.html)
- [How to get a service account JSON file from google cloud](https://cloud.google.com/iam/docs/creating-managing-service-account-keys#iam-service-account-keys-list-console)
- [How to create topics in Google Cloud Pub/Sub](https://cloud.google.com/pubsub/docs/admin)
- [How to create subscriptions in Google Cloud Pub/Sub](https://cloud.google.com/pubsub/docs/create-subscription)
