Call a custom topic (Recommended)

更新时间:
复制 MD 格式

Reverse Remote Procedure Call (RRPC) lets you use custom topics to communicate with IoT Platform. The RRPC topics incorporate your custom topics to support communication in various scenarios. This topic describes RRPC custom topics and connection methods.

Prerequisites

To develop a device that communicates with IoT Platform using custom topics, you can use the following Link SDK:

Custom topics

TopicFormatDescription
RRPC subscription/ext/rrpc/+/${topic}Subscribe to the custom topic in the RRPC request message that IoT Platform sends.
RRPC request message/ext/rrpc/${messageId}/${topic}The RRPC request message that IoT Platform sends.
RRPC response message/ext/rrpc/${messageId}/${topic}The RRPC response message that the device sends.

The preceding Topic format is as follows:

  • ${topic}: The complete custom topic in IoT Platform. The format is /${productKey}/${deviceName}/user/${TopicShortName}.

    ${TopicShortName} is the suffix of the custom topic and specifies the topic category. For more information, see Use custom topics for communication.

  • ${messageId}: Replace this variable with the ID of the RRPC message. When a server calls the RRpc operation to send a message to a device, IoT Platform generates a unique ID for the message.

For example, if the custom topic for the device device1 is a18wP***/device1/user/get, the topic for sending an RRPC message is /ext/rrpc/121307410***/a18wP***/device1/user/get.

Connect with RRPC

  1. Send an RRPC message from IoT Platform.

    The server-side calls the RRpc API of IoT Platform to send a message to the device. For more information, see RRpc.

    The following example shows how to make the call using the Java SDK for the IoT Platform cloud.

    If you use the custom topic format, make sure that the version of the IoT Platform Java SDK for cloud (aliyun-java-sdk-iot) is 6.0.0 or later.

    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-iot</artifactId>
        <version>6.0.0</version>
    </dependency>

    Example of calling the RRpc API:

    RRpcRequest request = new RRpcRequest();
    request.setProductKey("testProductKey");
    request.setDeviceName("testDeviceName");
    request.setRequestBase64Byte(Base64.getEncoder().encodeToString("hello world"));
    request.setTopic("/testProductKey/testDeviceName/user/get");// If you use a custom topic, pass the custom topic here.
    request.setTimeout(3000);
    RRpcResponse response = client.getAcsResponse(request);
    Note You can log on to the OpenAPI Developer Portal to call the RRpc API online and view call examples for the IoT Platform cloud SDKs in different languages.
  2. Configure the device.
    • For devices that use the C Link SDK, Android Link SDK, or Python Link SDK, no special operations are required.
    • For devices that use the Node.js Link SDK, you must download the Link SDK to your local computer before you start development. Modify the model.js file in the src folder. Add ext=1 to the parameter in the genConnectParams function. Then, use this Link SDK to develop the device.

      The original clientId is:

      clientId:`${this.clientId}|securemode=${this.securemode },signmethod=hmac${this.signAlgorithm},timestamp=${this.timestamp},${extra}`,

      After you add ext=1, the clientId is:

      clientId:`${this.clientId}|securemode=${this.securemode },signmethod=hmac${this.signAlgorithm},timestamp=${this.timestamp},${extra},ext=1`,
  3. The device sends an RRPC response.

    The request topic and the response topic for an RRPC call are the same. The device sends the response to the topic from which it received the request.

    Note Currently, devices can send RRPC response messages only with a Quality of Service (QoS) level of 0.

    For more information about how to use the Python Link SDK to respond to an RRPC request from a device, see RRPC capabilities.