MQTT is a Client Server publish/subscribe messaging transport protocol. It is light weight, open, simple, and designed so as to be easy to implement. These characteristics make it ideal for use in many situations, including constrained environments such as for communication in Machine to Machine (M2M) and Internet of Things (IoT) contexts where a small code footprint is required and/or network bandwidth is at a premium.
The MQTT protocol aims for these goals:
- Efficiency: the clients are usually very small and don’t have many resources.
- Lightweight: the message header are small to optimize network bandwidth.
- Reliable Delivery: it is important for devices to make sure the message was delivered.
- Bi-directional: both client and server can send messages.
- Data agnostic: it doesn’t matter the format of the message being sent.
- Session: many IoT devices connect over unreliable networks, so it’s important to reduce the time to reconnect the client.
- Security: it is easy to encrypt messages using TLS.
The MQTT is a publish-subscribe protocol. That is a client sends a message (or publish it) without knowing directly who is going to receive the message, if anyone. The same way, another client can receive messages (or subscribe) without knowing which device has sent it.
In this way, to send or to receive messages, the publishers and subscribers only need to know the broker address. Another advantage is that a broker can deliver a message for a client that is not online at the time it was published.
Let’s create a simple application in Go to send and receive messages using MQTT.