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:

Image for post
Image for post

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:

/sys/${productKey}/${deviceName}/rrpc/request/+

The running device receives the topic:

/sys/PK100101/DN213452/rrpc/request/443859344534

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

/sys/PK100101/DN213452/rrpc/response/443859344534

Example of Synchronous Revert-RPC

Code on the Device

const mqtt = require('aliyun-iot-mqtt');
//Device property
const options = require("./iot-device-config.json");
//Establish the connection
const client = mqtt.getAliyunIotMqttClient(options);

Revert-RPC through POP on the Server

const co = require('co');
const RPCClient = require('@alicloud/pop-core').RPCClient;

Revert-RPC response:

{
"MessageId": "1037292594536681472",
"RequestId": "D2150496-2A61-4499-8B2A-4B3EC4B2A432",
"PayloadBase64Byte": "eyJjb2RlIjoyMDAsIm1zZyI6ImhhbmRsZSBvayJ9",
"Success": true,
"RrpcCode": "SUCCESS"
}

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

Note: Thing model InvokeThingService is not a revert-RPC

Thing Model — Synchronous Service Definition

Image for post
Image for post

Implementation on the Device

const mqtt = require('aliyun-iot-mqtt');
//Device property
const options = require("./iot-device-config.json");
//Establish the connection
const client = mqtt.getAliyunIotMqttClient(options);

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

Server Side POP API InvokeThingService

const co = require('co');
const RPCClient = require('@alicloud/pop-core').RPCClient;

Result of the call:

{
"Data":{
"Result": "{\"currentMode\":12}",
"MessageId": "1536145625658"
},
"RequestId": "29FD78CE-D1FF-48F7-B0A7-BD52C142DD7F",
"Success": true
}

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

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store