Bird
Raised Fist0
ROSframework~10 mins

Publish-subscribe communication model in ROS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Publish-subscribe communication model
Publisher Node
↓
Subscriber Node(s)
↓
Process Message
The publisher sends messages to the ROS Master, which then forwards them to all subscriber nodes that have registered interest.
Execution Sample
ROS
import rospy
from std_msgs.msg import String

def callback(msg):
    rospy.loginfo(f"Received: {msg.data}")

rospy.init_node('listener')
rospy.Subscriber('chatter', String, callback)
rospy.spin()
This subscriber node listens to the 'chatter' topic and prints received messages.
Execution Table
StepActionPublisher StateROS Master StateSubscriber StateMessage Flow
1Publisher node starts and initializesReady to publishIdleIdleNo messages
2Subscriber node starts and subscribes to 'chatter'Ready to publishRegisters subscriber for 'chatter'Subscribed to 'chatter'No messages
3Publisher publishes message 'Hello'Message 'Hello' sentReceives message 'Hello' on 'chatter'Waiting for messageMessage queued for subscribers
4ROS Master forwards message to subscriberMessage sentForwards 'Hello' to subscriberReceives message 'Hello'Message delivered
5Subscriber callback processes messageIdleIdleProcesses and logs 'Received: Hello'Message processed
6No new messagesIdleIdleIdleNo messages
ExitNo more messages to publishPublisher stops or waitsNo messages to forwardSubscriber waitsExecution stops
💡 Publisher stops sending messages; system waits idle.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Publisher StateNot startedReady to publishMessage 'Hello' sentMessage sentIdleIdle
ROS Master StateIdleRegisters subscriberReceives messageForwards messageIdleIdle
Subscriber StateIdleSubscribedWaiting for messageReceives messageProcesses messageIdle
Message FlowNo messagesNo messagesMessage queuedMessage deliveredMessage processedNo messages
Key Moments - 3 Insights
Why does the subscriber receive messages only after subscribing?
Because in the execution_table at Step 2, the subscriber registers interest with the ROS Master. Before that, the ROS Master does not forward messages to it.
What role does the ROS Master play in message delivery?
As shown in Steps 3 and 4, the ROS Master acts as a broker that receives messages from publishers and forwards them to all subscribers registered for that topic.
Why does the subscriber process messages asynchronously?
In Step 5, the subscriber processes messages via a callback function triggered when a message arrives, allowing it to handle messages as they come without blocking.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the ROS Master state at Step 3?
ARegisters subscriber
BIdle
CReceives message 'Hello' on 'chatter'
DForwards 'Hello' to subscriber
💡 Hint
Check the 'ROS Master State' column in execution_table row for Step 3.
At which step does the subscriber first receive the message?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Subscriber State' and 'Message Flow' columns in execution_table.
If the subscriber never subscribes, what happens to the messages?
AMessages are not forwarded to subscriber
BROS Master forwards messages anyway
CMessages are queued for subscriber
DPublisher stops publishing
💡 Hint
Refer to Step 2 and Step 4 in execution_table about subscriber registration.
Concept Snapshot
Publish-subscribe model in ROS:
- Publisher sends messages to a topic via ROS Master
- Subscribers register interest to topics with ROS Master
- ROS Master forwards messages from publishers to all subscribers
- Subscribers receive messages asynchronously via callbacks
- Decouples sender and receiver for flexible communication
Full Transcript
In the ROS publish-subscribe communication model, a publisher node sends messages to a topic registered with the ROS Master. Subscriber nodes register their interest in topics with the ROS Master. When a publisher sends a message, the ROS Master receives it and forwards it to all subscribers of that topic. Subscribers process messages asynchronously using callback functions. This model allows nodes to communicate without knowing about each other directly, enabling flexible and scalable message passing.

Practice

(1/5)
1. What is the main role of a publisher in the ROS publish-subscribe model?
easy
A. To create new topics dynamically
B. To receive messages from a topic
C. To store messages in a database
D. To send messages to a topic

Solution

  1. Step 1: Understand the publish-subscribe roles

    In ROS, publishers send messages to topics, while subscribers receive messages from topics.
  2. Step 2: Identify the publisher's function

    The publisher's job is to send data to a topic so subscribers can receive it.
  3. Final Answer:

    To send messages to a topic -> Option D
  4. Quick Check:

    Publisher = sends messages [OK]
Hint: Publisher sends, subscriber receives messages [OK]
Common Mistakes:
  • Confusing publisher with subscriber
  • Thinking publisher stores messages
  • Assuming publisher creates topics
2. Which of the following is the correct way to create a ROS publisher in Python?
easy
A. pub = rospy.Publisher('topic_name', callback, queue_size=10)
B. pub = rospy.Subscriber('topic_name', String, callback)
C. pub = rospy.Publisher('topic_name', String, queue_size=10)
D. pub = rospy.Subscriber('topic_name', String)

Solution

  1. Step 1: Recall publisher syntax in ROS Python

    The correct syntax uses rospy.Publisher with topic name, message type, and queue size.
  2. Step 2: Check each option

    pub = rospy.Publisher('topic_name', String, queue_size=10) matches the correct syntax. Using Subscriber instead of Publisher is incorrect. pub = rospy.Publisher('topic_name', callback, queue_size=10) incorrectly uses callback instead of message type.
  3. Final Answer:

    pub = rospy.Publisher('topic_name', String, queue_size=10) -> Option C
  4. Quick Check:

    Publisher syntax = rospy.Publisher(topic, type, queue_size) [OK]
Hint: Publisher uses rospy.Publisher(topic, type, queue_size) [OK]
Common Mistakes:
  • Using Subscriber instead of Publisher
  • Passing callback instead of message type
  • Omitting queue_size parameter
3. Given the following ROS Python code snippet, what will be printed when a message with data 'Hello' is published?
import rospy
from std_msgs.msg import String

def callback(msg):
    print(f'Received: {msg.data}')

rospy.init_node('listener')
sub = rospy.Subscriber('chatter', String, callback)
rospy.spin()
medium
A. Received: Hello
B. Hello
C. Received: msg
D. No output

Solution

  1. Step 1: Understand the callback function

    The callback prints 'Received: ' followed by the message's data field.
  2. Step 2: Analyze the message content

    The message data is 'Hello', so the print statement outputs 'Received: Hello'.
  3. Final Answer:

    Received: Hello -> Option A
  4. Quick Check:

    Callback prints 'Received: ' + msg.data [OK]
Hint: Callback prints 'Received: ' plus message data [OK]
Common Mistakes:
  • Printing message object instead of data
  • Expecting just the message data without prefix
  • Thinking no output occurs without explicit print
4. Identify the error in this ROS Python subscriber code:
import rospy
from std_msgs.msg import String

def callback(msg):
    print(msg.data)

rospy.init_node('listener')
sub = rospy.Subscriber('chatter', String)
rospy.spin()
medium
A. Incorrect message type used
B. Missing callback function in Subscriber
C. Node not initialized
D. No import for rospy

Solution

  1. Step 1: Review Subscriber syntax

    Subscriber requires three arguments: topic name, message type, and callback function.
  2. Step 2: Check the given code

    The Subscriber call misses the callback argument, causing an error.
  3. Final Answer:

    Missing callback function in Subscriber -> Option B
  4. Quick Check:

    Subscriber needs callback argument [OK]
Hint: Subscriber must include callback function [OK]
Common Mistakes:
  • Omitting callback in Subscriber
  • Confusing Publisher and Subscriber arguments
  • Forgetting to initialize node
5. You want to create a ROS system where multiple nodes publish sensor data to the same topic, and one node subscribes to process all data. Which feature of the publish-subscribe model makes this possible?
hard
A. Topics allow multiple publishers and subscribers to communicate independently
B. Each topic can have only one publisher and one subscriber
C. Publishers must know all subscribers to send messages
D. Subscribers can only listen to one topic at a time

Solution

  1. Step 1: Understand topic communication in ROS

    Topics support multiple publishers and subscribers simultaneously, enabling flexible communication.
  2. Step 2: Analyze the scenario

    Multiple nodes publishing to the same topic and one subscriber processing all data is possible because topics allow many publishers and subscribers.
  3. Final Answer:

    Topics allow multiple publishers and subscribers to communicate independently -> Option A
  4. Quick Check:

    Topics support many-to-many communication [OK]
Hint: Topics support many publishers and subscribers [OK]
Common Mistakes:
  • Thinking topics allow only one publisher or subscriber
  • Believing publishers must know subscribers
  • Assuming subscribers can listen to only one topic
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