Topics (Publishers And Subscribers)

Article by:
Date Published:
Last Modified:


ROS provides a publish and subscribe mechanism on message buses called topics.

More information about the ROS messages can be found on the Messages page.

Custom Subscriber Callbacks

You can implement custom subscriber callbacks by using the boost::bind class.

void MyClass::Load() {
    // Create ROS subscriber
    ros::Subscriber sub = nodeHandle->subscribe<MsgType>(
        // Here is where the magic happens, "hello!" will be passed
        // into the callback also
        boost::bind(&MyClass::Callback, this, _1, "hello!"));

    void MyClass::Callback(const MsgType& msg, std::string myNewParam) {
    std::cout << "Passed in custom parameter = " << myNewParm << std::endl;

Smart Pointer Behaviour

Subscribers have built-in smart pointer like behaviour.

Once all copies of a subscriber go out of scope, the callback function will not be called any-more. Once all subscribers for a particular topic go out of scope, the topic will be un-subscribed.


Geoffrey Hunter

Dude making stuff.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License .


comments powered by Disqus