Intro

rosbridge is a middleware/JSON API that allows non-ROS clients to communicate with ROS through Websockets.

.

rosbridge provides a JSON interface to ROS, allowing any client to send JSON to publish or subscribe to ROS topics, call ROS services, and more. rosbridge supports a variety of transport layers, including WebSockets and TCP

Reference

Core Components/ Architecture

  • Rosbridge Protocol:- specification for the JSON messages used to communicate with the ROS environment.

    • At minimum it includes an “op” field specifying the operation (e.g. publish, subscribe, call service).
  • rosbridge_library the actual translation between the JSON strings and the internal ROS calls (topics, services, actions, etc.).

    • It handles the serialization and deserialization of messages without worrying about the transport layer.
  • rosbridge_server- a ROS node that opens a WebSocket server (usually ws://localhost:9090) and passes the incoming JSON messages to the rosbridge_library for processing, and sends ROS responses back over the WebSocket.

  • Client Libraries (e.g., roslibjs, roslibpy)- handle the client’s end of the WebSocket communication and provide a convenient API for developers to interact with the rosbridge server.

.

rosbridge_suite for ROS2- Quick Setup 1

See ROS Web Tutorial for a more complete guide

  • Install rosbridge_suite for ROS2 humble
sudo apt update
sudo apt install ros-humble-rosbridge-suite
  • Launch rosbridge_websocket server (Note: humble uses Python v10)
ros2 launch rosbridge_server rosbridge_websocket_launch.xml
# [rosbridge_websocket-1] [INFO] [1726600761.629841995] [rosbridge_websocket]: Rosbridge WebSocket server started on port 9090

By default, this will expose a WebSocket on port 9090 (reserved for ROS communication)

To customize the WebSocket port, add a port argument

ros2 launch rosbridge_server rosbridge_websocket_launch.xml port:=8080

[5050] - Web GUI - [9090] <-----> [9090] - rosbridge

  • Verify that the server is listening on the specified port
netstat -an | grep 9090
tcp        0      0 0.0.0.0:9090            0.0.0.0:*               LISTEN
tcp6       0      0 :::9090                 :::*                    LISTEN
  • Test the WebSocket connection in the console
    • This script will try to open a WebSocket connection to ws://localhost:9090 and log the status in the console.
var ws = new WebSocket('ws://localhost:9090');
ws.onopen = function() {
    console.log('WebSocket connection opened.');
};
ws.onerror = function(error) {
    console.error('WebSocket error: ', error);
};
ws.onmessage = function(event) {
    console.log('WebSocket message: ', event.data);
};

Footnotes

  1. https://medium.com/@rafaazahra_93357/how-setup-rosbridge-suite-for-ros2-roslib-js-library-74b918db1a64 ↩