---
# System prepended metadata

title: MQTT
tags: [IoT over Kubernetes, MQTT]

---

# MQTT

###### tags: `IoT over Kubernetes` `MQTT`

Reference: 
- https://www.youtube.com/watch?v=tQmXWNd1pNk
- https://www.youtube.com/watch?v=WmKAWOVnwjE

## MQTT Overview

The IoT is all about interconnecting devices and one lightweight, secure way for online devices to communicate is using MQTT.

MQTT is a publish/subscribe based messaging protocol that avoids direct connections between devices by relaying data through a center server called the the broker.

What is broker?
Broker is a Software on a computer, on premise or in the cloud, self-built or 3rd party hosted, open source or proprietary.

Broker is a forwarder between two connected devices. MQTT doesn't use address, but use the subject line: "topic".

Broker Benefits:
- Eliminates Insecure Connections
- Easlily Scales
- Manages Client Connect States

Flow:
![](https://i.imgur.com/Ou7II1U.png)

It's not only have one to one relationship, but MQTT broker is possible to connect to more devices which has the same topic.
![](https://i.imgur.com/y0AZMDV.png)

one to many capabilities.

Similiarly, the broker has capability to handle many to one. Just like database system for the same topic and sends all messages.
![](https://i.imgur.com/lwi3wHk.png)

MQTT is really desirable in IoT because it's easy to add new devices without touching the existing infrastucture and since new devices only need to communicate with the broker they don't actually need to be compatible with the other clients. 

MQTT is a BI-directional Communication Protocol because It can work in two way communication, means devices can publish data while still being able to receive configuration information on control command.
![](https://i.imgur.com/zyC8BFq.png)

MQTT is a powerful for both sharing data while as managing and controlling devices.

### Security

MQTT using transport layer security:
- TLS Encryption
- Username & passowrd protected connection
- Optional certification to requeires clients to provided scientifical file to manage server
- Each Client Unaware of Others

### Retained Messages
To log historical data, MQTT using database client for long term and keep of some specific topic to retain. 
![](https://i.imgur.com/wfeGL2N.png)

Besides publish data to the subscribers, MQTT also send it to database client.

### Persistant Session

To keep track of which client subscriber's of what topics, or the retain topic, and to effieciently manage all of the different user account. Broker can easily stores the current session information long term. 

If the MQTT publisher connections has a problem, It can easily reconnect publisher to broker without having re-subscribe of each subscriber. Because the broker remember them as a session information.
![](https://i.imgur.com/E6rDjQr.png)

### Client Connection Status

- Birth
- Death
- Last will and Testament

### Keep system healty

- add subscribers
- monitor connection status
- assign responsibilities
- automatic redundancy
- notify users.

