MQTT (MQ Telemetry Transport) is a lightweight messaging protocol that provides resource-constrained network clients with a simple way to distribute telemetry information. The protocol, which uses a publishing / subscribing communication pattern, is used for machine-to-machine (M2 M) communication and plays an important role in the Internet of Things ( IoT).
The MQTT protocol is a good alternative for wireless networks with varying latency rates due to occasional bandwidth limitations or poor connections.
The MQTT protocol has two parts: the client and the broker.
The MQTT broker is a server, while the client is a linked computer. When a device — or client — wants to send data to a server — or a broker — it is called a publisher. When the process is reversed, a subscription is called.
If a link is broken from a subscriber to a broker, the broker can buffer messages and transfer them to the subscriber when they are back online.
If the connection from the publishing client to the broker is broken without warning, the broker will close the connection and send a cached message to the subscribers with instructions from the publisher.
While the TT in MQTT stands for Telemetry Transport, the MQ refers to a product called IBM MQ.
How to Install MQTT Broker in ubuntu 18.04?
Installation steps for MQTT Broker in ubuntu 18.04
Lets install Install Mosquitto Broker(server) in ubuntu 18.04
First of all, open your Terminal (ctrl+Alt+t) Then enter the below command to update your OS.
sudo apt-get update
Install mosquitto broker command
sudo apt-get install mosquitto
Install the Clients for Testing the connection with mosquitto server.
sudo apt-get install mosquitto-clients
Setting Up Password For Mosquitto MQTT
Secure with a Password!
Let’s use mosquitto_passwd command to create a password file.
This file will be located in the directory:- /etc/mosquitto/passwd, and it makes it easy for Mosquitto to verify all connections.
runwithcode is my username which i want to use to verify the connection
sudo mosquitto_passwd -c /etc/mosquitto/passwd runwithcode
After entring the above command you have to enter your password twice.
Mosquitto configuration file:
Create a configuration file for Mosquitto pointing to the password file we have just created.
sudo nano /etc/mosquitto/conf.d/default.conf
And replace with the below lines.
allow_anonymousfalse password_file /etc/mosquitto/passwd listener 1883 localhost
port 1883 is the port which is open for the connection, its a default port of MQTT
if you want to add ssl certificate use below steps
Add new listener port 8883 for ssl
And add certfile, cafile and keyfile file location like below
certfile /etc/letsencrypt/live/mqtt.domain.com/cert.pem cafile /etc/letsencrypt/live/mqtt.domain.com/chain.pem keyfile /etc/letsencrypt/live/mqtt.domain.com/privkey.pem
Save and exit the file, then run the below command to restart Mosquitto, allowing the changes to take effect.
sudo systemctl restart mosquitto
You can chaeck the status of the Mosquitto. Using below command.
sudo systemctl status mosquitto
Let’s test the mosquitto broker(server) connection through mosquitto_client
As we know MQTT is a pub-sub based protocol.
Open Two terminals, so that we can publish the data in a topic and subscribe to the other terminal with the same topic.
Start the command line subscriber:
we are using ‘mytest/topic’ as a Topic and subscribe using below command
mosquitto_sub -v -t 'mytest/topic' -p 1883 -u "runwithcode" -P "password"
In another terminal, we send the data on the same topic we subscribed to.
Publish test message with the command line publisher:
mosquitto_pub -t 'mytest/topic' -m 'helloWorld' -p 1883 -u "runwithcode" -P "password"
If you receive the published data in the subscribe terminal means your configuration and install done properly.
I Hope, this MQTT tutorial helped you to install and configure your own MQTT Broker.