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

How to find out the publisher node for a certain received message?

asked 2012-08-15 22:20:16 -0600

sacrif gravatar image

updated 2012-08-16 00:49:49 -0600

Hi everyone!

I am currently writing a node to capture the network traffic between single nodes.Therefore my node subscribes to all published messages and sorts them - amongst others - according to their publisher. Since it is possible that different nodes publish the same topic I would like to know if there is a way to find out the publisher-node of a certain received message in my callback function.

Regards scr


Thanks for the hint on MessageEvents. In the meanwhile I also found that there exist MessageEvents, which can also be passed to the subscribers callback function. In cpp it can be used as shown HERE. Thereby you can simply use event.getPublisher() to obtain the publisher of a message.

However I need the publisher of a message in rosjava. There also exists a MessageEvent ( for rosjava. However it does not seem to contain the desired information (message publisher). Moreover I am not sure whether it can be used for a callback function as simple as in cpp. When I create subscriber as following I receive the Exception "Exception in thread "pool-1-thread-13" java.lang.ClassCastException: org.ros.internal.message.$Proxy3 cannot be cast to"


        Subscriber<> subscriber = node.newSubscriber(topicType.getName(), topicType.getMessageType());
        subscriber.addMessageListener(new MessageListener<>() {
            public void onNewMessage( event) {


Do you know how to use MessageEvents in rosjava, or is there an other way to find out a messages publisher?

EDIT2: After some more research I found this post. So it seams it is currently not possible to access a messages publisher in the subscriber callback function. Is there probably a smart workaround?

edit retag flag offensive close merge delete



Yes, use a callback signature that uses a [Message Event](

dornhege gravatar image dornhege  ( 2012-08-15 23:28:25 -0600 )edit

Well, with java you'd have to look in the respective client API docs if something like that is supported. It looks like the MessageEvent you found is something totally else and unrelated to ROS.

dornhege gravatar image dornhege  ( 2012-08-16 00:42:10 -0600 )edit

Hi, thx for the reply. I also found an other post which suggests my request is currently not provided (see EDIT2). The MessageEvent is at least used in the SubscriberHandshakeHandler and Subscriber class of rosjava. Is there probably a smart workaround to simply retrieve the message publisher?

sacrif gravatar image sacrif  ( 2012-08-16 00:52:36 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2012-08-16 04:38:58 -0600

joq gravatar image

updated 2012-08-16 04:41:32 -0600

Why do you need that information?

The usual ROS practice is to distinguish topics, but decouple subscribers from worrying about which node published each message.

To work around that: add a source field to your message (or wrap some standard message in a custom message containing one), and define constants for the message sources you wish to distinguish. Then clients can tell the source without needing to worry about other nodes coming, going and restarting.

I did something similar in this custom message.

edit flag offensive delete link more

Question Tools


Asked: 2012-08-15 22:20:16 -0600

Seen: 4,748 times

Last updated: Aug 16 '12