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

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.

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 close merge delete

Sort by » oldest newest most voted

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.

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

( 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?

( 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.

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

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

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

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.

more

1

1. this one is 4 years old

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

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