Before going to the definition of a topic, I will give you a real life analogy, so it will be much
easier for you to understand.
I will make an analogy with a radio transmitter and receiver as this is a simplified example.
Not everything I will say about radio will be correct, but the point here is to make you understand
Ross two topics.
Let's suppose we have one radio transmitter.
This radio transmitter will send some data on a given frequency and for people to easily remember,
this is represented by a number.
For example here 98.7 we 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.
So the box with 98.7 has a Ross topic.
And the radio transmitter is a publisher on that topic.
So for this case a data stream is sent over the 98.7 topic.
Now, maybe you want to listen to the radio station from your phone.
What you will do is to ask your phone to listen to 98.7.
In this case, the phone is a subscriber on the topic.
To play the music on your phone from the radio transmitter, you also need to send and receive the same
kind of signal here.
If the radio transmitter is sending an FM signal over the topic, and if on your phone you are trying
to decode an Am signal, then it will not work.
That's why you need to make sure that both the publisher and subscriber are sending messages with the
same data structure.
So to recap here, you can see the radio transmitter as a publisher, which will publish on the 98.7
topic with an FM signal.
The phone will subscribe from the 98.7 topic and will also decode FM signal.
Okay, so both sides need to agree on using the same name and the same data type.
Now to continue with this example, maybe you also want to listen to the radio station from another
device or from your car.
Then, as you know, you just need to listen to 98.7 and also both your device and your car should decode
FM signal.
You have now an example with one publisher and three subscribers, so we can have many subscribers on
the topic.
But on the other side, we can also have many publishers for one topic.
Imagine another radio transmitter, which is also publishing an FM signal on 98.7.
It can be the same radio station.
It can also be another one.
Sometimes when you are driving, you can arrive in a zone where two radio stations are broadcasting
on the same frequency, and in this case, here you have two publishers on the 98.7 topic.
All the subscribers will receive the messages from both publishers.
Now you can see all the blue boxes.
Here are nodes.
You have the radio transmitter.
Node one the radio transmitter.
Node two.
And then you have a node for the smartphone, another one for the radio player and another one for the
car.
Some nodes contain a publisher to the topic and some nodes contain a subscriber.
As you can notice, all publishers and subscribers are sending and receiving the same kind of data.
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 that it is receiving data from the 98.7 topic.
On the publisher side, a publisher is not aware of the other publishers on the topic and is also not
aware of who is receiving the data.
It only publishes the data to the topic and that's it.
So each node which is either publishing or subscribing to the topic, is totally independent and anonymous,
and any combination is possible.
For example, you could have three subscribers on the topic, and no publisher in this case.
Well, it's working, but the subscribers will just receive nothing.
If you have, on the other hand, two publishers on the topic and no subscriber, the data is just sent,
but nobody receives it.
Here's another combination.
You have only one radio transmitter, which is publishing on the 98.7 topic, and only the radio player
is subscribing to the topic.
Well, I guess you see the point.
So for now, we saw that you can publish data on a topic and subscribe to it to receive the data, but
a Rosetta node is not limited to that.
In fact, a node can publish on many different topics.
Let's say that the radio transmitter node two is publishing an FM signal on the 98.7 topic, and an
Am signal on the 101.3 topic.
The driver of the car just chose to listen to the 101.3 frequency, so the car is now subscribing to
that topic and decoding an Am signal, and thus it will receive the data from the radio.
Number two.
So a node can contain multiple publishers but also subscribers and any combination.
Actually imagine that the car, while listening to the radio is publishing its coordinates to a car
location topic.
The car node now has one subscriber to the 101.3 topic, and one publisher on the car location topic.
The computer node on the other side 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.
Now you should have a better comprehension of what is the topic and when it is useful.
Here is a quick recap with more information.
So according to the Ros documentation, a topic is a named bus over which nodes exchange messages.
Note that for the previous real world analogy, we used numbers with dots as topic names.
That was just to provide you with an easy example, but this is not valid.
Okay, a topic name must start with a letter and then it can have letters, numbers, underscores,
tildes and slashes.
And we're going to come back to this later in this course.
And technically speaking the messages are sent with DS which is the Ros2 middleware.
But the Ros2 client libraries that you will use on your code, for example, RCL, CPP, and CLP will
provide you with enough abstraction so you don't have to deal with the DS at all.
Then when to use a topic is often when you need to send a data stream.
The data stream is unidirectional, so 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.
For example, this is quite useful for sensors.
You have a node that reads the data from a sensor and publishes it to a topic, which is then available
for all the other nodes to listen to.
Publishers and subscribers are anonymous.
A publisher only knows it is publishing to a topic, and a subscriber only knows it is subscribing to
a topic and nothing more.
A topic has a name, but also a message type, and all publishers and subscribers on this topic must
use the message type associated with the topic.
Now, as you already know, you can write a node in multiple languages using, for example, the Clcp
library for C plus plus and CLP library for Python, while those libraries also include the topic functionality
so you can create a publisher or subscriber in any Rosetta supported language directly inside your nodes.
And finally, a node can contain many publishers and subscribers for many different topics.
To conclude here, you can see that you can easily separate your code into independent nodes, and then
you can make your nodes communicate with data streams sent over topics.