All Products
Search
Document Center

IoT Platform:M2M communication using Message Forwarding

Last Updated:Jun 20, 2026

This topic explains how to use the Data Forwarding feature of IoT Platform to enable M2M communication between a mobile app and a smart lamp.

Background

The following figure shows the process of controlling a smart lamp with a mobile app.

M2M设备间通信

Create products and devices

  1. In the IoT Platform console, create a product and a device for the mobile app in your instance. For more information, see Create a product and Create a device.

    In this example, set the following parameters to create the product: set Product Name to MobileApp, node type to directly connected device. Use the default values for the other parameters.

    Set the DeviceName for the device to ControlApp.

    In this example, the ProductKey of the MobileApp product is al987654321.

    After you create the product and device for the mobile app, you can connect the app to IoT Platform.

    When a mobile app user registers and logs in, your server sends device information to the mobile app, allowing the app to connect to the IoT Platform as a device.

  2. Add a custom topic with publish permissions for sending control commands.

    In this example, add the following topic: /al987654321/ControlApp/user/command. For more information, see Use custom topics for communication.

  3. Develop the mobile app.

    In this example, the mobile app uses HTTPS to communicate with IoT Platform.

    The following code shows the format of the payload data for a control command that the mobile app sends to the smart lamp over the custom topic:

    {
        "TargetDevice": "light", 
        "Switch": 0, 
        "Timestamp": 1557750407000
    }

    For details about device-side SDK development, see Connect devices by using Link SDK.

  4. In the IoT Platform console, in your instance, create a product and a device for the smart lamp and define its features. For more information, see Create a product, Create multiple devices at a time, and Add a Thing Specification Language (TSL) feature.

    In this example, set the following parameters to create the product: set Product Name to SmartLamp, node type to directly connected device. Use the default values for the other parameters.

    For the device that you create, set DeviceName to light.

    In this example, the ProductKey of the SmartLamp product is al123456789.

    You can add Thing Specification Language (TSL) features based on your business requirements. For example, add a TSL property named Switch. Set the data type to Boolean (0 for off and 1 for on) to control the smart lamp's switch.

  5. Add a custom topic with subscribe permissions to receive control commands from the mobile app.

    In this example, add the following topic: /al123456789/light/user/set. For more information, see Use custom topics for communication.

  6. Develop the smart lamp device to connect to IoT Platform, and implement the logic to receive and execute commands.

    In this example, the device uses MQTT to communicate with IoT Platform. The smart lamp device must subscribe to the topic /al123456789/light/user/set to receive commands. After the device receives a command, you must develop the logic to execute it.

    For details about device-side SDK development, see Connect devices by using Link SDK.

Configure Message Forwarding parser

Configure a Message Forwarding parser to forward commands published by the mobile app to the smart lamp's topic.

  1. Log on to the IoT Platform console.

  2. On the Overview page, click All environment. On the All environment tab, 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.

Add a data source

For more information about the parameters in the following steps, see Add a data source to be forwarded.

  1. Click the Data Source tab and perform the following steps to add a data source.

    Important

    If the page for the previous version is displayed, click Go to New Version in the upper-right corner. On the new page, click the Data Source tab.

    1. Click Create Data Source.

    2. In the Create Data Source dialog box, enter a name for the data source, such as AppData.

    3. Click OK.

  2. On the AppData page, click Add Topic.

  3. In the Add Topic dialog box, select the topic to process and click OK.

    In this example, select the topic /al987654321/ControlApp/user/command of the mobile app device ControlApp. This allows you to retrieve the values of the TargetDevice, Timestamp, and Switch fields from the message.

Configure a data destination

For more information about the parameters in the following steps, see Add a data destination.

  1. Go back to the Data Forwarding page and click the Data Destination tab.

  2. Click Create Data Destination.

  3. In the dialog box, enter a name for the data destination, such as OtherTopic, and select the SmartLamp product.

    For Select Action, select republish to another topic.

  4. Click OK.

Configure and start the parser

For more information about the parameters in the following steps, see Configure a parser.

  1. Click the Parser tab and perform the following steps to add a parser.

    1. Click Create Parser.

    2. In the dialog box, enter a name for the parser, such as DataParser.

    3. Click OK.

  2. On the DataParser page, associate a data source.

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

    2. In the dialog box, select AppData from the Data Source drop-down list.

    3. Click OK.

  3. On the DataParser page, click Data Destination in the configuration wizard to associate a data destination.

    1. In the upper-right corner of the Data Destination list, click Associate Data Destination.

    2. In the dialog box that appears, select OtherTopic from the Data Destination drop-down list.

    3. Click OK.

    4. In the data destination list, view and save the data destination ID. In this example, the ID is 1000.

      You must use this data destination ID in the parser script.

  4. On the DataParser page, click Parser Script in the configuration wizard to configure the script.

    1. In the script editor, enter a parser script. Set the destination topic to /al123456789/light/user/set, which the smart lamp device uses to receive commands.

      For more information about script syntax, see Script syntax. For more information about data forwarding functions, see writeIotTopic() function.

      // Use the payload() function to get the message content reported by the device and parse it as JSON.
      var data = payload("json");
      // Get the device name. In this example, the value of TargetDevice is the smart lamp device, light.
      var dn = data.TargetDevice;
      // Define the destination topic that the smart lamp subscribes to.
      var topic_set = "/al123456789/"+dn+"/user/set";
      // Call writeIotTopic() to forward the original payload to the destination topic (ID 1000).
      writeIotTopic(1000, topic_set, data)
    2. Below the script editor, click Save.

    3. Click Debugging. In the panel that appears on the right, select a product and a device, and enter a topic and payload to test the script.

      For Product, select MobileApp. For Device, select ControlApp. For Topic, enter /productKey/ControlApp/user/command. For Payload Data, enter {"TargetDevice": "light", "Switch": 0, "Timestamp": 1557750407000}.

      A successful test produces the following output.

      action: 
          transmit to republish[destinationId=1000], data:{"Switch":0,"Timestamp":1557750407000,"TargetDevice":"light"}
      variables: 
          data : {"Switch":0,"Timestamp":1557750407000,"TargetDevice":"light"}
          topic_set : /al123456789/light/user/set
          dn : light
    4. In the lower-left corner of the panel, click Close.

    5. Below the script editor, click Publish.

  5. Return to the Parser tab on the Data Forwarding page. In the Actions column for the DataParser parser, click Start.

  6. Click OK to start the parser.

Communicate between devices

  1. The mobile app user scans a QR code to bind the app to the smart lamp.

    When the app sends a binding request to your server, the server returns the DeviceName of the successfully bound smart lamp. In this example, the device name is light.

  2. The user sends a control command from the mobile app.

    1. The mobile app sends a command over a custom topic. In this example, the topic is /al987654321/ControlApp/user/command.

    2. IoT Platform uses the Message Forwarding parser to forward the command to the smart lamp topic. In this example, the topic is /al123456789/light/user/set.

    3. After the smart lamp receives the command, it performs the corresponding action.

    Note

    After unbinding, the app can no longer control the smart lamp.