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

Get (remote) node name(-space) when receiving a message

asked 2016-07-25 16:06:40 -0500

CodeFinder gravatar image

updated 2016-07-25 16:43:23 -0500

Hi all,

when I subscribe to a topic, is there a way to get the name / namespace of the publishing node on reception of a message?

I know about ros::this_node::getName() and the like [Ref] but that only yields the name/-space of the current node. For example, assume I (manually) subscribe to /rosout and I am receiving a message send from the node /foo/bar/node1. Is there a way to get this information (/foo/bar/node1) in the callback?

Please note that I prefer a way to do this in roscpp.

Thanks in advance!

EDIT: Since I am dealing with custom messages here, I've simply added the namespace of the sender to my message definition. However, I thought there's probably some smarter way to do this (and I was hoping that the sender is already contained somewhere in the received message data).

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-07-25 18:08:37 -0500

ahendrix gravatar image

There aren't a lot of good reasons for wanting to do this, because it breaks the anonymity of the Anonymous publish/subscribe data flow model. It can be useful for debugging, but building functional logic that depends on the publisher name it is not usually a good idea.

Caveats aside, you can access the publisher name by subscribing to the MessageEvent<msgtype> object (templated on your actual message type), which contains additional metadata in addition to the message data. Have a look at the MessageEvent subsection of the ROSCPP Publishers and Subscribers page for the details.

edit flag offensive delete link more


Thanks for your answer, good point! What I am considering is: given a global topic /ping, all my robots (/nodes) send a regular msg to that topic. This way, all robots are able to detect other active robots by subscribing to /ping. This "detection" is somehow necessary as there comes ...

CodeFinder gravatar image CodeFinder  ( 2016-07-26 07:03:02 -0500 )edit

... a point where two (or more) robots need to interact (/communicate) with each other.

To start communication, a node needs to know the namespace which I add to all msgs send to /ping. IMHO, This retains anonymity until two nodes start (bidirectional) communication. What do you think about this?

CodeFinder gravatar image CodeFinder  ( 2016-07-26 07:03:06 -0500 )edit

I like your approach because it keeps the anonymity in the pub/sub model, and because it explicitly communicates the namespaces to be used for future communication, instead of trying to infer the namespace from the node name.

ahendrix gravatar image ahendrix  ( 2016-07-26 11:48:50 -0500 )edit

Thanks, then I'll stick to that design. ;-)

CodeFinder gravatar image CodeFinder  ( 2016-07-26 16:11:05 -0500 )edit

answered 2020-05-25 08:48:57 -0500

hch gravatar image

Hi, I would like to ask this question also. I believe I have good reason to know the identity of the sender node, but maybe there is another way to resolve my problem and in that case I will be glad if someone can advise me.

My problem is: sometime, two nodes are running on the same computer or network segment and posting same topic. This is not exactly intentional, but it simply happens, just because i forget to terminate some node or some other user start his instance of some node. In this case, receiver of the topic gets mixed messages from both nodes, which causes problems due to data inconsistency. I need to detect that I receive messages from different nodes. There is no strict need to identify the source nodes, but I suppose it might help debugging a bit.

I would also add that topic namespaces do not solve my problem (consider topic names are fixed).

Thank you for any advice :)

Oh, I forgot to mention that I use ROS2.

edit flag offensive delete link more



Please post a new question:

  1. this one is 4 years old
  2. has an accepted answer
  3. is about ROS 1

that's all sufficiently different / old that a new question would be warranted.

gvdhoorn gravatar image gvdhoorn  ( 2020-05-25 10:24:21 -0500 )edit

Question Tools

1 follower


Asked: 2016-07-25 16:06:40 -0500

Seen: 1,248 times

Last updated: May 25 '20