The Eclipse moquitto provides the open source message Broker which supports the Mqtt Protocol versions 5.0,3.11,3,1.The Mqtt protocol is a very lightweight and can be operated on microcontroller based systems like Raspberry Pi to computer PC servers. The Mqtt clients can be operated from the small microcontroller based sensor which are constrained.
The Mosquitto Brokers can be installed on your systems by visiting the Eclipse mosquitto Download page.Here the setup file suitable to various os can be downloaded and installed. IF want to How to setup the Mosquiito Broker on WIndows PC then click here.
Once the Broker is setup on the system. We can initiate testing of the Mosquitto Broker by using mosquitto Publish and Subscribe Commands. To know some details about the Mqtt Publish and Subscribe operation details visit the blog Publish and Subscribe-Mqtt.
Test mosquitto mqtt broker with Publish and Subscribe operations
To test the Mqtt operation on Mosquitto Broker we will be using the windows os.So go the command prompt and change the directory to the folder where mosquitto related files are stored.(refere article setup the Mosquiito Broker on WIndows PC ).
Start mosquitto mqtt broker or mosquitto server
Start the mosquitto broker by typing the command mosquitto -v where mosquitto is the mosquitto application file and v stands for verbose mode where the operations on broker are visible on the terminal
C:\Users\deshp>cd c:\mosquitto
c:\mosquitto>mosquitto -v
1658465158: mosquitto version 2.0.14 starting
1658465158: Using default config.
1658465158: Starting in local only mode. Connections will only be possible from clients running on this machine.
1658465158: Create a configuration file which defines a listener to allow remote access.
1658465158: For more details see https://mosquitto.org/documentation/authentication-methods/
1658465158: Opening ipv4 listen socket on port 1883.
1658465158: Opening ipv6 listen socket on port 1883.
1658465158: mosquitto version 2.0.14 running
The terminal log shows mosquitto broker is started successfully and is running. The broker is listening to the port 1883 which is a default port for Mqtt for insecure mode.
Publish the message
Start another command prompt and go to same folder from where mosquitto broker is started.
Type the command mosquitto_pub -h localhost -t test -m hello -i clientPub -d
where
h stands for host name it can localip(localhost) or remote ip
t stands for topic.Here it is test
m stands for message. Here it is hello
i stands for client name. Here it is set as clientPub
d stands for debug mode.
Here QoS value id 0 in default.
Run the command in the command prompt.
c:\mosquitto
c:\mosquitto>mosquitto_pub -h localhost -t test -m hello -i clientPub -d
Client clientPub sending CONNECT
Client clientPub received CONNACK (0)
Client clientPub sending PUBLISH (d0, q0, r0, m1, 'test', ... (5 bytes))
Client clientPub sending DISCONNECT
The log on Publisher client terminal shows the Client sends the Connect request to the Broker. The Broker accepts it and acknowledge with ConnAck
Client publishes the message hello of length 5 with topic as test
After sending the message the Client issues disconnect command to the broker.
Check the log on mosquitto server terminal
1658466778: New connection from ::1:56571 on port 1883.
1658466778: New client connected from ::1:56571 as clientPub (p2, c1, k60).
1658466778: No will message specified.
1658466778: Sending CONNACK to clientPub (0, 0)
1658466778: Received PUBLISH from clientPub (d0, q0, r0, m0, 'test', ... (5 bytes))
1658466778: Received DISCONNECT from clientPub
1658466778: Client clientPub disconnected.
The broker gets the connection request from the publisher clientPub. The Broker accepts the connection and sends Ack command.
The Broker Receives the published message with topic test.
When received Disconnect command from the Publisher client Broker Disconnects the session.
Publish the message with Qos set as 1.
Type and run this command mosquitto_pub -h localhost -t test -m hello -q 1 -i clientPub -d
where q- stands for QoS (Quality of Service).Here it is set as 1.
The log from the Publisher terminal
c:\mosquitto>mosquitto_pub -h localhost -t test -m hello -i clientPub -q 1 -d
Client clientPub sending CONNECT
Client clientPub received CONNACK (0)
Client clientPub sending PUBLISH (d0, q1, r0, m1, 'test', ... (5 bytes))
Client clientPub received PUBACK (Mid: 1, RC:0)
Client clientPub sending DISCONNECT
When the client publishes the message it receives PUBACK from the Broker.
Check the log from mosquitto mqtt broker Terminal.
1658467448: New connection from ::1:56593 on port 1883.
1658467448: New client connected from ::1:56593 as clientPub (p2, c1, k60).
1658467448: No will message specified.
1658467448: Sending CONNACK to clientPub (0, 0)
1658467448: Received PUBLISH from clientPub (d0, q1, r0, m1, 'test', ... (5 bytes))
1658467448: Sending PUBACK to clientPub (m1, rc0)
1658467448: Received DISCONNECT from clientPub
1658467448: Client clientPub disconnected.
Here note the PUBACK sent by the Broker after receiving Published message from the client.
Publish the message with QoS set as 2.
Type and run the Pub command from the command prompt where QoS is set as 2
mosquitto_pub -h localhost -t test -m hello -i clientPub -q 2 -d
c:\mosquitto>mosquitto_pub -h localhost -t test -m hello -i clientPub -q 2 -d
Client clientPub sending CONNECT
Client clientPub received CONNACK (0)
Client clientPub sending PUBLISH (d0, q2, r0, m1, 'test', ... (5 bytes))
Client clientPub received PUBREC (Mid: 1)
Client clientPub sending PUBREL (m1)
Client clientPub received PUBCOMP (Mid: 1, RC:0)
Client clientPub sending DISCONNECT
Here the client has received the PUBREC an acknowledgment sent by the Broker.
If the client does not receive this Acknowledge then it sends the message again with DUP flag set as 1
indicating the message is duplicate.
After this,the client sends PUBREL command to the Broker.Now the message can be deleted from the queue.
The Broker has sent PUBCOMP which indicates the Publishing procedure is completed.
Then the publisher client can go for Disconnection.
Check the log form mosquitto server Terminal
1658468003: New connection from ::1:56618 on port 1883.
1658468003: New client connected from ::1:56618 as clientPub (p2, c1, k60).
1658468003: No will message specified.
1658468003: Sending CONNACK to clientPub (0, 0)
1658468003: Received PUBLISH from clientPub (d0, q2, r0, m1, 'test', ... (5 bytes))
1658468003: Sending PUBREC to clientPub (m1, rc0)
1658468003: Received PUBREL from clientPub (Mid: 1)
1658468003: Sending PUBCOMP to clientPub (m1)
1658468003: Received DISCONNECT from clientPub
1658468003: Client clientPub disconnected.
The Broker after receiving the message sends the Acknowledge PUBREC to the client.
The Broker receives PUBREL command from the broker which indicates the broker now can distribute this message to the subscriber.
After this the Broker sends PUBCOMP indicating the completion of the message transaction between the Broker and the client. So the duplication of the message us avoided when QoS is set as 2.
Subscribing to the Broker
Start another command prompt and go to the same folder where the mosquitto Broker files are stored. Type and run the following command from the command prompt to subscribe to the Broker
mosquitto_sub -h localhost -t # - i clientSub - d
where
h stands for host name it can localip(localhost) or remote ip
t stands for topic name. Here it is set as wild card.Any topic message can be received by this client.
i stands for clientname.It is set as clientSub
d stands for debug mode.
The Log for this command operation
c:\mosquitto>mosquitto_sub -h localhost -t # -i clientSub -d
Client clientSub sending CONNECT
Client clientSub received CONNACK (0)
Client clientSub sending SUBSCRIBE (Mid: 1, Topic: #, QoS: 0, Options: 0x00)
Client clientSub received SUBACK
Subscribed (mid: 1): 0
The client clientSub sends the Connect request to the Broker.
The Broker accepts the Connection request and sends ConnAck back to the client.
The client sends Subscribe Request to the Broker which is accepted by the broker. The Broker inreturn issues the Suback to the Cleint.
The client waits and pings the Broker for any message.
The log for Mosquitto Broker.
1658492081: New connection from ::1:62812 on port 1883.
1658492081: New client connected from ::1:62812 as clientSub (p2, c1, k60).
1658492081: No will message specified.
1658492081: Sending CONNACK to clientSub (0, 0)
1658492081: Received SUBSCRIBE from clientSub
1658492081: # (QoS 0)
1658492081: clientSub 0 #
1658492081: Sending SUBACK to clientSub
1658492142: Received PINGREQ from clientSub
1658492142: Sending PINGRESP to clientSub
The Broker receives the connection request from the clientSub over port 1883.
The Broker accepts the connection request sends the Conn Ack back to the client.
The client sends Subscribe request to the Broker which is accepted by the broker.
the Broker sends SubAck to the client.
The Broker receives PingReq from the client which is acknowledged from the Broker by sending PingResp.
Receive the published message from the Broker
Type and run the command from the the command prompt for the publisher client which sends hello message over topic test.
mosquitto_pub -h localhost -t test -m hello -i clientPub --d
Check the log from Subscriber client terminal.
Client clientSub sending PINGREQ
Client clientSub received PINGRESP
Client clientSub sending PINGREQ
Client clientSub received PINGRESP
Client clientSub received PUBLISH (d0, q0, r0, m0, 'test', ... (5 bytes))
hello
Client clientSub sending PINGREQ
Client clientSub received PINGRESP
The Subscriber client pings the Broker for any message and receives the message "hello" over wild card topic.
The Log for Mosquitto Broker
1658492954: New connection from ::1:62826 on port 1883.
1658492954: New client connected from ::1:62826 as clientPub (p2, c1, k60).
1658492954: No will message specified.
1658492954: Sending CONNACK to clientPub (0, 0)
1658492954: Received PUBLISH from clientPub (d0, q0, r0, m0, 'test', ... (5 bytes))
1658492954: Sending PUBLISH to clientSub (d0, q0, r0, m0, 'test', ... (5 bytes))
1658492954: Received DISCONNECT from clientPub
1658492954: Client clientPub disconnected.
1658492981: Received PINGREQ from clientSub
The broker receives the published message from the Publisher client
The broker then sends the message to the subscriber.
You can run the command for Subscriber client specifying the topic name eg.topic as test and test the response.
Related Blogs:
Install Mosquitto Mqtt Broker on Windows based PC
Related Videos: