Building a serverless IoT backend integration
Published: 2020-08-11 14:28:24

Having a lightweight protocol which delivers messages over low bandwidth or unreliable networks is the foundation for a system that can facilitate the transports of data in near real time.

MQTT is one such protocol which designed to overcome following limitations:

**Network limitation**

* Limited network bandwidth

* High latency

* Fragile connections

**Device Limitation**

* Limited memory

* Processing capabilities

MQTT is an open OASIS and ISO standard lightweight, publish-subscribe network protocol that transports messages between devices. The protocol usually runs over TCP/IP; however, any network protocol that provides ordered, lossless, bi-directional connections can support MQTT.

In this article we will be building a serverless backend service on AWS lambda which will integrate with a MQTT broker for publishing the passenger service request to the right group of taxi drivers. We will be building the passenger request API using Kumologica. [Kumologica](https://www.kumologica.com/) is a free low-code development tool to build serverless integrations. You can learn more about Kumologica in this [medium article](https://medium.com/@kumologica/kumologica-first-low-code-development-tool-for-aws-lambda-8831b5237428) or subscribe to our YouTube [channel](https://www.youtube.com/channel/UCsTDAlhijUFpsfThJu3hF9g) for the latest videos.

## **Use case**

In this use case we are having a passenger making a request from his mobile app for a taxi. The mobile app makes a call to the Kumologica *passenger requester API* which accepts the request and verifies in the database for the availability of the cab. Based on the availability status and the type of taxi requested by the passenger the flow will route the message to publish in the appropriate taxi topic on MQTT broker. The driver app will receive the message from the MQTT broker.

![High level use case flow](https://cdn-images-1.medium.com/max/2000/1*WJSOxFKZOh4oRjmiU9jgXw.png)*High level use case flow*

**Note:** In this article we are not covering the section where the driver app accepts the request and sends confirmation to the passenger. If you are interested in this section please let us know and we will put another article covering this use case.

## Prerequisite

1. Kumologica designer installed in your machine. [https://kumologica.com/download.html](https://kumologica.com/download.html)

1. [https://www.hivemq.com/public-mqtt-broker/](https://www.hivemq.com/public-mqtt-broker/)

3. DynamoDB table with the following records.

![DynamoDB table structure and data](https://cdn-images-1.medium.com/max/3028/1*RPtyOg_cnSI1y37rYJllQg.png)*DynamoDB table structure and data*

## Implementation

In order to implement the Passenger request API we will be following the below given logic

![Flow logic](https://cdn-images-1.medium.com/max/2000/1*nyBFIgZtfS_FRahKs94sBQ.png)*Flow logic*

1. Open Kumologica Designer, click the *Home* button and choose *Create New Kumologica Project*.

Enter name (for example PassengerRequestAPI), select directory for project and switch *Source* into *From Existing Flow …*

2.Copy and Paste the following [flow](https://github.com/KumologicaHQ/kumologica-demos/tree/master/IoTSampleUsingMQTT)

3.press *Create* Button.

**Install MQTT Node.**

MQTT node is a contribution node and must be explicitly installed in Kumologica Designer. Press *Add more nodes* button located in the bottom left corner of designer. You will be presented screen with all contribution nodes that are currently available. Choose MQTT node, press *Install* button. After few seconds, plugin will be installed and you will be prompted to restart Kumologica Designer. MQTT node will be located under the new category: *Platforms* after restart.

![Add more nodes panel](https://cdn-images-1.medium.com/max/4428/1*Dr8pZGY8HNGJ_MozYrv54w.png)*Add more nodes panel*

You should be seeing two flows as given below on the designer canvas.

The first flow is the API endpoint with the resource path **/passenger/request**. This flow accepts the request from the mobile app and drops the message to Amazon SQS queue (*PassengerSQS*). This is done as an event driven model in order to acknowledge back (to mobile app)quickly.

![](https://cdn-images-1.medium.com/max/2380/0*cAzLARQAZ04tCxHI)

The second flow is having the logic to fetch the driver data from the DynamoDB based on the areacode (area_code primary key) which is passed as request by the passenger. The flow picks the message from the SQS queue and invokes the DynamoDB. The flow will then check the *availability* field in the dynamodb response data. If the *availability* is having the value as ***yes*** then the flow checks for the appropriate MQTT topic to be selected. This done by checking the *vehicle_type* field in the dynamodb response data. Based on the *vehiclet_type* the MQTT node is invoked to publish the message to the MQTT broker topic in Hive.

**Note:** The configuration for the Hive MQTT broker for this usecase is available in the [HiveMQTT](https://www.hivemq.com/public-mqtt-broker/) site

![](https://cdn-images-1.medium.com/max/4420/1*2iYVcKelfADvFIZe1vCpwA.png)

**Note:** To try out the above flow ensure to furnish the settings of **MQTT** node, **SQS** node and **AWS DynamoDB** node with appropriate connection configuration.

**Deploy flow to AWS Lambda**

Select *CLOUD* tab on the right panel of Kumologica designer, choose AWS Profile and press *Deploy* button. This will trigger build and deployment of flow into AWS Lambda. On the trigger section you need to add to triggers.

1. API Gateway

1. Amazon SQS

![Trigger configuration for API gateway and SQS](https://cdn-images-1.medium.com/max/2000/0*D_HOpSLN3QFS_amv.png)*Trigger configuration for API gateway and SQS*

## Testing

1. Paste the below give payload in your postman or any rest client of your choice.

    {
     "areacode" : "2331",
     "name" : "John",
     "seat" : "4"
    }

Provide the urls as [https://<<your hostname>>/test/passenger/request](https://7salaz4cw5.execute-api.ap-southeast-2.amazonaws.com/test/passenger/request). Method as *POST* and data format as application/json.

2.Open the HiveMQTT [browser client](http://www.hivemq.com/demos/websocket-client/) and click **connect**. Once the status changes to green. Add the topic subscription as given below.

    testtopic/3seater
    testtopic/4seater
    testtopic/5seater

All the 3 topic subscriptions are ready to fetch the messages from the appropriate topic.

3.Now click *Send* in the postman. Now when you check the HiveMQTT browser client you should see the event in the appropriate topic.

![HiveMQTT browser client](https://cdn-images-1.medium.com/max/4100/1*lOIQ4bMA6JtJQGtQmOLLMw.png)*HiveMQTT browser client*

## Conclusion

This article presented how easy Kumologica Designer flow orchestrates and integrates with a MQTT broker platform using MQTT node. Within few minutes we have created and deployed the *passenger request* Kumologica flow.

Remember Kumologica is totally free to [download](https://kumologica.com/download.html) and use. Go ahead and give it a try, we would love to hear your feedback.

In the meantime, if there is any specific use case that you would like to see in a future article please let us know in the comments section.