Skip to content

[ROS 2] Proposal to fix BSON Only Mode for high-overhead data (e.g. PointCloud2) #1051

@apt-shirokane

Description

@apt-shirokane

Description

I am opening this issue to enable proper BSON handling when using rosbridge in ROS 2. The goal is to use the more efficient BSON format for handling relatively large messages, like sensor_msgs/msg/PointCloud2, because serializing them to text with JSON creates significant overhead.

Related Issues

This issue is related to the following previously closed issues:

I assume these were not closed because the bug was fixed, but were automatically closed as "not planned" due to a long period of inactivity.

Environment

  • Library Version: 2.0.1
  • ROS Version: Humble
  • Platform / OS: Ubuntu 22.04.5 LTS (on arm64)

Steps To Reproduce

In the current ros2 branch, the on_message function in websocket_handler.py attempts to decode all incoming binary messages as UTF-8. This prevents the bson_only_mode from working as intended.

  1. Launch rosbridge_server with bson_only_mode enabled.
ros2 launch rosbridge_server rosbridge_websocket_launch.xml bson_only_mode:=true
  1. Run a client that sends a message containing BSON-encoded binary data (which is not valid UTF-8).

Expected Behavior

When launched with bson_only_mode:=True, BSON binary messages should be processed correctly without causing a decoding error.

Actual Behavior

The rosbridge_server throws a UnicodeDecodeError and either crashes or outputs an error log.

Proposed Solution

I believe this issue can be resolved by applying the following patch to websocket_handler.py.

--- a/rosbridge_server/src/rosbridge_server/websocket_handler.py
+++ b/rosbridge_server/src/rosbridge_server/websocket_handler.py
@@ -158,15 +158,13 @@
 
     @log_exceptions
     def on_message(self, message):
-        if isinstance(message, bytes):
-            message = message.decode("utf-8")
-        self.incoming_queue.push(message)
-
+        if self.bson_only_mode and isinstance(message, bytes):
+            # BSON ONLY MODE: push binary directly
+            self.incoming_queue.push(message)
+        else:
+            if isinstance(message, bytes):
+                message = message.decode("utf-8")
+            self.incoming_queue.push(message)

Next Steps

Could you please provide your feedback on this approach? If this direction is acceptable, I am prepared to create a pull request.

/cc @jacobperron

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions