ROS 2 LearningMay 28, 20258 min read

ROS 2 Topics, Services, and Actions Explained

ROS 2 Topics are essential for robotics communication. Master topics, services, and actions with this complete beginner guide and start your ROS 2 journey today.

ROS 2 Topics, Services, and Actions Explained

Share :

Quick answer

ROS 2 Topics are essential for robotics communication. Master topics, services, and actions with this complete beginner guide and start your ROS 2 journey today.

Quick Answer

ROS 2 Topics are essential for robotics communication. Master topics, services, and actions with this complete beginner guide and start your ROS 2 journey today.

Who This Is For

  • ROS 2 Learner
  • Robotics Student
  • Software Developer

What You Will Learn

  • What ROS 2 means in practical robotics.
  • How this topic connects to real robot projects.
  • What to learn or build next after this article.

Introduction

ROS 2 Topics are a key part of the Robot Operating System 2 (ROS 2), a robust and flexible framework for building modern robotic applications. Designed to support real-time systems and distributed computing, ROS 2 is ideal whether you're working on a mobile robot, robotic manipulator, or sensor network. Understanding the core communication mechanisms, Topics, Services, and Actions, is fundamental to architecting efficient robotic systems. Each of these communication methods addresses a specific type of interaction between nodes in a ROS 2 system. Misusing them can lead to inefficiencies, bugs, or sluggish robot performance. In this beginner-friendly guide, we will explore what Topics, Services, and Actions are, their distinct purposes, when to use them, and how to implement them in practice. This blog is ideal for those starting their journey into ROS 2 development.

Understanding ROS 2 Communication Mechanisms

At the heart of any ROS 2-based system are nodes-independent processes that communicate over well-defined interfaces. ROS 2 Topics, along with services and actions, provide the three primary mechanisms for node-to-node communication.

  • Topics for streaming data
  • Services for synchronous requests
  • Actions for long-duration goals with feedback Let's explore each of them in detail.

Topics

ROS 2 Topics are the backbone of most real-time ROS 2 communication. They follow a publisher-subscriber model, where one or more nodes publish data, and other nodes subscribe to receive that data. Topics are ideal for high-frequency, asynchronous communication.

Key Characteristics of Topics:

  • Unidirectional : Data flows from publisher to subscriber
  • Asynchronous : No waiting or blocking; publishers send messages regardless of subscriber state
  • Many-to-Many Relationships : Multiple publishers and subscribers can coexist
  • Real-Time Streaming : Ideal for continuous data like sensor streams

Real-World Example:

A mobile robot with a LiDAR sensor continuously publishes distance data to a /scan topic. Several nodes-like localization, obstacle avoidance, and SLAM-subscribe to this topic to process data in real-time.

Common Use Cases:

  • IMU or GPS data streaming
  • Camera or LiDAR feeds
  • Status updates from hardware
  • Motor encoder feedback

Services

Services provide a synchronous, two-way communication model. A node (client) sends a request to a service (server), waits for it to be processed, and receives a response. It's a blocking interaction, suited for operations requiring an immediate answer.

Key Characteristics of Services:

  • Bidirectional : Request from client, response from server
  • Synchronous : Client waits for a response before proceeding
  • One-to-One Communication : Request targets a single server
  • Atomic Tasks : Ideal for quick, clearly defined operations

Real-World Example:

A monitoring node sends a request to a /get_battery_level service on the robot. The power management server reads and returns the current voltage level.

Common Use Cases:

  • Set or retrieve configuration parameters
  • Request robot state or location
  • Perform calibration routines
  • Reset or reboot subsystems

Actions

Actions are suited for long-running operations that need progress tracking and optional cancellation. They offer a goal-feedback-result pattern and blend the asynchronous nature of Topics with the structure of Services.

Key Characteristics of Actions:

  • Goal-Oriented : Client sends a goal to the server
  • Feedback Support : Periodic progress updates available
  • Cancelable : Tasks can be stopped during execution
  • Asynchronous Execution : Non-blocking operation with monitoring

Real-World Example:

A robotic arm receives a goal to pick and place an object. The action server updates the client on progress stages (e.g., "reaching," "grasping," "placing") and can cancel the task if the object is moved or an error occurs.

Common Use Cases:

  • Navigate to a target location
  • Execute arm manipulation tasks
  • Start and monitor charging sequence
  • Run multi-step autonomous routines

Comparison: Topics vs Services vs Actions

  • Feature
  • Topics
  • Services
  • Actions Communication TypeUnidirectional (Pub/Sub)Bidirectional (Req/Resp)Bidirectional with FeedbackSynchronizationAsynchronousSynchronousAsynchronousUse CaseContinuous Data StreamsQuick RequestsLong-running TasksFeedback SupportNoNoYesCancellationNoNoYesComplexityLowMediumHigh (requires .action file)

Implementing ROS 2 Topics, Services, and Actions in Python

Let's look at simple examples using rclpy to illustrate how to use each mechanism.

Topics: Publishing and Subscribing

Publisher Node

python import rclpy from rclpy.node import Node from std_msgs.msg import String class PublisherNode(Node): def init(self): super().init('publisher_node') self.publisher_ = self.create_publisher(String, 'topic', 10) self.timer = self.create_timer(0.5, self.timer_callback) def timer_callback(self): msg = String() msg.data = 'Hello, ROS 2!' self.publisher_.publish(msg) self.get_logger().info(f'Publishing: "{msg.data}"')

Subscriber Node

python import rclpy from rclpy.node import Node from std_msgs.msg import String class SubscriberNode(Node): def init(self): super().init('subscriber_node') self.subscription = self.create_subscription(String, 'topic', self.listener_callback, 10) def listener_callback(self, msg): self.get_logger().info(f'Received: "{msg.data}"')

Services: Client and Server

Service Server

python import rclpy from rclpy.node import Node from example_interfaces.srv import AddTwoInts class ServiceServerNode(Node): def init(self): super().init('service_server_node') self.srv = self.create_service(AddTwoInts, 'add_two_ints', self.add_callback) def add_callback(self, request, response): response.sum = request.a + request.b self.get_logger().info(f'Request: {request.a} + {request.b} = {response.sum}') return response

Service Client

python import rclpy from rclpy.node import Node from example_interfaces.srv import AddTwoInts class ServiceClientNode(Node): def init(self): super().init('service_client_node') self.client = self.create_client(AddTwoInts, 'add_two_ints') while not self.client.wait_for_service(timeout_sec=1.0): self.get_logger().info('Waiting for service...') self.request = AddTwoInts.Request() self.request.a = 2 self.request.b = 3 self.future = self.client.call_async(self.request)

Actions: Setting Goals and Receiving Feedback

Implementing actions requires defining a .action file, generating interfaces, and writing both server and client nodes. It's more involved than Topics or Services. For a full guide, refer to the official ROS 2 Actions tutorial.

Practical Applications in Robotics

Communication Type****Use Case Examples TopicsStream IMU data, camera feed, GPS updates, joint statesServicesSet robot modes, calibrate sensors, request diagnosticsActionsNavigate to waypoint, execute arm task, charging routine By aligning your communication mechanism with application needs, your system becomes more modular, responsive, and scalable.

Frequently Asked Questions (FAQs)

1. Can I use Actions instead of Services for everything?

Technically yes, but it's not ideal. Actions introduce additional complexity and overhead. Use Services for quick, atomic operations and Actions only when progress monitoring or cancellation is needed.

2. What happens if a Topic has no subscribers?

The publisher still works and sends messages, but no node will receive them. This doesn't cause errors unless your logic depends on acknowledgment or feedback.

3. Can one node both publish and subscribe to the same Topic?

Yes. This is common in control loops or monitoring systems where the node needs to track what it's publishing.

4. Are Services and Actions reliable for critical safety operations?

Services are reliable but blocking; they depend on the server's availability. Actions are good for controlled tasks with feedback but add complexity. For safety-critical functions, consider implementing watchdogs or fallback systems.

5. How do I choose between ROS 2 Topics, Services, and Actions?

Use this rule of thumb:

  • Use Topics for streaming sensor or state data
  • Use Services for requests that expect quick replies
  • Use Actions for tasks that require monitoring or cancellation

Additional Resources

Continue Learning ROS 2

To deepen your understanding of ROS 2 Topics and apply these concepts in real robotics projects, explore the Robotisim ROS 2 Tutorial Series. Learn how to build a robot using Raspberry Pi, microcontrollers, and real-world ROS 2 patterns from scratch.

Practical Example

A practical way to use this article is to connect the concept to a small robot workflow: identify the input, the processing step, and the output you expect from the robot. If the article involves ROS 2, test the idea in a small workspace or simulation before applying it to a larger robot project.

Common Mistakes

  • Trying to memorize the term without connecting it to a robot behavior.
  • Skipping the prerequisite concepts that make the workflow easier to debug.
  • Copying commands or code without checking what each node, topic, file, or parameter is responsible for.
  • Treating one tutorial as a complete roadmap instead of linking it to the next concept.

How This Connects to Other Topics

  • 3D Printing Robotics Hardware for an Autonomous Robot Build
  • How to Collect Raw Sensor Data for Robotics with ROS 2
  • How to Add Custom Libraries to a ROS 2 Python Package
  • How to Start Developing in ROS 2: A Beginner-Friendly Guide
  • How to Finish Your First ROS Robotics Project

Learn Next

  • 3D Printing Robotics Hardware for an Autonomous Robot Build
  • How to Collect Raw Sensor Data for Robotics with ROS 2
  • How to Add Custom Libraries to a ROS 2 Python Package
  • How to Start Developing in ROS 2: A Beginner-Friendly Guide
  • How to Finish Your First ROS Robotics Project
  • ROS 2 Foundation Path

FAQ

Is ROS 2 Topics, Services, and Actions Explained suitable for beginners?

Yes. The article is written to make the concept easier to understand, while still connecting it to practical robotics work.

What should I learn before this topic?

Start with the prerequisite ideas listed in the article, then connect them to a small project or simulation so the concept becomes concrete.

How does this topic connect to real robots?

It helps you understand how software, sensors, control, simulation, or career decisions show up in practical robot development.

What should I do after reading this article?

Pick one related concept from the Learn Next section and build a small example that uses it.

Can I learn this through Robotisim?

Yes. Robotisim connects these concepts to structured learning paths and project-based robotics practice.

Final Summary

ROS 2 Topics, Services, and Actions Explained is part of the broader ROS 2 Learning learning path. The key is to understand the concept, connect it to a real robot workflow, and then practice it through a focused project instead of learning it in isolation.

Connected learning path

This article supports ROS 2 Foundation Path, especially ROS 2.

Learn with Robotisim

Start learning ROS 2 step by step inside Robotisim.

Explore the academy

Learn next

3D Printing Robotics Hardware for an Autonomous Robot Build
Jun 03, 2025|7 min read

3D Printing Robotics Hardware for an Autonomous Robot Build

Discover how to 3D print robotics hardware for building autonomous robots. Learn key steps from assembly to motion control with ROS 2

Read more
How to Collect Raw Sensor Data for Robotics with ROS 2
Jun 05, 2025|7 min read

How to Collect Raw Sensor Data for Robotics with ROS 2

Learn to integrate sensors with ROS 2 and collect raw sensor data for robotics. Guide on real time data processing and sensor fusion for autonomous robots.

Read more
How to Add Custom Libraries to a ROS 2 Python Package
Jun 02, 2024|6 min read

How to Add Custom Libraries to a ROS 2 Python Package

Learn to create custom libraries for ROS 2 Python packages. Enhance your robotics projects with reusable code and improve your development workflow!

Read more
How to Start Developing in ROS 2: A Beginner-Friendly Guide
Jun 02, 2024|8 min read

How to Start Developing in ROS 2: A Beginner-Friendly Guide

Begin your ROS 2 development journey with our guide. Learn key concepts and setup for creating effective robotics applications in no time!

Read more