Forward data to Simple Message Queue (MNS)

更新时间:
复制 MD 格式

When the message volume for all or specified devices under a single product is less than 1,000 QPS, you can use data forwarding to send data from IoT Platform to Simple Message Queue (formerly MNS) (SMQ). Your server subscribes to messages from the MNS topic, enabling high-performance, closed-loop transmission between devices and servers. This topic uses a Thing Specification Language (TSL) model data submission topic as an example to describe the process of forwarding message data.

How it works

image

The data forwarding feature forwards messages in real time from a specified topic for all or specific devices under the same product to an MNS topic subscribed to by an MNS queue.

In the preceding figure:

Limits

  • Data forwarding to MNS is supported only by IoT Platform instances in specific regions. For more information about the supported regions, see Feature availability by region.

  • Both the new and previous versions of the data forwarding feature support forwarding data to MNS. For an example of how to use the previous version, see Forward data to Message Service (previous version).

  • For more information about the limits on forwarding device messages from an IoT Platform instance, see Limits.

Prerequisites

  • You have added a data source for the device topic. For example, you have created a data source named DataSource and added the TSL model data submission topic for a specific device. For more information, see Add a data source.

  • You have created an MNS topic and a subscription of the HTTP or queue push type for the topic. For more information about how to use MNS, see the Simple Message Queue (MNS) documentation.

    Important

    For an Enterprise instance, the MNS topic must be in the same region as the instance.

  • Your business server uses an MNS server-side SDK to listen to the MNS queue that subscribes to the MNS topic, allowing your cloud application to receive device messages from IoT Platform.

    For more information about how to call the Simple Message Queue (formerly MNS) (SMQ) server-side SDK, see the Topic User Guide for the corresponding SDK in SDK download and usage.

Background information

For information about the process of forwarding data between a device and a server using MNS, see Forward data to Message Service (MNS).

IoT Platform provides both server-side subscription and data forwarding services to forward device messages to an MNS client. Unlike server-side subscription, data forwarding lets you specify device topics and process data before forwarding. To compare solutions and find the best fit for your needs, see Comparison of data forwarding solutions. For information about how to configure server-side subscription, see Use server-side subscription with MNS.

Create a data destination

  1. Log on to the IoT Platform console.

  2. On the Overview page, find the instance that you want to manage and click the instance ID or instance name.

  3. In the left-side navigation pane, choose Message Forwarding > Data Forwarding.

  4. On the Data Forwarding page, click Go to New Version in the upper-right corner.

    Note

    If you have performed this operation before, you are automatically redirected to the new version of the Data Forwarding page.

  5. Click the Data Destination tab. On this tab, click Create Data Destination.
  6. In the Create Data Destination dialog box, enter a name for the data destination, such as DataPurpose. Configure the parameters as described in the following table, and then click OK.

    Parameter

    Description

    Select an operation.

    Select Forward data to Message Service (MNS).

    Region

    Select the region where Message Service is activated.

    Topic

    Select the SMQ topic that you want to use to receive data.

    SMQ forwards the received messages to the HTTP and queue subscriptions under this topic. Currently, you can subscribe to messages that IoT Platform pushes to an SMQ topic only using the HTTP or queue method.

    You can click Create Topic to go to the Simple Message Queue (formerly MNS) console to create an SMQ topic and a subscription. For more information, see the Simple Message Queue (SMQ) documentation.

    Role

    Grant IoT Platform the permissions to write data to Simple Message Queue (formerly MNS).

    If no RAM role exists, click Create RAM Role to create a RAM role and authorization policy in the RAM console. Create a RAM role.

Configure and start the parser

  1. Create a parser named DataParser. For more information, see Step 1: Create a parser.

  2. On the Parser Details page, associate the parser with the created data source.

    1. In the Data Source step of the wizard, click Associate Data Source.

    2. In the dialog box that appears, select DataSource from the Data Source drop-down list, and then click OK.

  3. On the Parser Details page, associate the parser with the created data destination.

    1. Click Data Destination in the wizard. In the Data Destination section, click Associate Data Destination.

    2. In the dialog box that appears, select DataPurpose from the Data Destination drop-down list, and then click OK.

    3. In the Data Destination section, view and save the data destination ID. In this example, the ID is 1000.

      When you write the parser script, you must use the data destination ID.

  4. On the Parser Details page, click Parser.
  5. In the script editor, enter a parser script.

    The parser script syntax is similar to JavaScript. For more information about how to edit scripts, see Script syntax.

    For more information about the function parameters, see Function list.

    // Use the payload() function to get the message content reported by the device and convert it to the JSON format.
    var data = payload("json");
    // Forward the TSL model data directly.
    writeMns(1000, data);
  6. Click Test. Follow the on-screen instructions to select a product and a device, enter the topic and payload data, and verify that the script can be executed.

    The following figure shows sample parameters:调试示例

    The following output confirms a successful script execution.

    运行结果

  7. Click Publish.
  8. Go to the Parser tab of the Data Forwarding page. Find the DataParser parser and click Start in the Actions column to start the parser.

What to do next

  • You can view the operational logs for device-to-cloud messages and data forwarding on the IoT Platform Logs tab. To go to this tab, go to the IoT Platform console, find your instance, and choose O&M > Log Service. For more information, see IoT Platform logs.

  • You can log on to the Simple Message Queue (formerly MNS) console to view the messages that are pushed from the MNS topic to the queue. For more information, see Receive messages.

References

  • For instructions on how to connect a device to IoT Platform to report data, see Device connection guide.

  • Check the available message forwarding TPS resources on the instance details page to ensure device messages can be forwarded to Simple Message Queue (formerly MNS) for consumption. For more information, see View instance information and operational data. If resources are insufficient, upgrade the instance. For more information, see Upgrade an instance.

  • You can view the metrics of the Simple Message Queue (formerly MNS) topic to check the number of received messages. For more information, see View topic metrics.

  • Use the Log Management feature to push message topic operation logs to Simple Log Service. If an exception occurs during message consumption, query the message trace by MessageId to diagnose the issue. For more information, see Push logs to Simple Log Service and View logs in Simple Log Service.