Handling server response

This section describes how to handle the messages or commands from the cloud side.

IoT communication is bidirectional i.e. device to cloud and cloud to device. So far we have seen how to generate messages and send it 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 function remains the same.

Specifying custom Handler function

In the template, you could specify a custom handler function which will be invoked once a message or response is received from the backend. The function is invoked with parameters specific to the protocol, however it has access to state variable and could use other helper API and features.

function (topic, response, state)
{
console.log("Received a subscription on the topic ", topic)
state.received++;
state.recv_queue.push(response);
//no need for return
}

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 above example, we have simply produced a log and pushed the received message to the receive queue.

Note that subscription handler do not need to return any value.

Subscription handler could be invoked multiple time asynchronously while your simulation is running. That's why result of subscription should be stored in a queue rather than being processed as a second run could overwrite the previous run results.

Let's now look at the response handling functions for each protocol

Custom Handler for MQTT

In MQTT, a client could subscribe to any particular topic. The subscription topic can be specified in the template by enabling subscription checkbox below message function. 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 - reference to the current state object for the client
}

Custom Handler for HTTP

In HTTP the custom handler is invoked with the result of the operation performed. E.g. if you did a POST operation, you could handle the response sent by the server.

function(url, response, state)
{
//url- The URL for which operation was perfomed
//response - A buffer object which is sent by the server
// state - reference to the current state object for the client
}

Custom 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(response, state)
{
//response - A buffer object which is sent by the server
// state - reference to the current state object for the client
}

Custom Handler for CoAP

CoAP handlers are invoked both as a response and as a result of observation of a particular resource endpoint.

function(url, response, state)
{
//url- The URL for which operation was perfomed
//response - A buffer object which is sent by the server
// state - reference to the current state object for the client
}