Publisher or Subscriber, Which is first?

Which among the following nodes should be started first? Publisher or Subscriber.

What happens if start subscriber first then publisher?

Note: Subscriber is working as intended (printing the received message) when started after publisher. But it misses receiving initial messages.
And, When the subscriber is started first, It doesn’t work as intended.

Hi @dhanaprakaashg,
Check out this discussion from ROS Answers subscribe before publish

While the above discussion link explains why the initial messages are lost, it doesn’t have any useful solutions on how to tackle the problem. A few interesting ways in which you can maybe resolve this

  1. get_num_connections()
    get_num_connections() method, which lets you "get the number of connections to other ROS nodes for this topic. For a Publisher, this corresponds to the number of nodes subscribing. For a Subscriber, the number of publishers."
    Documentation for get_num_connections

  2. Latching Messages
    When a connection is latched, the last message published is saved and automatically sent to any future subscribers that connect. This is useful for slow-changing or almost static data like a map.
    Documentation for Latching

  3. get_published_topics()
    You can use this to get a list of topics that the master is reporting as being published, you can use this in your subscriber to identify if a particular topic of your interest is being published or not before subscribing to it, further you can implement method 1 (get_num_connections()) in the publisher to start publishing only after the subscriber has been registered with the ros master.
    Documentation for get_published_topics

Note:: If your goal is to guarantee that a single message has been sent, you should be using services.

I don’t have any standalone examples to put up as of now, feel free to post here in case you are stuck implementing any of the above ideas.

Happy Learning…!!

2 Likes

Subscriber is publishing messages late because of the time lag. What i mean is, first you start publisher. Then there is a time gap of some seconds until you open a new terminal window and start a subscriber. Then subscriber is activated hence it catches the values right form the point when it is started.

Another important point about starting subscriber first , i think it will throw an error because it won’t receive any message from publisher so ROS master will not be able to make the connection.

I have tried to answer this as far as my knowledge goes.

Kindly correct me if i am wrong in concepts.

Thanks ! good luck !

Hi @ashutoshpurohit53

Subscriber is publishing messages late because of the time lag. What i mean is, first you start publisher. Then there is a time gap of some seconds until you open a new terminal window and start a subscriber. Then subscriber is activated hence it catches the values right form the point when it is started.

I believe you meant “Subscriber is not receiving messages late because of the time lag”.

Another important point about starting subscriber first , i think it will throw an error because it won’t receive any message from publisher so ROS master will not be able to make the connection.

Yes, this is kinda correct. But to be precise you only get a warning and not an error so the ros node will not shutdown.
The warning looks kinda like this: WARNING: topic [/hello] does not appear to be published yet

2 Likes