0
0
IOT Protocolsdevops~10 mins

mosquitto_pub and mosquitto_sub commands in IOT Protocols - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - mosquitto_pub and mosquitto_sub commands
Start mosquitto_sub
Subscribe to topic
Wait for messages
Start mosquitto_pub
Publish message to topic
Message received by mosquitto_sub
Display message
End
The subscriber listens on a topic waiting for messages. The publisher sends a message to that topic. The subscriber receives and shows the message.
Execution Sample
IOT Protocols
mosquitto_sub -t 'home/temperature'
mosquitto_pub -t 'home/temperature' -m '22C'
Subscribe to 'home/temperature' topic and publish '22C' message to it.
Process Table
StepCommandActionResult/Output
1mosquitto_sub -t 'home/temperature'Start subscriber listening on topic 'home/temperature'Waiting for messages...
2mosquitto_pub -t 'home/temperature' -m '22C'Publish message '22C' to topic 'home/temperature'Message sent
3mosquitto_sub receives messageSubscriber gets message from topicReceived message: 22C
4EndNo more messages sentSubscriber continues waiting or exits if stopped
💡 Execution stops when subscriber is manually stopped or no more messages are published.
Status Tracker
VariableStartAfter Step 2After Step 3Final
topicnone'home/temperature''home/temperature''home/temperature'
messagenone'22C''22C''22C'
subscriber_statewaitingwaitingreceived messagewaiting or stopped
Key Moments - 3 Insights
Why does mosquitto_sub show no output immediately after starting?
Because it waits silently for messages on the subscribed topic until a message is published, as shown in step 1 of the execution_table.
What happens if mosquitto_pub publishes to a different topic than mosquitto_sub is subscribed to?
The subscriber will not receive the message because it listens only to the specified topic, so no output appears in step 3.
Can mosquitto_sub receive multiple messages without restarting?
Yes, it keeps listening and displays each message received on the topic until manually stopped, as indicated in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does mosquitto_sub display immediately after starting?
A"Waiting for messages..."
B"Message sent"
C"Received message: 22C"
D"No output"
💡 Hint
Check Step 1 in the execution_table under Result/Output.
At which step does the subscriber receive the published message?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for when 'Received message: 22C' appears in the execution_table.
If the topic in mosquitto_pub is changed to 'home/humidity', what happens in the subscriber output?
ASubscriber receives '22C' message
BSubscriber shows 'Waiting for messages...' indefinitely
CSubscriber shows 'Message sent'
DSubscriber crashes
💡 Hint
Refer to key_moments about topic matching and step 3 in execution_table.
Concept Snapshot
mosquitto_sub -t <topic>  # Subscribe and wait for messages on <topic>
mosquitto_pub -t <topic> -m <message>  # Publish <message> to <topic>
Subscriber waits silently until message arrives
Publisher sends message once
Subscriber shows message when received
Topics must match for message delivery
Full Transcript
This visual execution shows how mosquitto_sub and mosquitto_pub commands work together. First, mosquitto_sub starts and listens on a topic, waiting silently for messages. Then mosquitto_pub sends a message to that topic. The subscriber receives the message and displays it. If the topics do not match, the subscriber does not receive the message. The subscriber keeps listening until stopped. This helps beginners see the flow of publishing and subscribing in MQTT using these commands.