Wait for subscriber to exist?
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.
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!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?
The original reason for this was to recreate the behavior of latched topics. I've found another way to do that though.