Receiver Function
The receiver function is used to handle the messages or commands that are received from the cloud backend.
IoT communication is bidirectional in nature, it includes both device to cloud and cloud to device communication. So far we have seen how to generate messages and send them to your IoT platform. Let's discuss how to handle the messages or commands coming from the server-side. The handling of these messages are specific to the protocol in use, however, the common functions remain the same.
function (topic, response, state)
{
console.log("Received a message on subscribed topic ", topic)
state.received++;
state.recv_queue.push(response);
}
It is important to understand that while a simulation is running, the handler may be invoked asynchronously. This is because the command from the server-side could be initiated anytime. To be on the safer side and avoid any race condition, the subscription handler should be small and defer all actions to be executed in the next iteration call. In the above example, we have simply produced a log and pushed the received message to the receive queue.
Note that the subscription handler does not need to return any value.
The subscription handler could be invoked multiple times asynchronously while your simulation is running. That's why the results of the subscription should be stored in a queue rather than being processed, as a second run could overwrite the previous run results.
Since response handler signature is common across all protocols and has a common list of arguments (topic, response, state), let's have a look at how these parameters are actually mapped for each protocol
Protocol
topic
response
MQTT
MQTT Topic (string)
Payload received from server (string)
HTTP
HTTP URL (string)
Body of the response from server (string)
CoAP
CoAP topic (string)
CoAP payload as (string)
TCP/UDP
undefined
payload received from server (string)
CoAP
CoAP Topic (string)
payload received from server (string)
Response Handler for MQTT
In MQTT, a client could subscribe to any particular topic. The subscription topic can be specified in the template by enabling the subscription button in the protocol tab. When a publication is received on the chosen topic, the subscription handler is invoked with
function(topic, response, state)
{
//topic - The MQTT topic on which subscription has been received
//response - A buffer object which is sent for the subscription
//state - A reference to the current state object for the client
}
Response Handler for HTTP
In HTTP, the custom handler is invoked with the result of the operation performed. For example, if you did a POST operation, you could handle the response sent by the server.
function(topic, response, state)
{
//topic - The URL for which operation was performed
//response - A buffer object which is sent by the server
//state - A reference to the current state object for the client
}
Response Handler for Raw (TCP/UDP)
In the case of the raw protocols, the custom handler is invoked with the result of every response received.
function(topic, response, state)
{
//topic - undefined, doesn't exist for TCP/UDP
//response - A string that is sent by the server
//state - A reference to the current state object for the client
}
Response Handler for CoAP
CoAP handlers are invoked both as a response and as a result of observation of a particular resource endpoint.
function(topic, response, state)
{
//topic - The URL for which operation was performed
//response - A buffer object which is sent by the server
//state - A reference to the current state object for the client
}
Copy link