ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How can I publish an integer and string (Int16 & String) using roslibjs?

asked 2013-06-26 00:05:32 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.


I am recently learning robotic web tools.In git has some samples. I want to publish an indeger / string message .On refreshing the browser I want to get the string message in command prompt. But In my code it is not publish anything right now. Any helps are appreciated.

I run

 rostopic echo /buttons
 roslaunch rosbridge_server rosbridge_websocket.launch

I am use the following code

 <!DOCTYPE html>
 <meta charset="utf-8" />
 <script src="../include/EventEmitter2/eventemitter2.js"></script>
 <script src="../build/roslib.js"></script>

   // Connecting to ROS
   // -----------------
   var ros = new ROSLIB.Ros();

   // If there is an error on the backend, an 'error' emit will be emitted.
   ros.on('error', function(error) {

   // Find out exactly when we made a connection.
   ros.on('connection', function() {
     console.log('Connection made!');

   // Create a connection to the rosbridge WebSocket server.

   // Publishing a Topic
   // ------------------

   // First, we create a Topic object with details of the topic's name and      message type.
   var testStr = new ROSLIB.Topic({
     ros : ros,
     name : '/buttons',
     messageType : 'std_msgs/String'
   // Then we create the payload to be published. The object we pass in to ros.Message matches the 
   // fields defined in the geometry_msgs/Twist.msg definition.
   var str = new ROSLIB.Message(

   // And finally, publish.


     <li><tt>rostopic echo /buttons</tt></li>
     <li><tt>roslaunch rosbridge_server rosbridge_websocket.launch</tt></li>

My Doubts

1) Is it there in any problem of the syntax I used?

2) how can I publish Int16 messages?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-06-26 03:58:05 -0500

rtoris288 gravatar image

updated 2013-06-26 03:59:44 -0500

You have both a syntax error and a bug in your message definition. To catch syntax errors, it's useful to run the JavaScript error console your web browser provides.

var str = new ROSLIB.Message({

The syntax error is that you are missing brackets { } around the message data.

Secondly, you have defined your message type as std_msgs/String which does not have a field called a (see Instead, the field is called data:

var str = new ROSLIB.Message({
  data : 'hello'

With the above change, everything should work.

To publish an std_msgs/Int16 message, simply change your data type:

var testInt = new ROSLIB.Topic({
  ros : ros,
  name : '/ints',
  messageType : 'std_msgs/Int16'

var int = new ROSLIB.Message({
  data : 5

edit flag offensive delete link more



hi ... thank you very much.. I have one more doubt... how can I publish a variable as an integer var int = new ROSLIB.Message({ data : int_variable }); instead of 5 just want to use a variable

unais gravatar image unais  ( 2013-06-27 00:10:35 -0500 )edit

It helps me to to publish a std_msgs/Int32, I was a little lost with this work. Thank you :D

Wilianson gravatar image Wilianson  ( 2016-11-07 11:46:59 -0500 )edit

Question Tools


Asked: 2013-06-26 00:05:32 -0500

Seen: 4,660 times

Last updated: Jun 26 '13