An online or offline event of the MQTT client triggers the generation of a notification message by the Message Queue for MQTT broker. You can export the data of this message from Message Queue for MQTT to other Alibaba Cloud services. You can also use Message Queue for MQTT SDK for Java to implement message sending and receiving between the MQTT client and the backend application. This topic uses the data exchanges of Message Queue for Apache RocketMQ, the only service supported, as an example.

Prerequisites

Background information

You can configure client status notification rules to export the data of online and offline event notifications of the MQTT client to other Alibaba Cloud services.

This topic uses SDK for Java in an Internet environment as an example to describe how Message Queue for MQTTsends online and offline event notification messages of the MQTT client to a backend application.

You can use open-source third-party SDKs that support multiple languages to send and receive messages in this scenario. For more information, see Download the SDK.

quick_start_client_stats_notify

As shown in the preceding figure, both your backend application and the MQTT client are developed in Java. When the MQTT client that you have deployed in an Internet environment goes online or offline, this event triggers the MQTT service to generate an event message. You can configure notification rules of the client to send the notification message to the backend application by using Message Queue for Apache RocketMQ. The brokers of the two services send messages to and receive messages from their respective clients by using the SDKs for Java provided by the services.

For more information about the client status notification feature of the client, see Obtain the status of an MQTT client.

Note Messages cannot be sent between a Message Queue for Apache RocketMQ topic in one region and a Message Queue for MQTT topic in another region. Therefore, all resources in this topic must be created in the Internet region. For more information, see Region-specific topics.

Network access

Message Queue for MQTT provides Internet endpoints, classic network endpoints and virtual private cloud (VPC) endpoints. We recommend different types of endpoints based on scenarios:
  • In Internet of Things (IoT) and mobile Internet scenarios, we recommend that you use Internet endpoints to connect your MQTT client to Message Queue for MQTT.
  • Classic network endpoints and VPC endpoints are used only in special scenarios. If your applications are deployed on cloud servers, we recommend that you use a server-side messaging service, such as Message Queue for Apache RocketMQ.
Notice To connect an MQTT client to Message Queue for MQTT with an endpoint, use the domain name rather than the IP address because the IP address can change at any time. The Message Queue for MQTT product team is not liable for any direct or indirect faults or losses arising in the following scenarios:
  • Your MQTT client uses the IP address instead of the domain name for access. The Message Queue for MQTT product team updates the domain name resolution, and the original IP address becomes invalid.
  • A firewall policy on IP addresses has been set in the network where your MQTT client is run. The Message Queue for MQTT product team updates the domain name resolution, and the new IP address is blocked due to the firewall policy.
This topic uses the Internet endpoint of Message Queue for MQTT as an example. For more information about scenarios caparison and message attribute mappings between Message Queue for MQTT and Message Queue for Apache RocketMQ, see the following two topics:

Procedure

The following figure shows the procedure of sending and subscribing to client status notification messages by the MQTT client.

quick_start_client_status_notify_process

Step 1: Create a Message Queue for MQTT instance and obtain an endpoint.

  1. Log on to the Message Queue for MQTT console. In the top navigation bar, select the region where the instance is located.
  2. In the Instances section on the Overview page, click Create Now.
  3. On the buy page, select the desired instance type and click Buy Now.
    You can purchase subscription or pay-as-you-go instances. For more information, see Billing methods. This topic uses a pay-as-you-go instance as an example.
  4. On the Confirm Order page, select the corresponding service agreement, and click Pay.
    This topic uses a pay-as-you-go instance as an example.
  5. Return to the Message Queue for MQTTconsole. In the left-side navigation pane, choose Instances > Instance Information.
  6. In the Endpoint Information section on the Instance Information tab, you can view the endpoint information. This topic uses the Internet endpoint as an example.

Step 2: Create a parent topic.

The MQTT protocol supports multi-level topics. You must create a parent topic in the Message Queue for MQTT console or by calling the OpenAPI. You do not need to create a subtopic. For more information about topics, see Terms. In this tutorial, a parent topic is created in the Message Queue for MQTT console.

  1. In the left-side navigation pane, choose Topics > Create Topic.
  2. In the Create Topic dialog box, set Topic and Description and click OK.
    You can view the topic you create on the Topics page.

Step 3: Create a group ID.

For more information about group IDs, see Terms.

  1. In the left-side navigation pane, choose Groups > Create Group ID.
  2. In the Create Group ID dialog box, set Group ID and click OK.
    You can view the group ID you create in the group ID list.

Step 4: Create status notification rules for the client.

The parameters in the rule must be the same as those of the resources you have created.

  1. In the left-side navigation pane, choose Rules > Create Rule.
  2. On the Create Rule panel, click the Online/Offline Notification tab.
  3. On the Online/Offline Notification tab, set the following parameters as prompted, and click OK.
    Parameter Example value Description
    Rule ID 111111 The global unique identifier of a rule.
    • It can contain only letters, digits, hyphens (-), and underscores (_) and must contain at least one letter or digit.
    • It must be 3 to 64 characters in length, and excessive characters will be automatically truncated.
    • It cannot be updated after the rule is created.
    Rule Description migrate from rocketmq The description of the rule.
    Status Enable Specifies whether to enable the current rule. Valid values:
    • Enable
    • Disable
    Instance Group
    Group ID GID_Client_Status Specifies the device group from which data is exported. For more information about group IDs, see Terms.
    Notification
    Note Only Message Queue for Apache RocketMQ is supported.
    Cloud Service Message Queue for Apache RocketMQ Specifies the target cloud service to which the client status notifications are forwarded. The value is Message Queue for Apache RocketMQ.
    Instance ID MQ_INST_13801563067*****_BbyOD2jQ The ID of the instance from the target cloud service, that is, the ID of the Message Queue for Apache RocketMQ instance.
    Topic Topic_test The resource key of the target cloud service, that is, the topic of Message Queue for Apache RocketMQ. If the value is set to Topic_set, the status notifications of Message Queue for MQTT clients are forwarded to to the Topic_test topic.
    You can view the client status notification rule you create in the rule list of Rules.

Step 5: Send and receive messages by using SDK for Java.

  1. Download a third-party open-source SDK for Java at Eclipse Paho Java Client.
  2. Download the demo of Alibaba Cloud Message Queue for MQTT SDK for Java at mqtt-java-demo for reference during code development.
  3. Decompress this demo project package to a specific folder.
  4. In IntelliJ IDEA, import the decompressed files to create a project, and check that the following dependencies are included in the pom.xml file.
    <dependencies>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.10</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.paho</groupId>
                <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
                <version>1.2.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.2</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.48</version>
            </dependency>
            <dependency>
                <groupId>com.aliyun.openservices</groupId>
                <artifactId>ons-client</artifactId>
                <version>1.8.5.Final</version>
            </dependency>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-onsmqtt</artifactId>
                <version>1.0.3</version>
            </dependency>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <version>4.5.0</version>
            </dependency>
        </dependencies>
    Note For more information about the ons-client version, see Release notes.
  5. In the MQTTClientStatusNoticeProcessDemo.java class, configure parameters as instructed in code annotations. Most of these parameters configure the Message Queue for MQTT resources created in Steps 1 to 3 and the resources that you created in Message Queue for Apache RocketMQ. For more information about the sample code, see MQTTClientStatusNoticeProcessDemo.java.

Verify the result

After the provider sends the message and the consumer subscribes to the message, you can query the trace of the message in the Message Queue for MQTT console to verify whether the message has been sent and received. For more information, see Query a message trace.

References