Forward data to other topics
A device cannot subscribe to or publish messages to the topics of another device. Data forwarding enables device-to-device (M2M) communication by routing processed messages from one device topic to another through a parser script. This topic uses a Thing Specification Language (TSL) model data reporting topic as an example.
How it works
Data forwarding routes data from Topic 1 to Topic 2. The following diagram shows the data flow.
Data forwarding routes messages from Topic 1 of Device 1 to Topic 2 of Device 2. If Device 2 has subscribed to Topic 2 with the required permissions, it receives messages from Topic 1 of Device 1, enabling communication between different devices.
In the preceding diagram:
-
Data source: The message originates from Topic 1. For information about the supported topic categories, see Data formats (for products and devices that are not cloud gateways), Custom topics (for MQTT cloud gateways), Message forwarding topics (for GB/T 32960 cloud gateways), Message forwarding topics (for JT/T 808 cloud gateways), and Message forwarding topics (for SL 651 cloud gateways).
-
Data destination: The destination topic, Topic2, receives messages from Topic1 and supports the following topic types.
-
For products and devices that are not cloud gateways: Custom topics and the topic for sending TSL model data are supported:
/sys/${productKey}/${deviceName}/thing/service/property/set. -
For cloud gateway products and devices: Custom topics are supported.
-
-
Parser script: A script that is configured to use the
writeIotTopic(destinationId, topic, payload)function to forward data from Topic 1 to Topic 2.For more information about the function, see Functions for forwarding data to data destinations.
Scenarios
-
The destination topic for data forwarding is a custom topic:
-
Communication between different devices that belong to the same product.
-
Communication between two devices that belong to different products.
-
-
The destination topic for data forwarding is the topic for sending TSL model data:
/sys/${productKey}/${deviceName}/thing/service/property/set.A device receives forwarded data from this topic and sets its property values accordingly. This scenario applies when the destination device needs to update its properties based on the forwarded data.
Limits
-
The destination device must have permission to subscribe to the destination topic and must have subscribed to it. Otherwise, it cannot receive the forwarded message.
-
Cloud gateway products and devices do not support cross-product data forwarding. Data can be forwarded only to a communication topic that belongs to the current product.
-
MQTT-based instances support only one product. Therefore, cross-product data forwarding is not supported by default.
Prerequisites
You must add a data source for the device topic. For example, you can create a data source named DataSource and add the TSL model data reporting topic of a specific device. For more information, see Add a data source.
Step 1: Create a data destination
-
Log on to the IoT Platform console.
On the Overview page, find the instance that you want to manage and click the instance ID or instance name.
-
In the left-side navigation pane, choose .
-
On the Data Forwarding page, click Go to New Version in the upper-right corner.
NoteIf you have performed this operation before, you are automatically redirected to the new version of the Data Forwarding page.
-
Click the Data destination tab, and then click Create data destination.
-
In the Create Data Destination dialog box, enter a data destination name, such as DataPurpose, configure the parameters according to the descriptions below, and click OK.
The following figure shows a sample configuration.

Parameter
Description
Select Operation
Select Publish To Another Topic.
Product
Select the product to which the destination topic belongs.
The specific topic must be set in the parser script using the
writeIotTopic(destinationId, topic, payload)function. For more information about the function, see Data forwarding functions.
Step 2: Configure and start the parser
-
Create a parser, such as DataParser. For more information, see Step 1: Create a parser.
-
On the Resolver Details page, associate the data source.
-
In the configuration wizard, under Data Source, click Associate Data Source.
-
In the dialog box, select DataSource from the Data Source drop-down list and click OK.
-
-
On the Resolver Details page, you can associate data destinations.
-
In the configuration wizard, click Data Destination. Then, in the upper-right corner of the Data Destination list, click Associate Data Destination.
-
In the dialog box that appears, from the Data Destination drop-down list, select DataPurpose and click OK.
-
In the data destination list, find the Data Destination ID and record it. For example, 1000.
You will need this Data Destination ID in the parsing script.
-
-
On the parser details page, click the Parser Script tab.
-
In the script editor, enter a parser script.
The parser script syntax is similar to JavaScript. For more information about how to write a script, see Script syntax.
To forward data to another topic, use the
writeIotTopic(destinationId, topic, payload)function. For more information about the function parameters, see Function list.// Use the payload() function to obtain the message that is reported by the device and convert the message to the JSON format. var data = payload("json"); // Forward the TSL model data. writeIotTopic(1000, "/sys/a1I***/room3/thing/service/property/set", data); -
Click Debug, select a product and device, enter the Topic and Payload data, and verify that the script is executable.
The following figure shows sample parameters:

The script executed successfully, as shown in the following output.

-
Click Publish.
-
Return to the Resolver tab on the Data Forwarding page and click Start for the DataParser resolver.
Operation example
M2M communication between devices based on message forwarding