I want to publish ros2 custom message through ros2-web-bridge to react app. In react i have subscribed to published custom message. ros2-web-bridge is running on port ws://localhost:9090. I have created the custom message interface on ros2.
// this is client side web app subscribe method
var example = new ROSLIB.Topic({
ros: ros,
name: "/sample",
messageType: `tutorial_interfaces/msg/Num`,
});
// Subscribe a Topic
example.subscribe(function (message) {
console.log("Subscribe data", message);
});
// This is ros2 publish python code
import rclpy
from rclpy.node import Node
from tutorial_interfaces.msg import Num # CHANGE
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(Num, 'sample', 10) # CHANGE
timer_period = 0.5
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0
// This is ros2 published class and method
def timer_callback(self):
msg = Num() # CHANGE
msg.num = self.i # CHANGE
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%d"' % msg.num) # CHANGE
self.i += 1