RRPC capabilities

Updated at:

Reverse RPC (RRPC) is a feature that allows a server to send a request to a device using an IoT Platform cloud API and receive a response from the device.

Function introduction

The Message Queuing Telemetry Transport (MQTT) protocol uses an asynchronous publish/subscribe (PUB/SUB) pattern. This pattern is not suitable for scenarios that require a server to synchronously control a device and receive a response. To support these scenarios, IoT Platform provides a synchronous request and response mechanism that is built on the MQTT protocol. This mechanism enables synchronous communication without modifying the MQTT protocol. IoT Platform provides an API for the server to send requests. The device replies by publishing a message in a specific format. The server uses the same API to synchronously retrieve the response from the device.

When a server initiates an RRPC call using a cloud API, the device receives a request on a topic with the format /ext/rrpc/${messageId}/${rrpc_topic}. The device processes the message and pushes the response to /ext/rrpc/${messageId}/${rrpc_topic}. The Python Link SDK encapsulates these implementation details.

For more information about RRPC, see What is RRPC.

The cloud uses RRPC calls in two scenarios:

  • Message communication: RRpc. This API sends an RRPC request. To use this API, you must implement the RRPC call on the device. When this API is used, the `Topic` request parameter cannot be empty and must be set to a custom topic.

  • Device management: InvokeThingsService. If you use the Premium Edition and register a service as synchronous, the RRPC pattern is used when that service is invoked.

Use the Link SDK

To use the RRPC feature, use Aliyun IoT Python SDK 1.1.0 or a later version.

  • Use RRPC with a standard RRPC topic

    1. Set the on_topic_rrpc_message callback to process RRPC topic requests.

      linkkit.on_topic_rrpc_message = on_topic_rrpc_message
      ...
      def on_topic_rrpc_message(self, id, topic, payload, qos, userdata):
          print("on_topic_rrpc_message: id:%s, topic:%s, payload:%s" % (id, topic, payload))
          self.linkkit.thing_answer_rrpc(id, payload)     
    2. After the device connects to the cloud, call subscribe_rrpc_topic to subscribe to the RRPC topic.

      def on_connect(self, session_flag, rc, userdata):
              print("on_connect:%d,rc:%d,userdata:" % (session_flag, rc))
              self.linkkit.subscribe_rrpc_topic("/${YourProductKey}/${YourDeviceName}/user/get")

      The topic /${YourProductKey}/${YourDeviceName}/user/get is an example. Replace it with your custom topic.

    After the RRPC request is processed, respond using thing_answer_rrpc. `id` is the ID of the RRPC request and `payload` is the payload of the response message.

    The IoT Platform cloud SDK uses the RRpc interface to call this type of RRPC and retrieve a synchronous response.

  • Use RRPC with a TSL model service

    Set the on_thing_call_service callback to process synchronous service requests.

    linkkit.on_thing_call_service = on_thing_call_service
    ...
    def on_thing_call_service(self, identifier, request_id, params, userdata):
        print("on_thing_call_service: identifier:%s, request_id:%s, params:%s" % (identifier, request_id, params))
        ...
        self.linkkit.thing_answer_service(identifier, request_id, 200, {}) 

    After the service request is processed, respond using thing_answer_service. The `request_id` in the response must be the `request_id` from the request.

    The cloud SDK uses the InvokeThingsService interface to call this type of service and retrieve a synchronous response.