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

Revision history [back]

click to hide/show revision 1
initial version

As @gvdhoorn and @knxa noted, this is expected behavior (though perhaps surprising), because the advertise() call returns before the subscriber has made the socket connection to the publisher (which takes maybe 300ms).

If you are writing a command-line app that has to publish something to a listener that is already running, there are two things you have to do:

  1. Insert the following code snippet between advertise() and publish()

    while (0 == chatter_pub.getNumSubscribers()) { ROS_INFO("Waiting for subscribers to connect"); ros::Duration(0.1).sleep(); }

  2. Call ros::shutdown before your program exits, to flush the published message to the subscriber

As @gvdhoorn and @knxa noted, this is expected behavior (though perhaps surprising), because the advertise() call returns before the subscriber has made the socket connection to the publisher (which takes maybe 300ms).

If you are writing a command-line app that has to publish something to a listener that is already running, there are two things you have to do:

  1. Insert the following code snippet between advertise() and publish()

while (0 == chatter_pub.getNumSubscribers()) { ROS_INFO("Waiting for subscribers to connect"); ros::Duration(0.1).sleep(); }

  1. Call ros::shutdown before your program exits, to flush the published message to the subscriber

As @gvdhoorn and @knxa noted, this is expected behavior (though perhaps surprising), because the advertise() call returns before the subscriber has made the socket connection to the publisher (which takes maybe 300ms).

If you are writing a command-line app that has to publish something to a listener that is already running, there are two things you have to do:

  1. Insert the following code snippet between advertise() and publish()

while (0 == chatter_pub.getNumSubscribers()) { ROS_INFO("Waiting for subscribers to connect"); ros::Duration(0.1).sleep(); }

  1. Call ros::shutdown before your program exits, to flush the published message to the subscriber

As @gvdhoorn and @knxa noted, this is expected behavior (though perhaps surprising), because the advertise() call returns before the subscriber has made the socket connection to the publisher (which takes maybe 300ms).

If you are writing a command-line app that has to publish something to a listener that is already running, there are two things you have to do:

  1. Insert the following code snippet between advertise() and publish()

 while (0 == chatter_pub.getNumSubscribers()) {
       ROS_INFO("Waiting for subscribers to connect");
       ros::Duration(0.1).sleep();
  }

}
  1. Call ros::shutdown ros::shutdown() before your program exits, to flush the published message to the subscriber