📖 Scenario: You are working on a smart home system where multiple devices communicate using MQTT. You need to understand how the MQTT broker manages message delivery between devices.
🎯 Goal: Build a simple simulation of an MQTT broker role using Python dictionaries and functions to show how messages are received from publishers and sent to subscribers.
📋 What You'll Learn
Create a dictionary to hold topics and their subscribers
Add a function to register subscribers to topics
Add a function to publish messages to a topic and deliver to subscribers
Print the messages delivered to each subscriber
💡 Why This Matters
🌍 Real World
MQTT brokers are central in IoT systems to route messages between devices like sensors, lights, and controllers.
💼 Career
Understanding MQTT broker roles helps in configuring and troubleshooting IoT networks and cloud messaging services.
Progress0 / 4 steps
1
Create the topics dictionary
Create a dictionary called topics with these exact keys: 'home/livingroom' and 'home/kitchen'. Each key should have an empty list as its value to hold subscribers.
IOT Protocols
Hint
Think of topics as channels where devices subscribe to get messages.
2
Add subscriber registration function
Add a function called subscribe that takes topic and client as parameters and appends client to the list in topics[topic].
IOT Protocols
Hint
This function lets a device say it wants messages from a topic.
3
Add publish function to send messages
Add a function called publish that takes topic and message as parameters. It should loop over each client in topics[topic] and print "Delivering '{message}' to {client}".
IOT Protocols
Hint
This simulates the broker sending messages to all subscribers of a topic.
4
Subscribe clients and publish messages
Use subscribe to add 'device1' and 'device2' to 'home/livingroom', and 'device3' to 'home/kitchen'. Then call publish to send 'Lights on' to 'home/livingroom' and 'Temperature 22C' to 'home/kitchen'. This will print the delivery messages.
IOT Protocols
Hint
Devices get messages only for topics they subscribed to.
Practice
(1/5)
1. What is the main role of an MQTT broker in IoT communication?
easy
A. To receive messages from devices and forward them to subscribers
B. To generate sensor data for devices
C. To act as a device that publishes messages only
D. To encrypt messages between devices
Solution
Step 1: Understand MQTT broker function
The MQTT broker acts as a middleman that receives messages from publishers and forwards them to subscribers.
Step 2: Eliminate incorrect roles
Generating data, acting only as a publisher, or encrypting messages are not the broker's main roles.
Final Answer:
To receive messages from devices and forward them to subscribers -> Option A
Quick Check:
Broker = message receiver and forwarder [OK]
Hint: Broker forwards messages between devices [OK]
Common Mistakes:
Thinking broker generates data
Confusing broker with publisher device
Assuming broker only encrypts messages
2. Which network port does an MQTT broker usually listen on by default?
easy
A. 8080
B. 22
C. 443
D. 1883
Solution
Step 1: Recall default MQTT port
The MQTT protocol commonly uses port 1883 for unencrypted communication.
Step 2: Identify other ports
Port 8080 is for HTTP alternate, 443 for HTTPS, and 22 for SSH, not MQTT.
Final Answer:
1883 -> Option D
Quick Check:
MQTT default port = 1883 [OK]
Hint: MQTT default port is 1883 [OK]
Common Mistakes:
Choosing common web ports like 80 or 443
Confusing MQTT port with SSH port 22
Assuming MQTT uses random ports
3. Given this MQTT broker behavior: A device publishes a message to topic home/livingroom/temperature. What does the broker do next?
medium
A. Sends the message to all devices regardless of subscription
B. Sends the message to all devices subscribed to home/livingroom/temperature
C. Deletes the message immediately after receiving
D. Saves the message locally and does not forward it
Solution
Step 1: Understand topic-based forwarding
The MQTT broker forwards messages only to devices subscribed to the specific topic.
Step 2: Eliminate incorrect behaviors
Broker does not just save or delete messages without forwarding, nor does it broadcast to all devices.
Final Answer:
Sends the message to all devices subscribed to home/livingroom/temperature -> Option B
Quick Check:
Broker forwards by topic subscription [OK]
Hint: Broker forwards messages only to subscribed topics [OK]
Common Mistakes:
Assuming broker broadcasts to all devices
Thinking broker stores messages without forwarding
Believing broker deletes messages immediately
4. You configured an MQTT broker but devices cannot connect. Which of these is a likely cause?
medium
A. Broker is not listening on the correct port
B. Devices are publishing to the correct topic
C. Broker is forwarding messages correctly
D. Devices have correct subscription filters
Solution
Step 1: Check broker listening port
If the broker is not listening on the expected port (usually 1883), devices cannot connect.
Step 2: Understand other options
Publishing topics, forwarding messages, and subscription filters affect message flow but not initial connection.
Final Answer:
Broker is not listening on the correct port -> Option A
Quick Check:
Connection issue = wrong broker port [OK]
Hint: Check broker port if devices can't connect [OK]
Common Mistakes:
Blaming topic or subscription before connection
Ignoring broker network settings
Assuming forwarding affects connection
5. You want to ensure that an MQTT broker only forwards messages to devices subscribed to topics starting with office/. Which broker feature helps achieve this?
hard
A. Device authentication
B. Message encryption
C. Topic filtering with wildcards
D. Retained messages
Solution
Step 1: Understand topic filtering
MQTT brokers use topic filters with wildcards to forward messages only to matching subscriptions.
Step 2: Exclude unrelated features
Encryption secures data, authentication controls access, and retained messages store last message; none filter topics.
Final Answer:
Topic filtering with wildcards -> Option C
Quick Check:
Topic filtering controls message forwarding [OK]
Hint: Use topic filters with wildcards to control forwarding [OK]
Common Mistakes:
Confusing encryption with filtering
Thinking authentication filters topics
Assuming retained messages control forwarding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.