0
$\begingroup$

Rosanswers logo

I would like for one node to wait for a subscriber to a topic to exist before publishing to it. Is there a straightforward way to do that in rclcpp?

I'm developing on rolling if that matters.


Originally posted by tyler-picknik on ROS Answers with karma: 241 on 2021-11-24

Post score: 0


Original comments

Comment by aprotyas on 2021-11-25:
I think @Geoff's answer suffices for your purpose, but FYI there are wait_for_[publishers/subscribers] methods in the RCL layer (refer to https://github.com/ros2/rcl/pull/907/). It would be nice if there were client library (rclcpp/rclpy) wrappers because that fits your use case exactly!

Comment by Mike Scheutzow on 2021-11-25:
Some advice: in a robust distributed design, there are almost no good reasons a publishing node needs to know the number of subscribers. Remember you don't control who connects, and message delivery is unreliable. How does your node use this information?

Comment by tyler-picknik on 2022-03-02:
The original reason for this was to recreate the behavior of latched topics. I've found another way to do that though.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The easiest method I can think of off the top of my head is to check the publisher's get_subscription_count() method before publishing, and skip publishing (or even preparing to publish) if the count is zero. You might also need to check get_intra_process_subscription_count() as well.


Originally posted by Geoff with karma: 4203 on 2021-11-24

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.