Set up publishers and subscribers workflow
Add publisher and subscriber components to a scene so it can send and receive ROS messages.
This page assumes you're familiar with the concepts covered in Introduction to messaging.
The scene must have an active connector (for example, a ROS Endpoint Connector) before you register publishers, subscribers, or services. Without one, these calls fail silently: RegisterPublisher, Subscribe, and the MonoBehaviour helpers log a warning and leave CanPublish false or skip the subscription. For setup, refer to ROS Endpoint Connector reference.
Publish messages directly
Register a publisher once, then call Publish for each message:
IPublisher pub = connector.RegisterPublisher<PoseMsg>("/robot/pose");
pub.Publish(new PoseMsg(...));
Subscribe to messages directly
Subscribe to a topic and handle each incoming message in a callback:
connector.Subscribe<PoseMsg>("/robot/pose", msg =>
{
Debug.Log($"Position: {msg.position}");
});
Call and implement services
Implement a service (server side):
connector.ImplementService<SetBoolRequest, SetBoolResponse>(
"/enable_motor",
request => new SetBoolResponse { success = request.data });
Call a service (client side):
IServiceClient client = connector.RegisterServiceClient<SetBoolRequest, SetBoolResponse>("/enable_motor");
client.SendRequest<SetBoolRequest, SetBoolResponse>(
new SetBoolRequest { data = true },
response => Debug.Log($"Success: {response.success}"));
Use the MonoBehaviour helpers
These abstract MonoBehaviour classes handle connector resolution and registration for you, so your component only needs to implement the parts specific to its topic.
PublisherBehaviour<T>
PublisherBehaviour<T> is an abstract MonoBehaviour that resolves a connector and registers a publisher in Start().
public class SpeedPublisher : PublisherBehaviour<TwistMsg>
{
public override string DefaultTopic => "/cmd_vel";
void Update()
{
if (CanPublish)
Publish(new TwistMsg(...));
}
}
SubscriberBehaviour<T>
SubscriberBehaviour<T> is an abstract MonoBehaviour that subscribes to a topic in Start().
public class OdomSubscriber : SubscriberBehaviour<OdometryMsg>
{
public override string DefaultTopic => "/odom";
protected override Action<OdometryMsg> Callback =>
msg => transform.position = msg.pose.pose.position.From<FLU>();
}
AutoPublisher<T>
AutoPublisher<T> extends PublisherBehaviour<T> with time-based publishing. Override CreateMessage() to return the message to send; the base class handles the timing.
| Property | Default | Description |
|---|---|---|
PublishFrequency |
0.01 Hz | How often to publish, in Hz. |
PublishInFixedUpdate |
false | Publish in FixedUpdate instead of Update. |
public class ImuAutoPublisher : AutoPublisher<ImuMsg>
{
protected override ImuMsg CreateMessage() => BuildImuMsg();
}
Reduce garbage collection pressure with a message pool
Publishing creates a new message object each time, which adds to garbage collection pressure and can cause performance stalls. IMessagePool provides object pooling for messages to reduce this pressure by reusing message instances instead of allocating new ones. Call IPublisher.SetMessagePool(pool) to attach a pool to a specific publisher.