Using EMQX Cloud Data Integrations to save data to Firebase Realtime Database

emqx 客服发表于:2023年03月15日 11:43:59


    In this article, we will simulate temperature and humidity data and report it to the  EMQX cloud via the MQTT protocol, after which we will use the EMQX Cloud data  integration to enable NAT gateway and save the data to the Firebase Realtime Database  over the public network.

Introduction to EMQX Cloud

    EMQX Cloud is an MQTT middleware for the IoT from EMQ. As the world's first fully  managed MQTT 5.0 public cloud service, EMQX Cloud provides a one-stop O&M  colocation and a unique isolated environment for MQTT services. In the era of Internet of  Everything, EMQX Cloud can help you quickly build industry applications and easily realize  the collection, transmission, computation and persistence of IoT data.


    With the infrastructure provided by cloud providers, EMQX Cloud is available in  dozens of countries and regions around the world, providing low-cost, secure, and  reliable cloud services for 5G and Internet of Everything applications. 

    For more information, please go to the EMQX Cloud website or see the EMQX Cloud  documentation.

Introduction to Firebase Realtime Database

    The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON  and synchronized in realtime to every connected client. When you build cross-platform  apps with our Apple platforms, Android, and JavaScript SDKs, all of your clients share one  Realtime Database instance and automatically receive updates with the newest data.


  1. create a deployment of EMQX Cloud

    Create a deployment of EMQX Cloud, other options default.


    When the status is Running, the creation of the deployment is complete.


    When the status is Running, the creation of the deployment is complete.


  2. Enable NAT Gateway

    NAT gateways can provide network address translation services to provide  Professional deployments with the ability to access public network resources without the  need for VPC peering connections.


  3. Create Firebase Realtime Database instances

    If you are creating a Firebase Realtime Database instance for the first time, you can  refer to the help document. First, go to the Firebase Realtime Database console and  create a database.


Data Integrations

  1. Create a resource

    Go to the Data Integrations page. On the data integration page, click webhook  resources.


    Fill in the Firebase Realtime Database connection address, and then click test. Please  check the Firebase Realtime Database service if the test fails. Click the New button after  the test is passed and you will see the Create Resource successfully message.


  2. Create a new rule

    After the resource is successfully created, you can return to the data integration page  and find the newly created resource, and click create rule. Our goal is that as long as the  emqx/test topic has monitoring information, the engine will be triggered. Certain SQL  processing is required here:

     Only target the topic "emqx/test"

     Get the three data we need: "msg"

    According to the above principles, the SQL we finally get should be as follows:

  3. SELECT  
    payload.msg as msg 
    FROM "emqx/test"


    You can click SQL Test under the SQL input box to fill in the data:

     topic: emqx/test

     payload:

  4. { "msg": "hello" }

    Click Test to view the obtained data results. If the settings are correct, the test output  box should get the complete JSON data as follows:


    If the test fails, please check whether the SQL is compliant and whether the topic in  the test is consistent with the SQL filled in.

  5. Add Action to Rule

    After completing the rule configuration, click Next to configure and create an action. Then enter the fields and tags as follows:

  6. #path /emqxcloud/demo.json 
    #Body ${msg}



  1. Use MQTTX to simulate data reporting

    We recommend you to use MQTT X, an elegant cross-platform MQTT 5.0 desktop  client to subscribe/publish messages. Click on the add button and fill in the deployment information to connect to the  deployment. You need to replace with the created  deployment connection address, add client authentication information to the EMQX  Cloud console. Enter the topic name and payload message to publish the message.


  2. View results in Firebase Realtime Database

    Go to the operation page. Choose the 'data' bar. Here you can see the results of the  MQTT data forwarding.


    So far, we have used EMQX Cloud data integration to save the entire process of data  to the Firebase Realtime Database over the public network.