What is a ROS Topic?

You are starting with ROS and are wondering: what is a ROS topic ?

To explain what is a ROS topic, I will use here a different approach from what you can already find on the Internet. Instead of giving you a bunch of code to write and run, I will use a real life analogy so you can easily understand the concept behind ROS topics.

This post is a part of the What is ROS? series.

>> As an additional resource to this article I’ve also made a video explanation, with a slightly different approach. To get the most benefits, read the article and also watch the video.

After watching the video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials!

You are learning ROS?

Check out ROS For Beginners and learn ROS step by step.

Our first publisher

I will use an analogy with a radio transmitter and receiver. As this is a simplified analogy, not everything I will say about radio will be correct, but the point here is to make you understand ROS topics.

Let’s suppose we have one radio transmitter.

This radio transmitter will send some data on a given frequency. To make things easier for people to remember, the frequency will be represented by a number attached to the name of the radio.

In this example, we have the 98.7 frequency.

You can think of 98.7 also as a name, so you know that if you want to receive music from the radio station, you need to connect your device to “98.7”.

You can see the green box here, 98.7, as a ROS topic, and the radio transmitter is a publisher of this topic. So for this case, a data stream is sent over the 98.7 topic.

What is a ROS topic? 1/6

Time to add some subscribers

Now, maybe you want to listen to the radio station from your phone. You will ask your phone to listen to 98.7.

In this case, the phone is a subscriber of the topic.

To play the music on your phone, from the radio transmitter, you also need to send and receive the same kind of message.

Here, if the radio transmitter is sending AM signal over the topic, and if, on your phone, you are trying to decode FM signal, then it will not work! The phone will have the right frequency but won’t manage to decode the signal.

That’s why both the publisher and subscriber must send messages with the same data structure.

So we have our radio transmitter which is a publisher on the 98.7 topic, using an AM signal.

The phone will subscribe to the 98.7 topic, and will also decode an AM signal.

The communication is now complete!

What is a ROS topic? 2/6

Multiple subscribers for one topic

Having one subscriber is nice, but what if you also want to listen to the radio station from another device, or from your car ?

With radio as you know it, you just need to connect all your device and car to the 98.7 radio. Also, both your device and car need to be able to decode AM signal.

What is a ROS topic? 3/6

With ROS, you can have multiple subscribers for the same topic. You can see here an example with a topic and 3 subscribers.

And as you can guess, a subscriber is not aware of the other subscribers, and is not aware of who is publishing the data. It only knows it is receiving data from the 98.7 topic.

Thus, we can say that subscribers are anonymous.

Multiple publishers for one topic

You can have many subscribers for one topic, but on the other side you can also have many publishers for the same topic.

Imagine another radio transmitter which is also publishing an AM signal to 98.7. It can be the same radio station, it can also be another radio station.

Sometimes, when you are driving, you arrive in a zone where 2 radio stations are publishing on the same frequency.

In this case, you have 2 publishers on the 98.7 topic. All the subscribers will receive the messages from both publishers.

What is a ROS topic? 4/6

On this picture, all blue boxes are ROS nodes. If you are not familiar with ROS nodes yet, check out this guide first.

You have the radio transmitter node number 1, the radio transmitter node number 2, and then you have a node for the smartphone, the radio player, and the car.

Some nodes contain a publisher to the 98.7 topic, some nodes contain a subscriber for this topic.

As you notice, all publishers and subscribers are sending and receiving the same kind of data.

We’ve just seen before that subscribers are anonymous. Well, that also works for ROS publishers. A publisher is not aware of the other publishers on the topic, and is not aware of who is receiving the data. It only publishes data to the topic, and that’s it. Publishers on a ROS topic are anonymous.

So, each node which is publishing or subscribing to the topic is totally independent. Any combination is possible.

For example, you could have 3 subscribers on the topic and no publisher. In this case, well, it’s still working, but the subscribers will just receive no data.

If you have 2 publishers on the topic, and no subscriber, the data is just sent and no one receives it.

Another combination: you have only one radio transmitter which is publishing on the 98.7 topic, and only the car is subscribing to the topic. I’ll stop there, I guess you see the point.

Multiple publishers/subscribers inside one node

For now you saw that you can publish data on a topic, and subscribe to it to receive data.

A ROS node is not limited to that. In fact, a node can publish and subscribe on many different topics.

What is a ROS topic? 5/6

Let’s say that the radio transmitter node number 2 is publishing AM signal on the 98.7 topic, and FM signal on the 101.3 topic. The driver of the car just chose to listen to the radio 2, so the car is now subscribing to the 101.3 topic, and decoding FM signal (of course, the car needs to be able to decode both kind of signals and be aware of the signal associated with the radio name).

A node can contain multiple publishers, but also subscribers.

Now, imagine that the car, while listening to the radio, is publishing its coordinates to a car_location topic.

What is a ROS topic? 6/6

The car node has now one subscriber on the 98.7 topic, and one publisher on the car_location topic. The computer node is subscribing to the car location topic, and for the communication to be successful, both nodes are sending and receiving the same kind of message.

Well, that’s it for the analogy! You should now have a better comprehension of what is a ROS topic and when it is useful.

Wrapping things up – What is a ROS topic ?

According to ROS wiki, a topic is a named bus over which nodes exchange messages.

Note that for the real world analogy I used numbers with dots as topic name. This is not valid, a topic name must start with a letter, followed by letters, numbers, underscores, tildes, and slashes. For example, you could a topic named “/radio_98_7”.

Technically speaking, the messages are sent over TCP/IP. The ROS libraries that you will use on your code, will provide you with enough abstraction so you don’t have to deal with the TCP/IP layer.

Now that you have gotten the big picture with the real world analogy, here are some conclusion points so you can have a quick and actionable summary:

  • When to use a topic, is often when you need to send a data stream. The data stream is unidirectional. Some nodes can publish on the topic, and some nodes can subscribe to the topic. There is no response from a subscriber to a publisher, the data is only going one way.
  • Publishers and subscribers are anonymous. A publishers only knows it is publishing to a topic, and a subscriber only knows it is subscribing to a topic. Nothing else.
  • A topic has a message type. All publishers and subscribers on this topic must use the message type associated with the topic.
  • As you already know, you can write a node in multiple languages, using for example the roscpp library for C++, and rospy library for Python. Well, those libraries also include the Topic functionality. So, you can create a publisher or subscriber in any ROS supported language you want, directly inside ROS nodes.
  • When a node wants to publish something, it will inform the ROS master. When another node wants to subscribe to a topic, it will ask the ROS master from where it can get the data. You can see the ROS master as a DNS server for nodes to find where to communicate.
  • Finally, a node can contain many publishers and subscribers for many different topics.

Did you find this tutorial useful?

Do you want to learn how to program with ROS?

If yes, this course is for you:

ROS For Beginners - A Step By Step Course

>> ROS For Beginners - A Step By Step Course <<