What is a publisher in ROS2?
Atlasbot · 16/8/2026 · 10 min read
A node that wants to "speak" out data — robot position, camera image, battery state — needs to generate a Publisher: object representing the right to send data to a particular topic.
Key concepts
The publisher is created inside a node, attached to a fixed topic name and message type. After creation, the node just calls .publish(msg) whenever new data to send — is normally set in a periodic run function thanks to ROS2's Timer (e.g. publish every 0.1 s = frequency 10Hz).
Publisher does not wait for a response, unaware of any subscriber listening — it simply pushes data into the system, assigning the DDS below to take care of the delivery to registered subscribers (see also Topic and DDS).
Summarized: Making a publisher does not mean that data that will automatically be sent — must actively invoke
.publish()each time new data is available, usually through a regular Run Timer.
How it works
Minimum Publisher in a Python node, sending fixed velocity every 0.5 seconds:
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist
class VelocityPublisher(Node):
def __init__(self):
super().__init__('velocity_publisher')
self.publisher_ = self.create_publisher(Twist, '/cmd_vel', 10) # 10 = queue depth (QoS)
self.timer = self.create_timer(0.5, self.timer_callback) # callback every 0.5s
def timer_callback(self):
msg = Twist()
msg.linear.x = 0.2 # 0.2 m/s straight forward
self.publisher_.publish(msg)
self.get_logger().info(f'Đã gửi: linear.x = {msg.linear.x}')
The parameter 10 in create_publisher(Twist, '/cmd_vel', 10) is the message queue depth (part of QoS — Quality of Service, see separate article) — the maximum number of messages to be kept in buffer if the recipient does not process in time.
Technical details
Formulas
Timer and frequency publish inverse relation — need to be properly accounted for when the request is "publish at X Hz":
Timer period (seconds) = 1 / Desired frequency (Hz)
Example: publish odometry at 50Hz
Timer period = 1 / 50 = 0.02 seconds → create_timer(0.02, callback)
Parameters
frequency reasonable publish differs significantly by data type — publish too fast waste bandwidth, too slow to make "old" data affect the back control loop:
| Data Type | typical frequency publish |
|---|---|
Odometry (/odom) | 20 – 50Hz |
LiDAR scan (/scan) | 10 – 15Hz |
| Camera image | 15 – 30fps |
| Battery Status/Diagnostics | 1Hz or Event - driven |
Practical applications
Not all publishers publish under fixed Timer — low-change data such as battery states or hardware error flags that typically publish in a event-driven fashion (send only when the true value changes, or publish much slower than the primary sensor data) to save bandwidth and avoid "stopping" other topics that are in need of bandwidth for high frequency data such as LiDAR/camera. Designing the right frequency for each type of publisher is an important architectural decision when multiple nodes run on a robot-internal network.
Practical example
Equivalent Publisher written in C++ (rclcpp), usually used when higher performance is required than Python:
#include "rclcpp/rclcpp.hpp"
#include "geometry_msgs/msg/twist.hpp"
class VelocityPublisher : public rclcpp::Node {
public:
VelocityPublisher() : Node("velocity_publisher") {
publisher_ = create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", 10);
timer_ = create_wall_timer(std::chrono::milliseconds(500),
std::bind(&VelocityPublisher::timer_callback, this));
}
private:
void timer_callback() {
auto msg = geometry_msgs::msg::Twist();
msg.linear.x = 0.2;
publisher_->publish(msg);
}
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr publisher_;
rclcpp::TimerBase::SharedPtr timer_;
};
$ ros2 topic echo /cmd_vel
Output:
linear:
x: 0.2
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
The same logic structure as the Python — publisher, timer, publish in callback — with only different language syntax, exemplifies what the multilingualism said in ROS2 article is.
Common mistakes
- Creates publisher but forgets to call
.publish()— publisher exists, node runs normally, but no message is actually sent — checks withros2 topic echoto confirm that there really is data flowing through, not just believes the code "runs without errors". - The queue depth is too small compared to the speed of subscriber processing — if the subscriber processes slower than the speed of publication, the old message in the queue is removed when the queue is full — with data to be complete must not be lost (e.g. log critical events), it is necessary to increase the queue depth or change the QoS policy accordingly.
- Publish as soon as the node is initialized, before discovery is complete — DDS takes a short time for the publisher/subscriber to "find each other" via discovery — the first few messages sent as soon as the node is initialized may be lost because the subscriber has not yet connected, not a code error.
- Using the same object message between publications forgetting to reset the field — if only a few fields are updated and then the old object is re-printed, the unupdated fields retain their previous values, which may inadvertently send the "old" data mixed into the new data.
Conclusion
Publisher is the "sending" half of the pub-sub model in ROS2 — Mastering how to create a publisher, choosing the right publication frequency by data type, and understanding how queue depth affects data loss are fundamental skills when writing any ROS2 node. The next issue of Subscriber — the other half, the recipient of the topic.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.