Getting Started with MQTT as a Message Broker

Kasjful Kurniawan
Bento Tech Innovation
4 min readJun 16, 2023

As a developer communication between devices is important. As we know HTTP it’s a standard communication used for client and server connections. The alternative service for communication between devices is MQTT as a message broker with publisher and subscriber method. The MQTT it’s a minimum-sized transfer protocol. For the REST API user, MQTT is a new method for message transfer between client and server. The MQTT it’s an alternative to WebSocket services. Maybe that can be replaced with MQTT services in Backend and Frontend services.

MQTT is a standards-based messaging protocol, or set of rules, used for machine-to-machine communication. MQTT is mostly found in Internet of Things (IoT) devices for communicating between machines or devices to servers. These IoT devices use MQTT For data transmission, as it is easy to implement and can communicate data efficiently. MQTT supports messaging between devices to the cloud and the cloud to the device.

The MQTT services are used for communication with the customer and seller in our company. Seller and customer can be chat to frequently through MQTT services.

Why choose MQTT?

The main concern is that choosing MQTT as a communication protocol is lightweight for data communication. MQTT uses 2 bytes of header, which makes it so fastest communication protocol than HTTP as REST API. They are the advantages of MQTT Protocols :

1. MQTT is a Publisher and Subscriber protocol.

The MQTT is like publisher and subscriber protocol like a websocket for communication between client and server. Maybe can be replaced websocket as message communication between client and server. As publisher and subscriber protocol the communication between the client and server can be delivered fastest than REST API in HTTP.

In the Pub-Sub model, the protocols have a centralized message broker that distributes data between all devices. Each device can be a publisher or subscriber or both of them at the same time. In the Pub-Sub model, each device only connects to the broker and doesn’t know about other present devices on the network. ‌The broker increases network performance in uncertain conditions.

2. MQTT is a lightweight protocol.

MQTT was invented and developed by IBM for monitoring oil pipelines in the desert through satellite. MQTT has only 2 Bytes on the protocol header size. It means MQTT protocol packets are smaller than HTTP. HTTP protocols need 8000 Bytes only for headers.

The advantage of this lightweight protocol is its usability on small devices with low memory and low processing power.

3. Flexible and scalable

Another advantage of MQTT is flexible and scalable. The protocol allows the devices to publish and subscribe to multiple topics. The hierarchical structure of the MQTT pub-sub model enables the devices to exchange relevant and specific information without unnecessary data transfer.

How MQTT Works?

Some entities in MQTT work together to make up protocol. They are some entities in the protocol that have different functions here:

Publisher: Using the Topic, this device communicates with subscribers.

Topic: Each resource has a different identifier. The publisher communicates with the topic first, who then relays the message to the subscriber.

Subscriber: The end device that receives messages from the publisher via the topic is referred to as a subscriber.

Broker: The server is the organizational communication channel between publishers and subscribers.

In MQTT HBMQTT, gmqtt, and paho-mqtt lib can be used in place of a cloud platform if developing a system employing MQTT communication is not appropriate.

Quality of Service (QoS) level, a crucial component of the MQTT protocol, is an agreement between a message’s sender and recipient. It specifies the degree of assurance with which the system can transmit a certain message.

The option to select a service level that matches the network dependability and application logic of the client is provided by QoS. Communication in faulty networks is much safer and more secure, that’s communication created with QoS since MQTT controls message re-transmission and ensures delivery (even when the underlying transport is problematic).

Installing MQTT

First, you must set up a mosquito broker. For instructions on installing the MQTT broker on your Windows, Mac, and Linux computer, visit the official Mosquitto Downloads page. You can also download and use the official Docker image from Docker Hub if you are more familiar with it. Pull the image with this command:

docker pull eclipse-mosquitto:latest

For instructions to run mqtt from the downloaded image, you can run with this command:

docker run -it -p 1883:1883 -p 1883:1883 eclipse-mosquitto:latest

This command starts the container that will be listening on port 1883 for incoming connections and downloads the eclipse-mosquitto image.

Connecting the MQTT

Finally, you can connect to MQTT broker now. I prefer using the MQTTX as desktop client for MQTT connection. See the website for installation instructions.

Here, you can fill with localhost as host for testing it after run a mosquitto locally.

After that, you can use an mqttx with a pub-sub model based on the topic for the mqtt connection. Finally, the mqtt is ready to use.

--

--