Skip to content

ROS 2 Topics and Pub-Sub Patterns

Source: ros2-copilot-skills topic pub-sub skill

Why This Matters

Topics are the backbone of most ROS 2 systems, but a topic graph only becomes useful when message rates, QoS, timestamps, and callback behavior are designed intentionally. Many robots appear flaky because their topic flows were treated as trivial plumbing.

Distilled Takeaways

  • Use topics for continuous streams such as commands, telemetry, images, scans, and derived state.
  • Prefer timer-based publishing over ad hoc constructor-time publication so the system has a stable cadence and subscribers have time to connect.
  • Stamp messages with the node clock, not wall-clock helpers from the standard library, so simulation and bag playback keep working.
  • Large messages like images and point clouds need explicit bandwidth discipline.
  • Topic health should be observable from the CLI with ros2 topic info, hz, bw, and targeted echo checks.

Practical Guidance

  • Store subscription handles as members or the subscription silently disappears.
  • Keep topic names consistent with their scope: absolute names only when you mean to bypass namespace structure.
  • Treat message type selection as part of API design. A bad message choice leaks pain into downstream nodes.
  • When multiple topic streams must be correlated, do not fake synchronization with “latest message” globals unless the timing tolerance is truly loose.

Corroborating References

When to Read the Original Source

Go to the original skill when you want concrete Python and C++ publisher/subscriber patterns, examples for latched topics, and the warning list for timestamps, queue depth, and large-message transport.