Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rosnodejs

> 开发工具
Open source

Client library for writing ROS nodes in JavaScript with nodejs

200 stars0 likes2 views
WebsiteGitHub

About

Client library for writing ROS nodes in JavaScript with nodejs

rosnodejs

Install

npm install rosnodejs

Start a node

javascript
const rosnodejs = require('rosnodejs');
rosnodejs.initNode('/my_node')
.then(() => {
  // do stuff
});

Publish/Subscribe

javascript
const nh = rosnodejs.nh;
const sub = nh.subscribe('/chatter', 'std_msgs/String', (msg) => {
  console.log('Got msg on chatter: %j', msg);
});

const pub = nh.advertise('/chatter', 'std_msgs/String');
pub.publish({ data: "hi" });

The advertise function takes an options object as optional third argument:

javascript
{
  queueSize: 1,
  latching: false //
  throttleMs: 0
}

If you plan to publish messages right after another in the same function, set throttleMs: -1. Otherwise only the last message will be sent.

Udp transport (Experimental)

javascript
const nh = rosnodejs.nh;
const sub = nh.subscribe('/chatter', 'std_msgs/String', (msg) => {
  console.log('Got msg on chatter: %j', msg);
}, {
  transports: ["TCPROS", "UDPROS"],  // specify transports, default ["TCPROS"]
  dgramSize: 1500   // optional: datagram packet size, default: 1500 bytes
});

const pub = nh.advertise('/chatter', 'std_msgs/String');
pub.publish({ data: "hi" });

Services

javascript
const service = nh.advertiseService('/add_two_ints', 'beginner_tutorials/AddTwoInts', (req, res) => {
  res.sum = req.a + req.b;
  return true;
});

const client = nh.serviceClient('/add_two_ints', 'beginner_tutorials/AddTwoInts');
client.call({a: 1, b: 2});

Params

javascript
nh.setParam('val', 2);
nh.getParam('val')
.then((val) => {
  // do stuff
});

Generating Messages

Messages can be generated in a number of ways depending on the versions of ROS and Node.js you're using.

  • catkin - works in ROS Kinetic and later for Node.js v6+.
bash
$ catkin_make
OR
$ catkin build
  • loadAllPackages() - One-time "build" call available through rosnodejs for versions of ROS before Kinetic and Node.js v6+. Should be called separately and in advance of processes attempting to use messages.
javascript
const rosnodejs = require('rosnodejs');
rosnodejs.loadAllPackages();
  • On the fly - all versions of ROS and Node.js 4.5+. When generating on the fly, messages can not be required until the node has initialized.
javascript
const rosnodejs = require('rosnodejs');
rosnodejs.initNode('my_node', { onTheFly: true }).then(() => {
  const stdMsgs = rosnodejs.require('std_msgs');
  ...
}
Pre-Kinetic Kinetic & Later
Node.js >= v6 loadAllPackages(), on the fly catkin, loadAllPackages(), on the fly
Node.js { ... });
const pub = nh.advertise('/chatter', StringMsg);

const AddTwoInts = rosnodejs.require('beginner_tutorials').srv.AddTwoInts; const service = nh.advertiseService('/add_two_ints', AddTwoInts, (req, res) => { ... }); const client = nh.serviceClient('/add_two_ints', AddTwoInts);

## Actions (Experimental)
```js
const nh = rosnodejs.nh;
const as = new rosnodejs.ActionServer({
  nh,
  type: 'turtle_actionlib/Shape',
  actionServer: '/turtle_shape'
});

as.on('goal', function (goal) {
  goal.setAccepted();
});

as.start();

const ac = new rosnodejs.ActionClient({
  nh,
  type: 'turtle_actionlib/Shape',
  actionServer:'/turtle_shape'
});

ac.sendGoal({edges: 3, radius: 1});

Run the turtlesim example

Start:

bash
roscore
rosrun turtlesim turtlesim_node
rosrun turtle_actionlib shape_server

Then run

bash
node src/examples/turtle.js

or, if you are running an older version of node:

bash
npm run compile
node dist/examples/turtle.js

Catkin-Like

Checkout rosnodejs_examples for a more catkin-inspired take on using rosnodejs.

Inspired By

rosnodejs was inspired by other work that you can learn more about here

  • roscpp & rospy
  • Robots as web services
  • ROSBridge
  • roslibjs

Issues· 17 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptclient-libraryjavascriptnodejsrobot

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具