ROS2-008-Communication Mechanisms, Wrapping Up: Distributed Communication and ROS_DOMAIN_ID
Introduction
In the earlier core material we went through the four communication mechanisms systematically. Back then all four were used for nodes and topics talking to each other inside a single ROS device. So how do we handle communication between several ROS2 devices?
Distributed communication
Distributed communication is exactly this kind of strategy: over the network, between different hosts, for exchanging data.
First, we should understand that ROS2 is itself a distributed communication framework — it can use middleware to make communication between different devices very easy.
The middleware ROS2 is built on is the Data Distribution Service (DDS). When several ROS2 devices sit on the same network, distributed communication can happen through DDS's domain ID mechanism (ROS_DOMAIN_ID). Roughly, the process is:
- Set the domain ID value before starting the nodes
- When different nodes have the same domain ID value, they can discover each other and communicate.
- Conversely, when different nodes have different domain ID values, none of the above works.
- And of course, by default every node starts with a domain ID value of "0", so as long as they are on the same network, without configuring the domain ID at all, in theory the nodes are already doing distributed communication.
What is it used for?
Plenty of data-exchange use cases depend on distributed communication. For example:
- Drone swarms, robot swarms and the like, where each individual robot needs the speed, position, trajectory and other information of the robots around it.
- Remote-controlling a robot, where you may need the environmental information that robot has collected and need to send control commands down to it.
- Other use cases not listed here that involve exchanging data between multiple devices.
How do you do it?
For multi-machine communication you can use the domain ID to group nodes: nodes in the same group can talk freely to each other, while nodes in different groups cannot. If you want every node in one group, just use the default domain ID. If you want to split nodes into several groups, then before starting a node in a terminal, set that node's domain ID inside the terminal (say to 6). The command is:
export ROS_DOMAIN_ID=6After running the command above, any node started in that terminal is placed in the domain whose domain ID is 6.
A demo of the domain ID at work
If you want to give every node on the machine running ROS2 one shared domain ID, run this in a terminal:
echo "export ROS_DOMAIN_ID=6" >> ~/.bashrcThen restart the terminal, and from then on every node running on that device is automatically placed in the domain whose domain ID is 6.
⚠ Things to watch out for ⚠
When setting ROS_DOMAIN_ID, stick to the following:
- It is recommended that ROS_DOMAIN_ID take a value in [0,101];
- The total number of nodes within each ROS_DOMAIN_ID is capped — the total must be less than or equal to 120;
- If ROS_DOMAIN_ID equals 101, the total number of nodes in that domain ID is capped harder — the total must be less than or equal to 54.
The reason comes down to the two calculation rules below 👇 just for your information. (Anyway, as long as you follow this advice nothing will go wrong — read on only if you really want to know why)
1. How ROS_DOMAIN_ID is calculated inside DDS
- DDS is based on the TCP/IP or UDP/IP network protocols, and network communication requires a port number. The port number is an unsigned integer of 2 bytes (i.e. 2*8=16 bit), and its value range is [0, ], i.e. between [0,65535];
- Port allocation follows its own rules too: the DDS spec sets 7400 as the starting port, so the usable ports shrink to [7400,65535]. And since by default each ROS_DOMAIN_ID occupies 250 ports in DDS, the number of ROS_DOMAIN_IDs is , with the corresponding value range [0,231];
- Different operating systems reserve different ports, and when DDS uses ports it has to steer around those reserved ones to avoid conflicts. On Linux the usable ROS_DOMAIN_IDs are , while on Windows & Mac the usable domains are [0,166]. So all told, for cross-platform compatibility, it is recommended that ROS_DOMAIN_ID take a value in [0,101].
2. How the total number of nodes in a ROS_DOMAIN_ID is calculated
- Each DOMAIN_ID occupies 250 ports by default, and within those 250 ports every ROS2 node takes two ports.
- The DDS spec defines that within each DOMAIN_ID's port block, the first and second ports are the Discovery Multicast port and the User Multicast port respectively, and the first node in the DOMAIN_ID starts from the tenth port of the block — that is, the tenth and eleventh ports are the Discovery Unicast port and the User Unicast port used by the first node, the twelfth and thirteenth are the Discovery Unicast port and the User Unicast port used by the second node, and so on.
- So the maximum number of nodes usable within one DOMAIN_ID is
- When the DOMAIN_ID value is 101, because the back half of its ports are operating-system reserved ports, the maximum number of nodes under that DOMAIN_ID is 54.
Info
If you want the exact formulas to reference in the documentation, see this URL: Statically configure a Firewall to let OMG DDS Traffic through | Data Distribution Service (DDS) Community RTI Connext Users Note: that site is entirely in English
Important
This article was translated by Hermes Agent; the original text can be found at here. The translation is for reference only. Translation time: 2026-11-05
