Skip to content

usePublisher

Advertises a ROS topic for the lifetime of the calling component and returns a stable publish function bound to it. Re-advertises whenever the topic name/type or other options change, and unadvertises on unmount.

tsx
import { usePublisher } from "roshooks";

interface Twist {
  linear: { x: number; y: number; z: number };
  angular: { x: number; y: number; z: number };
}

const { publish } = usePublisher<Twist>({
  name: "/cmd_vel",
  messageType: "geometry_msgs/Twist",
});

Options

OptionTypeDefaultDescription
namestringThe topic name, e.g. "/cmd_vel".
messageTypestringThe message type, e.g. "geometry_msgs/Twist".
throttle_ratenumber0Minimum time (ms) between messages.
queue_sizenumber100The bridge-side publish queue size.
latchbooleanfalseLatch the topic when publishing.
queue_lengthnumber0The bridge-side subscribe queue length.
reconnect_on_closebooleantrueAutomatically re-advertise on reconnection.

Returns

FieldTypeDescription
publish(message: TMessage) => voidPublishes a message. A no-op until the underlying topic is ready (there's a one-render window right after mount).
topicTopic<TMessage> | nullThe underlying roslib.js Topic instance, or null before it's created.

Example

tsx
function StopButton() {
  const { publish } = usePublisher<Twist>({
    name: "/cmd_vel",
    messageType: "geometry_msgs/Twist",
  });

  return (
    <button
      onClick={() =>
        publish({
          linear: { x: 0, y: 0, z: 0 },
          angular: { x: 0, y: 0, z: 0 },
        })
      }
    >
      Stop
    </button>
  );
}

publish internally calls Topic.publish, which advertises the topic automatically the first time it's called — there's no separate "advertise" step to manage yourself.

Released under the BSD-3 License.