Ask Your Question

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

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

CodeFinder gravatar image

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

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

1 Answer

Sort by ยป oldest newest most voted

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

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 imageCodeFinder ( 2016-07-26 07:03:02 -0600 )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 imageCodeFinder ( 2016-07-26 07:03:06 -0600 )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 imageahendrix ( 2016-07-26 11:48:50 -0600 )edit

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

CodeFinder gravatar imageCodeFinder ( 2016-07-26 16:11:05 -0600 )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

1 follower


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

Seen: 463 times

Last updated: Jul 25 '16