0
0
IOT Protocolsdevops~30 mins

Publish-subscribe architecture in IOT Protocols - Mini Project: Build & Apply

Choose your learning style9 modes available
Build a Simple Publish-Subscribe System
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Think of get_messages as checking all the boxes where your device is a friend and collecting all messages inside.