Bird
Raised Fist0
IOT Protocolsdevops~20 mins

mosquitto_pub and mosquitto_sub commands in IOT Protocols - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MQTT Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of mosquitto_pub with QoS 1
What is the output or result when running this command?
mosquitto_pub -h test.mosquitto.org -t "home/temperature" -m "22" -q 1
IOT Protocols
mosquitto_pub -h test.mosquitto.org -t "home/temperature" -m "22" -q 1
AThe command fails with a syntax error due to missing -p port option
BThe message "22" is published but with QoS 0, ignoring the -q flag
CThe message "22" is published to topic "home/temperature" with QoS 1, and the command exits with status 0
DThe command connects but does not send any message because -m is ignored
Attempts:
2 left
💡 Hint
The -q flag sets the Quality of Service level for message delivery.
💻 Command Output
intermediate
2:00remaining
mosquitto_sub output for retained messages
If a retained message "ON" exists on topic "home/lights", what will this command output immediately after subscribing?
mosquitto_sub -h test.mosquitto.org -t "home/lights"
IOT Protocols
mosquitto_sub -h test.mosquitto.org -t "home/lights"
AIt prints an error about missing -m flag
BIt immediately prints "ON" and then waits for new messages
CIt prints nothing until a new message is published
DIt disconnects immediately without output
Attempts:
2 left
💡 Hint
Retained messages are sent to new subscribers immediately.
❓ Configuration
advanced
2:30remaining
Correct mosquitto_pub command to use TLS with client certificate
Which mosquitto_pub command correctly publishes message "Hello" to topic "secure/topic" using TLS with client certificate and key files located at /certs/client.crt and /certs/client.key?
Amosquitto_pub -h broker.example.com -t "secure/topic" -m "Hello" --cafile /certs/ca.crt --cert /certs/client.crt --key /certs/client.key -p 8883
Bmosquitto_pub -h broker.example.com -t "secure/topic" -m "Hello" --cert /certs/client.crt --key /certs/client.key
Cmosquitto_pub -h broker.example.com -t "secure/topic" -m "Hello" --tls --cert /certs/client.crt --key /certs/client.key
Dmosquitto_pub -h broker.example.com -t "secure/topic" -m "Hello" --cafile /certs/ca.crt -p 1883
Attempts:
2 left
💡 Hint
TLS requires specifying the CA file and client cert/key along with the secure port.
❓ Troubleshoot
advanced
2:00remaining
Troubleshooting mosquitto_sub connection failure
You run this command:
mosquitto_sub -h 192.168.1.100 -t "sensor/data" -p 1884

But it fails to connect. What is the most likely cause?
AThe topic name "sensor/data" is invalid
BThe mosquitto_sub command requires -m flag to connect
CThe IP address 192.168.1.100 is unreachable due to firewall
DThe broker is not listening on port 1884, default is 1883
Attempts:
2 left
💡 Hint
Check the broker's listening port and firewall settings.
🔀 Workflow
expert
3:00remaining
Sequence to test message delivery with mosquitto_pub and mosquitto_sub
Arrange the steps in correct order to test publishing and subscribing to a topic using mosquitto_pub and mosquitto_sub on the same machine.
A1,2,3,4
B2,1,3,4
C1,3,2,4
D2,3,1,4
Attempts:
2 left
💡 Hint
Subscribe first to receive messages published after subscription.

Practice

(1/5)
1. What is the main purpose of the mosquitto_pub command in MQTT?
easy
A. To send messages to a specific MQTT topic
B. To listen for messages on a topic
C. To create a new MQTT broker
D. To delete MQTT topics

Solution

  1. Step 1: Understand the role of mosquitto_pub

    The mosquitto_pub command is used to publish or send messages to MQTT topics.
  2. Step 2: Compare with other options

    mosquitto_sub listens for messages, not mosquitto_pub. Creating or deleting topics is not done by this command.
  3. Final Answer:

    To send messages to a specific MQTT topic -> Option A
  4. Quick Check:

    mosquitto_pub = send messages [OK]
Hint: Remember: pub means publish (send) messages [OK]
Common Mistakes:
  • Confusing mosquitto_pub with mosquitto_sub
  • Thinking it manages topics instead of messages
  • Assuming it listens for messages
2. Which of the following is the correct syntax to subscribe to the topic home/temperature using mosquitto_sub?
easy
A. mosquitto_sub -m home/temperature
B. mosquitto_sub -t home/temperature
C. mosquitto_sub -p home/temperature
D. mosquitto_sub -s home/temperature

Solution

  1. Step 1: Identify the correct option flag for topic

    The -t flag is used to specify the topic in mosquitto_sub.
  2. Step 2: Verify other flags

    -p is for port, -m is for message in mosquitto_pub, and -s is not a valid flag here.
  3. Final Answer:

    mosquitto_sub -t home/temperature -> Option B
  4. Quick Check:

    -t means topic [OK]
Hint: Use -t to specify topic in mosquitto_sub [OK]
Common Mistakes:
  • Using -p instead of -t for topic
  • Confusing mosquitto_pub flags with mosquitto_sub
  • Using invalid flags like -s
3. What will be the output when running mosquitto_sub -t sensors/humidity in one terminal followed by mosquitto_pub -t sensors/humidity -m 45 in another terminal connected to the same broker?
medium
A. The subscriber terminal will display nothing
B. The publisher terminal will display '45'
C. The subscriber terminal will display '45'
D. Both terminals will display '45'

Solution

  1. Step 1: Understand the publish-subscribe flow

    mosquitto_pub sends the message '45' to topic sensors/humidity. The subscriber listens on the same topic.
  2. Step 2: Identify where the message appears

    The subscriber terminal receives and displays the message. The publisher terminal does not display the message.
  3. Final Answer:

    The subscriber terminal will display '45' -> Option C
  4. Quick Check:

    Publisher sends, subscriber receives message [OK]
Hint: Subscriber shows messages sent to its topic [OK]
Common Mistakes:
  • Expecting publisher to show message output
  • Thinking subscriber won't receive messages
  • Confusing topic names or broker connection
4. You run mosquitto_sub -t home/lights but do not see any messages even after publishing with mosquitto_pub -t home/lights -m ON. What is the most likely cause?
medium
A. The subscriber is not connected to the same MQTT broker as the publisher
B. The topic name is misspelled in the publisher command
C. The message payload is empty
D. The subscriber command is missing the -m flag

Solution

  1. Step 1: Check connection to MQTT broker

    Both publisher and subscriber must connect to the same broker to exchange messages.
  2. Step 2: Evaluate other options

    Topic names match, message payload is 'ON' (not empty), and subscriber does not use -m flag (which is for publishing).
  3. Final Answer:

    The subscriber is not connected to the same MQTT broker as the publisher -> Option A
  4. Quick Check:

    Same broker needed for pub-sub communication [OK]
Hint: Check broker connection if no messages received [OK]
Common Mistakes:
  • Assuming subscriber needs -m flag
  • Ignoring broker address mismatch
  • Overlooking topic name typos
5. You want to publish the message "Alert: Temperature high!" to the topic alerts/temperature using mosquitto_pub with QoS level 1 and retain the message. Which command is correct?
hard
A. mosquitto_pub -r -q 1 -m "Alert: Temperature high!" alerts/temperature
B. mosquitto_pub -m "Alert: Temperature high!" -t alerts/temperature -q 0 -r
C. mosquitto_pub -t alerts/temperature -m "Alert: Temperature high!" -q 2
D. mosquitto_pub -t alerts/temperature -m "Alert: Temperature high!" -q 1 -r

Solution

  1. Step 1: Identify flags for QoS and retain

    -q 1 sets QoS level 1, and -r sets the retain flag in mosquitto_pub.
  2. Step 2: Check command syntax and order

    mosquitto_pub -t alerts/temperature -m "Alert: Temperature high!" -q 1 -r correctly uses -t for topic, -m for message, -q 1 for QoS, and -r for retain. Other options have wrong QoS, missing retain, or incorrect argument order.
  3. Final Answer:

    mosquitto_pub -t alerts/temperature -m "Alert: Temperature high!" -q 1 -r -> Option D
  4. Quick Check:

    Use -q 1 and -r to set QoS and retain [OK]
Hint: Use -q for QoS and -r to retain message [OK]
Common Mistakes:
  • Using wrong QoS level
  • Forgetting the -r flag for retain
  • Incorrect argument order causing errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard