Skip to content

RobotWebTools/rclnodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,025 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rclnodejs - The ROS 2 Client Library for JavaScript

npm npm downloads GitHub stars Coverage Status node TypeScript

ROS 2 Distro CI Status
ROS 2 Linux x64
Linux arm64
Windows
ASan

rclnodejs is a Node.js client library for ROS 2 that provides comprehensive JavaScript and TypeScript APIs for developing ROS 2 solutions.

Key features: Topics, Services, Actions, Parameters, Lifecycle Nodes, TypeScript support, RxJS Observables, Electron integration, browser ↔ ROS 2 WebSocket bridge (rosocket), and prebuilt binaries for Linux x64/arm64.

const rclnodejs = require('rclnodejs');
rclnodejs.init().then(() => {
  const node = new rclnodejs.Node('publisher_example_node');
  const publisher = node.createPublisher('std_msgs/msg/String', 'topic');
  publisher.publish(`Hello ROS 2 from rclnodejs`);
  node.spin();
});

This example assumes your ROS 2 environment is already sourced.

Documentation

Installation

Most users only need Install from npm below. If you have cloned this repository and want to run the bundled examples, see Quick Start instead.

Prerequisites

Before installing, building, or running rclnodejs, source your ROS 2 environment:

source /opt/ros/<distro>/setup.bash

Install from npm

Use this path if you want to depend on rclnodejs from your own ROS 2 Node.js application.

npm i rclnodejs

After installation, use the example at the top of this README as a minimal publisher, or continue with Quick Start to run the examples in this repository.

Install from GitHub

Use this path only if you need a branch or commit not yet published to npm. GitHub installs build from source.

npm install RobotWebTools/rclnodejs#<branch>

Docker: For containerized development, see the included Dockerfile for building and testing with different ROS distributions and Node.js versions.

See the features and try the examples to get started.

Prebuilt Binaries

rclnodejs ships with prebuilt native binaries for common Linux configurations, so most installs skip compilation.

Supported Platforms:

  • Ubuntu 22.04 (Jammy) - ROS 2 Humble
  • Ubuntu 24.04 (Noble) - ROS 2 Jazzy, Kilted
  • Ubuntu 26.04 (Resolute) - ROS 2 Lyrical
  • Architectures: x64, arm64
  • Node.js: >= 20.20.2 (N-API compatible)

Installations outside this matrix automatically fall back to building from source. To force a source build even when a prebuilt binary is available:

export RCLNODEJS_FORCE_BUILD=1
npm install rclnodejs

Quick Start

From a clone of this repository, after sourcing your ROS 2 environment:

  1. Install the repository dependencies from the project root.
npm install
  1. Run a publisher example from this checkout.
node example/topics/publisher/publisher-example.js

More runnable examples in example/ and step-by-step guides in tutorials/.

ROS 2 Interface Message Generation

rclnodejs auto-generates JavaScript bindings and TypeScript declarations for every ROS 2 .msg, .srv, and .action interface available in your sourced ROS 2 environment. This happens during npm install, so in most projects you do not need to run anything by hand.

Use the generated types directly:

const rclnodejs = require('rclnodejs');
let stringMsgObject = rclnodejs.createMessageObject('std_msgs/msg/String');
stringMsgObject.data = 'hello world';

Re-running message generation

If you install additional ROS packages after rclnodejs was installed, re-run the generator from your project so the new interfaces are picked up:

npx generate-ros-messages

Generated files are written to <your-project>/node_modules/rclnodejs/generated/.

IDL Message Generation

In addition to the standard ROS 2 message generation (.msg, .srv, .action), rclnodejs can also generate JavaScript message files directly from IDL (Interface Definition Language) files. This is useful for custom IDL files or when you need finer control over the generation process.

To generate messages from IDL files:

npm run generate-messages-idl

Using rclnodejs with TypeScript

TypeScript declaration files are included in the package and exposed through the types entry in package.json. In most projects, configuring your tsconfig.json is sufficient:

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "target": "es2020",
  },
}

Then import * as rclnodejs from 'rclnodejs' works the same as the JavaScript example at the top of this README. See TypeScript demos for more.

rosocket — Browser ↔ ROS 2 bridge

A tiny WebSocket gateway to ROS 2 — built into rclnodejs. New in 2.0.0-beta.0.

rosocket exposes ROS 2 topics/services as plain WebSocket URLs — a lightweight alternative to the rosbridge + roslibjs stack. Zero browser code, one Node.js process; browsers use only built-in WebSocket + JSON, no JavaScript library required.

npx rosocket --port 9000 --topic /chatter:std_msgs/msg/String
const ws = new WebSocket('ws://host:9000/topic/chatter');
ws.onmessage = (e) => console.log(JSON.parse(e.data).data);
ws.onopen    = () => ws.send(JSON.stringify({ data: 'hi' }));

See rosocket/README.md for the URL scheme, service calls, and the programmatic startRosocket() API.

Observable Subscriptions

rclnodejs supports RxJS Observable subscriptions for reactive programming with ROS 2 messages. Use operators like throttleTime(), debounceTime(), map(), and combineLatest() to build declarative message processing pipelines.

const { throttleTime, map } = require('rxjs');

const obsSub = node.createObservableSubscription(
  'sensor_msgs/msg/LaserScan',
  '/scan'
);
obsSub.observable
  .pipe(
    throttleTime(200),
    map((msg) => msg.ranges)
  )
  .subscribe((ranges) => console.log('Ranges:', ranges.length));

See the Observable Subscriptions Tutorial for more details.

Electron-based Visualization

Build interactive desktop ROS 2 apps with Electron + Three.js, packaged for Windows/macOS/Linux via Electron Forge. Featured demo: 🦾 manipulator — a two-joint arm with manual/automatic control.

manipulator demo

More in demo/electron.

Performance Benchmarks

Benchmark results for 1000 iterations with 1024 KB messages (Ubuntu 24.04 WSL2, i7-1185G7):

Library Topic (ms) Service (ms)
rclcpp (C++) 168 627
rclnodejs (Node.js) 744 927
rclpy (Python) 1,618 15,380

See benchmark/README.md for the full setup and methodology.

rclnodejs-cli

rclnodejs-cli is a companion project providing command-line tooling for scaffolding rclnodejs application skeletons and working with launch files for multi-node orchestration.

Contributing

Please read the Contributing Guide before making a pull request.

Thanks to all contributors!

License

This project abides by the Apache License 2.0.