📖 Scenario: Imagine you are building a simple messaging system where devices can send messages to topics and other devices can receive messages from those topics. This is called a publish-subscribe system, commonly used in IoT (Internet of Things) to allow many devices to communicate efficiently.
🎯 Goal: You will create a basic publish-subscribe system in Python that stores messages by topic, allows devices to publish messages to topics, and allows devices to subscribe and receive messages from those topics.
📋 What You'll Learn
Create a dictionary to hold topics and their messages
Add a list of subscribers for each topic
Write a function to publish messages to a topic
Write a function to subscribe a device to a topic
Write a function to get all messages for a subscriber from their subscribed topics
Print the messages received by a subscriber
💡 Why This Matters
🌍 Real World
Publish-subscribe architecture is widely used in IoT devices to allow sensors and controllers to communicate without knowing about each other directly. This helps scale systems and manage many devices easily.
💼 Career
Understanding publish-subscribe systems is important for roles in IoT development, cloud messaging services, and event-driven architectures common in modern software and DevOps environments.
Progress0 / 4 steps
1
Create the topics dictionary
Create a dictionary called topics with these exact keys: 'sports', 'news', and 'weather'. Each key should have an empty list as its value to hold messages.
IOT Protocols
Hint
Think of topics as boxes labeled 'sports', 'news', and 'weather'. Each box will hold messages.
2
Create the subscribers dictionary
Create a dictionary called subscribers with the same keys as topics: 'sports', 'news', and 'weather'. Each key should have an empty list as its value to hold subscriber names.
IOT Protocols
Hint
Subscribers are like friends who want to get messages from each topic box.
3
Write publish and subscribe functions
Write two functions: publish(topic, message) that adds message to topics[topic], and subscribe(device, topic) that adds device to subscribers[topic].
IOT Protocols
Hint
Publishing means putting a message in the topic box. Subscribing means adding a device to the list of friends who get messages from that box.
4
Get messages for a subscriber and print them
Write a function get_messages(device) that returns a list of all messages from topics where device is subscribed. Then, subscribe 'device1' to 'sports' and 'news', publish messages 'Game tonight' to 'sports' and 'Elections coming' to 'news', and print the messages received by 'device1' using print(get_messages('device1')).
IOT Protocols
Hint
Think of get_messages as checking all the boxes where your device is a friend and collecting all messages inside.
Practice
(1/5)
1. What is the main role of a broker in a publish-subscribe architecture?
easy
A. To receive messages from publishers and forward them to subscribers
B. To directly connect publishers and subscribers
C. To store all messages permanently
D. To encrypt messages between devices
Solution
Step 1: Understand the broker's function
The broker acts as a middleman that receives messages from publishers.
Step 2: Identify message forwarding role
The broker forwards these messages to subscribers interested in specific topics.
Final Answer:
To receive messages from publishers and forward them to subscribers -> Option A
Quick Check:
Broker forwards messages [OK]
Hint: Broker forwards messages, it doesn't connect devices directly [OK]
Common Mistakes:
Thinking broker connects devices directly
Assuming broker stores messages permanently
Confusing broker with encryption service
2. Which of the following is the correct way to describe a topic in publish-subscribe?
easy
A. A label used to organize messages for subscribers
B. A direct connection between two devices
C. A security protocol for message encryption
D. A device that publishes messages
Solution
Step 1: Define what a topic is
A topic is a label or category that organizes messages so subscribers receive only relevant ones.
Step 2: Eliminate incorrect options
Topics are not connections, security protocols, or devices.
Final Answer:
A label used to organize messages for subscribers -> Option A
Quick Check:
Topic organizes messages [OK]
Hint: Topics organize messages, not devices or connections [OK]
Common Mistakes:
Confusing topics with device connections
Thinking topics are security features
Mixing up topics with publishers
3. Given this scenario: A device publishes messages to topic home/temperature. Which device will receive the messages?
medium
A. All devices on the network
B. Any device subscribed to home/humidity
C. Any device subscribed to home/temperature
D. Only the publishing device
Solution
Step 1: Identify the topic published to
The device publishes to home/temperature.
Step 2: Determine which subscribers get the message
Only devices subscribed to home/temperature receive the messages.
Final Answer:
Any device subscribed to home/temperature -> Option C
Quick Check:
Subscribers to topic get messages [OK]
Hint: Subscribers to the exact topic get messages [OK]
Common Mistakes:
Assuming all devices get messages
Thinking publishing device receives its own messages
Confusing similar topic names
4. A subscriber is not receiving messages from the broker. Which of these is a likely cause?
medium
A. Publisher is sending messages to the subscriber directly
B. Subscriber is publishing messages instead of subscribing
C. Broker is encrypting messages incorrectly
D. Subscriber is not subscribed to the correct topic
Solution
Step 1: Check subscription correctness
If the subscriber is not subscribed to the right topic, it won't get messages.
Step 2: Rule out other causes
Publishers don't send messages directly; encryption issues or wrong roles are less common causes here.
Final Answer:
Subscriber is not subscribed to the correct topic -> Option D
Quick Check:
Correct topic subscription needed [OK]
Hint: Check subscriber's topic subscription first [OK]
Common Mistakes:
Thinking publisher sends messages directly
Blaming encryption without evidence
Confusing subscriber and publisher roles
5. In a large IoT system using publish-subscribe, how can you ensure that only devices in the 'factory/floor1' area receive temperature alerts without affecting others?
hard
A. Publish all alerts to factory/temperature and let devices filter messages locally
B. Publish temperature alerts to topic factory/floor1/temperature and have devices subscribe to it
C. Send alerts directly to each device's IP address
D. Use a single topic temperature for all devices
Solution
Step 1: Use topic hierarchy for targeted messaging
Publishing to factory/floor1/temperature targets only devices subscribed to that specific area.
Step 2: Avoid broad topics or direct device addressing
Broad topics or direct IP messaging reduce scalability and flexibility.
Final Answer:
Publish temperature alerts to topic factory/floor1/temperature and have devices subscribe to it -> Option B
Quick Check:
Use specific topics for targeted delivery [OK]
Hint: Use detailed topic paths for precise message delivery [OK]
Common Mistakes:
Using broad topics causing unwanted message delivery
Trying direct device IP messaging in pub-sub
Ignoring topic hierarchy benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.