Skip to content
On this page

Ingest MQTT Data into GCP Pub/Sub

EMQX supports seamless integration with Google Cloud Pub/Sub for real-time extraction, processing, and analysis of MQTT data. Additionally, it enables data push and subscription to various Google Cloud services such as Cloud Functions, App Engine, Cloud Run, Kubernetes Engine, and Compute Engine.

EMQX GCP Pub/Sub integration allows you to send MQTT messages and events to GCP Pub/Sub, which can help you flexibly choose services on Google Cloud and build IoT applications more easily.

Prerequisites

Feature List

Quick Start Tutorial

This section introduces how to configure the GCP Pub/Sub data bridge, including how to set up the GCP service, create data bridges and rules for forwarding data to GCP and test the data bridges and rules.

Create Service Account Key in GCP

You need to create a service account and a service account key to use the GCP PubSub service.

  1. Create a Service Account in your GCP account. Ensure that the Service Account has permission to at least publish messages to the topic of interest.

  2. Click the email address for the service account you created. Click the Key tab. In the Add key drop-down list, select Create new key to create a Service Account key for that account and download it in JSON format.

    TIP

    Store the Service Account key securely for later use.

    service-account-key

Create and Manage Topics in GCP

Before configuring the GCP Pub/Sub Bridge on EMQX, you need to create a topic and be familiar with the basic management operation in GCP.

  1. In the Google Cloud console, go to the Pub/Sub ->Topics page. For detailed instructions, see Create and manage topics.

    TIP

    The Service Account must have permission to publish that topic.

  2. In the Topic ID field, enter an ID for your topic. Click Create topic.

    create-topic-GCP-console
  3. Go to the Subscriptions page. Click the Topic ID in the list. Create a subscription to the topic.

    • Select Pull in Delivery type.
    • Select 7 Days for Message retention duration.

    For detailed instructions, see GCP Pub/Sub Subscription.

    add-subscription-to-topic
  4. Click Subscription ID -> Messages -> Pull can view the message sent to the topic.

    subscriptions-idsubscriptions-id-pull

Create a GCP Pub/Sub Bridge

  1. Go to EMQX Dashboard, click Integration -> Data Bridge.

  2. Click Create on the top right corner of the page.

  3. In the Create Data Bridge page, click to select Google PubSub, and then click Next.

  4. In the Name field, enter a name for the data bridge. The name should be a combination of upper/lower case letters and numbers.

  5. In the Bridge Role field, select Producer or Consumer from the drop-down list according to your business needs and complete the corresponding configurations.

  6. Before clicking Create, you can click Test Connectivity to test that the bridge can connect to the Redis server.

  7. Click Create to finish the creation of the data bridge.

    A confirmation dialog will appear and ask if you like to create a rule using this data bridge, you can click Create Rule to continue creating rules to specify the data to be saved into GCP PubSub. You can also create rules by following the steps in Create Rules for GCP PubSub Data Bridge.

Create a Rule for GCP PubSub Producer Data Bridge

You can continue to create rules to specify the data to be saved into GCP PubSub.

  1. Go to EMQX Dashboard, click Integration -> Rules.

  2. Click Create on the top right corner of the page.

  3. Input my_rule as the rule ID.

  4. Set the rules in the SQL Editor. Here we want to save the MQTT messages under topic /devices/+/events to GCP PubSub, we can use the SQL syntax below.

    Note: If you want to specify your own SQL syntax, make sure that the SELECT part includes all fields required by the payload template in the data bridge.

    sql
    SELECT
      *
    FROM
      "/devices/+/events"
  5. Click the Add Action button, select Forwarding with Data Bridge from the dropdown list, and then select the data bridge you just created under Data Bridge. Then click the Add button.

  6. Click Create at the page bottom to finish the creation.

Now a rule to forward data to GCP PubSub via a GCP PubSub bridge is created. You can click Integration -> Flows to view the topology. It can be seen that the messages under topic /devices/+/events are sent and saved to GCP PubSub after parsing by rule my_rule.

Test the Data Bridge and Rule

  1. Use MQTTX to send messages on the topic /devices/+/events.

    bash
    mqttx pub -i emqx_c -t /devices/+/events -m '{ "msg": "hello GCP PubSub" }'
  2. Check the running status of the data bridge, there should be one new incoming and one new outgoing message.

  3. Go to GCP Pub/Sub -> Subscriptions, click MESSAGES tab. You should see the message.