Practice of Synchronous Call Based on Pub/Sub Model

Synchronous Call Scenarios

The MQTT protocol is an asynchronous communication model based on pub/sub, and is not applicable for scenarios where the server sends instructions to the device and the device responds with the results.

Alibaba Cloud IoT platform has developed a request and response synchronization mechanism based on the MQTT protocol, enabling simultaneous communication without modifying the MQTT protocol. The application server initiates a revert-RPC through the POP API. The IoT device only needs to reply to the pub message in a fixed format within the timeout, and the server can synchronously obtain the response from the IoT device.

The pub/sub procedure can be visualized as follows:

Topic Format Convention

Request: /sys/${productKey}/${deviceName}/rrpc/request/${messageId}

Response: /sys/${productKey}/${deviceName}/rrpc/response/${messageId}

$ is a variable that varies from devices

The messageId is the message ID generated by the IoT platform, and the messageId in the responseTopic of the device is consistent with the requestTopic.

Example

The device needs to subscribe:

The running device receives the topic:

After receiving the message, reply to the topic within the timeout:

Example of Synchronous Revert-RPC

Code on the Device

Revert-RPC through POP on the Server

Revert-RPC response:

“Thing Model” — An Example of Synchronous Service Invocation Using InvokeThingService

Note: Thing model InvokeThingService is not a revert-RPC

Thing Model — Synchronous Service Definition

Implementation on the Device

Note: The payload of the device-response must satisfy the output parameter structure defined in the thing model.

Server Side POP API InvokeThingService

Result of the call:

To learn more about Alibaba Cloud IoT Platform, visit www.alibabacloud.com/product/iot

Reference:https://www.alibabacloud.com/blog/practice-of-synchronous-call-based-on-pub%2Fsub-model_594095?spm=a2c41.12185799.0.0

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.