Connecting ROS 2 Nodes with the ROS_DISCOVERY_SERVER
env
- Level: 🌴 easy
- Compatible ROS distributions:
ROS 2 Jazzy
,ROS 2 Iron
- Compatible RMW implementations:
rmw_fastrtps_cpp
Fast DDS provides an alternative discovery mechanism based on Discovery Server. In addition to its performance benefits, it offers an easy way to connect ROS 2 nodes running on different Husarnet-connected hosts.
This approach is superior to using the ROS_STATIC_PEERS
environment variable because it allows new hosts to join the ROS 2 network dynamically without requiring the nodes on all peers to be restarted with an updated ROS_STATIC_PEERS
. In its minimal configuration, ROS_DISCOVERY_SERVER
requires only one peer to act as a Discovery Server; however, you can add multiple such peers as a backup option.
The ROS_SUPER_CLIENT
environment variable, as used in the example, can launch a DDS entity as a Super Client with more discovery information (without setting the ROS_SUPER_CLIENT
environment variable to TRUE
, you will not see all ROS 2 topics with ros2 topic list
).
Examples
Below is a demonstration using a talker-listener ROS 2 demo. Assume that ds-host
, talker-host
and listener-host
are Husarnet hostnames of the three devices in the same Husarnet group:
Host Setup
- "Discovery Server" host
- "talker" host
- "listener" host
This FastDDS CLI command runs the Discovery Server on an arbitrarily chosen host within the Husarnet group. You can execute it on any host, such as your laptop, a random Husarnet-connected server, or directly on the robot with the following command in the terminal:
fastdds discovery -i 0 -l husarnet-local -p 11811
This command starts the Discovery Server with id=0
, listening for connections on the IPv6 address provided by Husarnet (husarnet-local
is an alternative hostname to ds-host
in this example) on port 11811
.
The host running the Discovery Server can simultaneously run ROS 2 nodes:
export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
export ROS_SUPER_CLIENT=TRUE
export ROS_DISCOVERY_SERVER=ds-host:11811
ros2 run demo_nodes_cpp listener
Execute the following commands in the terminal:
export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
export ROS_SUPER_CLIENT=TRUE
export ROS_DISCOVERY_SERVER=ds-host:11811
ros2 run demo_nodes_cpp talker
Execute the following commands in the terminal:
export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
export ROS_SUPER_CLIENT=TRUE
export ROS_DISCOVERY_SERVER=ds-host:11811
ros2 run demo_nodes_cpp listener
Docker Setup
- "Discovery Server" host
- "talker" host
- "listener" host
Create the following file:
services:
ds:
image: ros:jazzy-ros-core
network_mode: host
command: fastdds discovery -i 0 -l husarnet-local -p 11811
Then, execute the following command:
docker compose up
Create the following two files in the same directory:
FROM ros:jazzy-ros-core
RUN apt update && apt install -y \
ros-${ROS_DISTRO}-demo-nodes-cpp && \
rm -rf /var/lib/apt/lists/*
name: ${CHATTER_ROLE:-talker}
services:
chatter:
build: .
network_mode: host
environment:
- RMW_IMPLEMENTATION=rmw_fastrtps_cpp
- ROS_DISCOVERY_SERVER=ds-host:11811
command: ros2 run demo_nodes_cpp ${CHATTER_ROLE:-talker}
Then, execute the following command:
CHATTER_ROLE=talker docker compose up --build --force-recreate
Create the following two files in the same directory:
FROM ros:jazzy-ros-core
RUN apt update && apt install -y \
ros-${ROS_DISTRO}-demo-nodes-cpp && \
rm -rf /var/lib/apt/lists/*
name: ${CHATTER_ROLE:-talker}
services:
chatter:
build: .
network_mode: host
environment:
- RMW_IMPLEMENTATION=rmw_fastrtps_cpp
- ROS_DISCOVERY_SERVER=ds-host:11811
command: ros2 run demo_nodes_cpp ${CHATTER_ROLE:-talker}
Then, execute the following command:
CHATTER_ROLE=listener docker compose up --build --force-recreate
You can also run the Docker-based demo on the same host, by running Husarnet within the container: