Mqtt is a one of the leading protocol in Internet of things. The Mqtt stands for Message Queuing telemetry transport. This protocol is based on PUB-SUB model. The Broker, Publisher and a subscriber are required for the communication. This protocol operates over Wi-Fi..
The client which transmits the message is called a Publisher .The Client which receives the message is called Subscriber. The communication is based on the topic
The Publisher client publishes the message over certain topic to the broker and this message can be received by any client subscriber which is subscribed to the broker over this topic. Thus publisher does not which client is receiving this message and the subscriber client does not know which publisher has transmitted the message. Mqtt is a lightweight protocol hence can be easily used by Constrained application based device where low memory is available like microcontrollers, Arduino, Raspberry pi etc,
Publishing a message
The process of transmitting the message is called publishing the message. The clients which publishes the message is called publisher.
The message needs to be published over a topic. There is no restriction of topic specification. But the broker can restrict some topics. The Publisher can publish the message on a single topic, It can not publish message to multiple topics. The publisher publishes the message to the broker not to the other client directly. However, the group of Clients can receive the same message over a topic to which they are subscribed.
Qos:Quality of the service
Ever message published by the client has Qos.If not specified then default value 0 is considered. The Qos has three values 0,1,2. The default value is zero.
When Qos is set as 0 means there is no guarantee that the message will be received by the client.
When Qos is set as 1 means there is a guarantee that the message will be received but duplicates messages may be delivered. The broker acknowledges the message.
When Qos is set as 1 means the message is delivered and acknowledged by the Broker.The duplicate messages will not be received.
Retain Flag
While publishing the message when the retain flag is set that means the message is retained by the broker.When subscriber joins the broker on the same topic on message is already published with retain flag is set, It can receive the message.
The case in which sensor publishes the status of the Door when change happens then retain flag can be used. When any subscriber joins the broker over same topic can receive the status.
The events when the client publisher publishes message
The activities happening when any client publishes the message to the broker are as given below.
The client publishes the message hello over a topic.Here the mosquitto commands of Mosquitto Mqtt is used.
Start Mosquitto Broker by running the command mosquitto -v from command prompt.
C:\mosquitto>mosquitto -v
1658378964: mosquitto version 2.0.14 starting
1658378964: Using default config.
1658378964: Starting in local only mode. Connections will only be possible from clients running on this machine.
1658378964: Create a configuration file which defines a listener to allow remote access.
1658378964: For more details see https://mosquitto.org/documentation/authentication-methods/
1658378964: Opening ipv4 listen socket on port 1883.
1658378964: Opening ipv6 listen socket on port 1883.
1658378964: mosquitto version 2.0.14 running
Run the command mosquitto_pub -h localhost -t test -m hello -i client from other cmd prompt terminal.
Activities in Publisher terminal.
mosquitto_pub -h localhost -t test -m hello -i client -d
Client client sending CONNECT
Client client received CONNACK (0)
Client client sending PUBLISH (d0, q0, r0, m1, 'test', ... (5 bytes))
Client client sending DISCONNECT
The publisher client sends connect request to Broker. In return it receives Con Acknowledge from Broker.
The client transmits message over the topic test.
After this, Client issues Disconnect commands which terminates the connection
Activities in Broker Terminal
mosquitto -v
1658386843: mosquitto version 2.0.14 starting
1658386843: Using default config.
1658386843: Starting in local only mode. Connections will only be possible from clients running on this machine.
1658386843: Create a configuration file which defines a listener to allow remote access.
1658386843: For more details see https://mosquitto.org/documentation/authentication-methods/
1658386843: Opening ipv4 listen socket on port 1883.
1658386843: Opening ipv6 listen socket on port 1883.
1658386843: mosquitto version 2.0.14 running
1658386902: New connection from ::1:64363 on port 1883.
1658386902: New client connected from ::1:64363 as client (p2, c1, k60).
1658386902: No will message specified.
1658386902: Sending CONNACK to client (0, 0)
1658386902: Received PUBLISH from client (d0, q0, r0, m0, 'test', ... (5 bytes))
1658386902: Received DISCONNECT from client
1658386902: Client client disconnected.
The activity block shows following events
The New connection request sent by the client to the broker.The Broker accepts the connection and sends Ack to the client
The broker receives the topic test and message of 5 bytes.
Then Client sends disconnect message and the connection is broken.
Subscribing to the Broker
The client which receives the message from the broker over the topic is called Subscriber client.The subscriber client connects with the broker over the desired topic. The client can subscribe to the wild card # topics to receive messages from multiple clients. The Qos Quality of service has same meaning as it has in case of Publisher's client.
Run the command mosquitto_sub from the command prompt terminal to know the process of connection of Subscriber with the Broker.
Activities in Subscriber Terminal
C:\mosquitto>mosquitto_sub -h localhost -t test -i client1 -d
Client client1 sending CONNECT
Client client1 received CONNACK (0)
Client client1 sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0, Options: 0x00)
Client client1 received SUBACK
Subscribed (mid: 1): 0
The client1 sends the Connect message to the Broker. The client receives Acknowledge from the Broker.
Client sending SUBSCRIBE frame to the Broker with topic test and Qos =0.
Client1 receives SUBACK from the broker. and the client1 gets connected with the Broker.
Activities on Broker Terminal
C:\mosquitto>mosquitto -v
1658378964: mosquitto version 2.0.14 starting
1658378964: Using default config.
1658378964: Starting in local only mode. Connections will only be possible from clients running on this machine.
1658378964: Create a configuration file which defines a listener to allow remote access.
1658378964: For more details see https://mosquitto.org/documentation/authentication-methods/
1658378964: Opening ipv4 listen socket on port 1883.
1658378964: Opening ipv6 listen socket on port 1883.
1658378964: mosquitto version 2.0.14 running
1658379915: New connection from ::1:49868 on port 1883.
1658379932: New client connected from ::1:49868 as client1 (p2, c1, k60).
1658379932: No will message specified.
1658379932: Sending CONNACK to client1 (0, 0)
1658379932: Received SUBSCRIBE from client1
1658379932: test (QoS 0)
1658379932: client1 0 test
1658379932: Sending SUBACK to client1
1658379992: Received PINGREQ from client1
1658379992: Sending PINGRESP to client1
1658380052: Received PINGREQ from client1
1658380052: Sending PINGRESP to client1
1658380112: Received PINGREQ from client1
1658380112: Sending PINGRESP to client1
1658380173: Received PINGREQ from client1
1658380173: Sending PINGRESP to client1
1658380232: Received PINGREQ from client1
1658380232: Sending PINGRESP to client1
1658380292: Received PINGREQ from client1
1658380292: Sending PINGRESP to client1
The broker receives the connection request from the broker. The connection is accepted by the broker.
The CONNACK is send by the broker back to the subscriber
The SUBCRIBE Request is received by the broker. from client1.over topic test with QoS = 0.
The broker sends SUBSACK to the client1.
The Subscriber client1 sends PINGREQ to the Broker periodically to check any message from the broker .In return it receives PINGRESP,
Publisher and Subscriber communication:
Let us use the Eclipse mosquitto Mqtt commands from command prompt of windows to publish and receive the message.
Publishing the message:
c:\mosquitto>mosquitto_pub -h localhost -t test -m hello -r -d
Client null sending CONNECT
Client null received CONNACK (0)
Client null sending PUBLISH (d0, q0, r1, m1, 'test', ... (5 bytes))
Client null sending DISCONNECT
The publisher client publishes the message "hello" over topic test on local network.
Receiving the message:
c:\mosquitto>mosquitto_sub -h localhost -t test -d
Client null sending CONNECT
Client null received CONNACK (0)
Client null sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0, Options: 0x00)
Client null received SUBACK
Subscribed (mid: 1): 0
Client null received PUBLISH (d0, q0, r1, m0, 'test', ... (5 bytes))
hello
The subscribe client receives the message "hello" over topic test which is published by the Publisher client.
The logs for the Mosquitto Mqtt Broker Terminal
C:\mosquitto>mosquitto -v
1658451529: mosquitto version 2.0.14 starting
1658451529: Using default config.
1658451529: Starting in local only mode. Connections will only be possible from clients running on this machine.
1658451529: Create a configuration file which defines a listener to allow remote access.
1658451529: For more details see https://mosquitto.org/documentation/authentication-methods/
1658451529: Opening ipv4 listen socket on port 1883.
1658451529: Opening ipv6 listen socket on port 1883.
1658451529: mosquitto version 2.0.14 running
1658451674: New connection from ::1:50230 on port 1883.
1658451674: New client connected from ::1:50230 as auto-8C126B0E-E331-7C2B-0663-7A720B7DDA45 (p2, c1, k60).
1658451674: No will message specified.
1658451674: Sending CONNACK to auto-8C126B0E-E331-7C2B-0663-7A720B7DDA45 (0, 0)
1658451674: Received PUBLISH from auto-8C126B0E-E331-7C2B-0663-7A720B7DDA45 (d0, q0, r1, m0, 'test', ... (5 bytes))
1658451674: Received DISCONNECT from auto-8C126B0E-E331-7C2B-0663-7A720B7DDA45
1658451674: Client auto-8C126B0E-E331-7C2B-0663-7A720B7DDA45 disconnected.
1658451796: New connection from ::1:50233 on port 1883.
1658451796: New client connected from ::1:50233 as auto-A7E328C8-823D-A03D-AEEB-531934A34C84 (p2, c1, k60).
1658451796: No will message specified.
1658451796: Sending CONNACK to auto-A7E328C8-823D-A03D-AEEB-531934A34C84 (0, 0)
1658451796: Received SUBSCRIBE from auto-A7E328C8-823D-A03D-AEEB-531934A34C84
1658451796: test (QoS 0)
1658451796: auto-A7E328C8-823D-A03D-AEEB-531934A34C84 0 test
1658451796: Sending SUBACK to auto-A7E328C8-823D-A03D-AEEB-531934A34C84
1658451796: Sending PUBLISH to auto-A7E328C8-823D-A03D-AEEB-531934A34C84 (d0, q0, r1, m0, 'test', ... (5 bytes))
1658451856: Received PINGREQ from auto-A7E328C8-823D-A03D-AEEB-531934A34C84
The red color log shows the transaction happening between the Broker and the Publisher client.
The Blue color log shows the transaction between the Broker and the Subscriber. The broker transmits the message received from the client to the Subscriber. Please note here,the subscriber client has been started after the message is published by the Publisher. As retain flag -r is set, the subscriber receives the message even after joining the Broker late.
Related Blogs:
Install Mosquitto Mqtt Broker on Windows based PC
Related Videos: