Ask Your Question
3

How to get the number of subscribers on a topic?

asked 2014-06-10 14:53:39 -0500

ricardoej gravatar image

updated 2014-06-11 08:17:29 -0500

Hi everybory!

I want to know the list (or number) of subscribers on a topic programmatically. I know that I can use the rostopic list but I need that information in my node.

Thanks!

Edit: There is a method to find it: uint32_t ros::Publisher::getNumSubscribers() const But I need to know the number of subscribers on a topic from another node and I don't have the Publisher object, just the name of the topic

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2014-06-12 20:00:36 -0500

kramer gravatar image

Never used it myself, but perhaps the TopicManager would help?

edit flag offensive delete link more
0

answered 2018-04-28 21:06:13 -0500

Cerin gravatar image

updated 2018-04-28 21:11:11 -0500

I'm not sure why you're implying you can't use rostopic list from your node. Nodes can call command line tools. Unfortunately, there doesn't seem to be a an easier way to find this info through the C++ or Python API. I just scrape the output of rostopic list <topic> --verbose in my custom nodes.

It will output something like:

Published topics:
 * <topic> [std_msgs/String] 1 publisher

Subscribed topics:
 * <topic> [std_msgs/String] 1 subscriber

The number before publisher/subscriber is the number of nodes publishing to or subscribing to that topic respectively. You can easily use regexp or simple string parsing to extract that number.

edit flag offensive delete link more
0

answered 2014-06-10 15:30:21 -0500

ipso gravatar image

updated 2014-06-10 15:31:04 -0500

Let Me Google That For You: "ros publisher number subscribers".

Fifth result: uint32_t ros::Publisher::getNumSubscribers() const.

edit flag offensive delete link more

Comments

Thanks for the reply, but I'll put more information about my problem: when I have the Publisher object I know that I can use this method. But I need to know the number of subscribers in another node. I don't have the Publisher object, just the name of the topic.

ricardoej gravatar imagericardoej ( 2014-06-11 08:14:39 -0500 )edit

"AttributeError: 'Publisher' object has no attribute 'getNumSubcribers'"

Cerin gravatar imageCerin ( 2018-04-28 21:00:50 -0500 )edit

@Cerin: this is C++, not Python.

gvdhoorn gravatar imagegvdhoorn ( 2018-04-29 01:02:09 -0500 )edit

With rospy, use get_num_connections().

gvdhoorn gravatar imagegvdhoorn ( 2018-04-29 01:03:25 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-06-10 14:53:39 -0500

Seen: 4,138 times

Last updated: Apr 28 '18