ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
8

How to get the number of subscribers on a topic?

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

ricardoej gravatar image

updated 2022-09-11 10:30:38 -0600

lucasw gravatar image

Hi everybody!

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

4 Answers

Sort by ยป oldest newest most voted
1

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

ipso gravatar image

updated 2022-01-16 18:39:53 -0600

lucasw gravatar image

uint32_t ros::Publisher::getNumSubscribers() const

Returns the number of subscribers that are currently connected to this Publisher.

edit flag offensive delete link more

Comments

1

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 image ricardoej  ( 2014-06-11 08:14:39 -0600 )edit

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

Cerin gravatar image Cerin  ( 2018-04-28 21:00:50 -0600 )edit

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

gvdhoorn gravatar image gvdhoorn  ( 2018-04-29 01:02:09 -0600 )edit
1

With rospy, use get_num_connections().

gvdhoorn gravatar image gvdhoorn  ( 2018-04-29 01:03:25 -0600 )edit
2

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

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 -0600

Cerin gravatar image

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

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 2022-02-08 18:22:31 -0600

peci1 gravatar image
#include <ros/topic_manager.h>
ros::TopicManager::instance()->getNumSubscribers("topic")

This returns the number of subscribing nodes, not single Subscriber objects...

edit flag offensive delete link more

Question Tools

Stats

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

Seen: 10,482 times

Last updated: Feb 08 '22