All Products
Search
Document Center

IoT Platform:Subscribe to device messages from IoT Platform

Last Updated:Sep 09, 2024

After a device is connected to IoT Platform, the device submits data to IoT Platform. Then, the data can be forwarded to your server by using Advanced Message Queuing Protocol (AMQP). This topic describes how to configure an AMQP server-side subscription. This way, your server can receive data from a street lamp by using an AMQP client.

Background information

The following figure shows how to configure an AMQP server-side subscription to obtain device messages. For more information, see Configure an AMQP server-side subscription and Connect an AMQP client to IoT Platform. AMQP

Procedure

  1. Log on to the IoT Platform console.

  2. On the Overview tab, click Public Instance.

    Important

    If IoT Platform is activated in the China (Shanghai), Singapore, Japan (Tokyo), or US (Virginia) region, perform this step. If IoT Platform is activated in other regions, skip this step.

  3. Configure a consumer group to consume messages. Your server can listen to the consumer group to obtain device messages.

    1. In the left-side navigation pane, choose Message Forwarding > Server-side Subscription. On the Server-side Subscription page, click Consumer Groups.

    2. On the Consumer Groups tab, click Create Consumer Group.

    3. In the Create Consumer Group dialog box, set the Consumer Group Name parameter to StreetLampConsumerGroup and click OK.

  4. Configure a server-side subscription for the product to which the street lamp device belongs. This way, your server can subscribe to various types of messages of the product.

    1. In the left-side navigation pane, choose Message Forwarding > Server-side Subscription.

    2. On the Subscriptions tab of the Server-side Subscription page, click Create Subscription.

    3. In the Create Subscription dialog box, configure the parameters and click OK. The following table describes the parameters.

      Parameter

      Description

      Product

      Select StreetLamp.

      Subscription Type

      Select AMQP.

      Consumer Group

      Select the StreetLampConsumerGroup consumer group that is created in the previous step.

      Message Type

      Select Device Upstream Notification.

  5. Connect the AMQP client to IoT Platform.

    In this example, Java is used for development. We recommend that you use the Apache Qpid JMS client. To download the client and view the instructions, see Qpid JMS 0.57.0.

    In this example, the development environment consists of the following components:

    To connect the AMQP client to IoT Platform, perform the following steps:

    1. Download the demo package and decompress it.

    2. Open IntelliJ IDEA and import the sample project amqp-demo in the demo package.

    3. In the AmqpClient.java file that is stored in the src/main/java/com.aliyun.iotx.demo directory, configure the required parameters to connect the JMS client to IoT Platform. The following table describes the parameters.

      Parameter

      Example

      Description

      accessKey

      LTAI4GFGQvKuqHJhFa******

      Log on to the IoT Platform console, move the pointer over the profile picture, and then click AccessKey Management to obtain the AccessKey ID and AccessKey secret.

      Note

      If you use a RAM user, you must attach the AliyunIOTFullAccess permission policy to the user. The policy allows the user to manage IoT Platform resources. If you do not attach the policy to the user, the connection to IoT Platform fails. For more information about how to grant permissions on a RAM user, see RAM user access.

      accessSecret

      iMS8ZhCDdfJbCMeA005sieKe******

      consumerGroupId

      VWhGZ2QnP7kxWpeSSjt******

      The ID of the consumer group that is created in Step 3. To obtain the ID, perform the following steps: Go to the Public Instance page of the IoT Platform console. Choose Message Forwarding > Server-side Subscription. On the Server-side Subscription page, click Consumer Groups. The Consumer Groups tab displays the ID.

      iotInstanceId

      • Public instance of the new version: iot-w3***

      • Public instance of the old version: ""

      The ID of the instance.

      • If you use a public instance of the new version, you can obtain the instance ID on the Overview page. Find the public instance and view the ID.

      • If you use a public instance of the old version, leave this parameter empty.

      For more information, see Overview.

      clientId

      12345

      The ID of the client. You must specify a custom ID. The ID must be 1 to 64 characters in length. We recommend that you use a unique identifier as the client ID, such as the UUID, MAC address, or IP address of the server on which the client runs.

      After the AMQP client is connected to IoT Platform and started, perform the following steps to view the details of the client: Log on to the IoT Platform console and click the card of the instance that you want to manage. In the left-side navigation pane, choose Message Forwarding > Server-side Subscription. On the Consumer Groups tab, find the consumer group that you want to manage and click View in the Actions column. The ID of each client is displayed on the Consumer Group Status tab. You can use client IDs to identify clients with ease.

      connectionCount

      4

      The number of connections that are enabled on the AMQP client. Maximum value: 128. This parameter is used for scale-out in real-time message pushing scenarios.

      On the Consumer Group Details page, each connected client is displayed as ${clientId}+"-"+ Number. The minimum number is 0.

      host

      • Public instance of the new version: iot-***.amqp.iothub.aliyuncs.com

      • Public instance of the old version: 198426864******.iot-amqp.cn-shanghai.aliyuncs.com

      The AMQP endpoint.

      • If you use a Java, .NET, Python 2.7, Node.js, or Go client, the port number is 5671.

      • If you use a Python 3 or PHP client, the port number is 61614.

      For more information, see View the endpoint of an instance.

    4. After you run the sample code, the following log data is returned. The data indicates that the AMQP client is connected to IoT Platform and can receive messages.

      Note

      In this example, the Thread.sleep(60 * 1000); code snippet is added to terminate the program after the program starts and runs for 1 minute. You can specify the running time based on your business requirements.

      Result