1.2.4. WebSocket Service Server¶
This example tackles the task of bridging a WebSocket server with one or more client applications, implemented using a wide variety of protocols.
Specifically, we discuss how to forward petitions coming from Fast DDS, ROS 1 and ROS 2
service client applications to a WebSocket WebSocketAddTwoInts server application,
so that it can process them and fulfill each request with a proper answer message.
Note
If you are looking for an example on how to perform a service request from a WebSocket client to another protocol, please refer to any of the remaining examples in the server/client examples section.
1.2.4.1. Requirements¶
To prepare the deployment and setup the environment, you need to have Integration Service correctly installed in your system. To do so, please follow the steps delineated in the Installation section.
Also, to get this example working, the following requirements must be met:
Having Fast DDS (v.2.0.0 or superior) installed and the Integration Service
DDSAddTwoIntsexample working. This example can be found in the main Integration Service repository, under the examples/utils/dds/DDSAddTwoInts folder; to compile it, you can either compile the whole Integration Service project usingcolconwith the CMake flagBUILD_EXAMPLESenabled; or execute the following steps:cd ~/is-workspace/src/Integration-Service/examples/utils/dds/DDSAddTwoInts mkdir build && cd build cmake .. -DBUILD_EXAMPLES=ON && make
Having the Fast DDS System Handle installed. You can download it from the FastDDS-SH dedicated repository into the
is-workspacewhere you have Integration Service installed:cd ~/is-workspace git clone https://github.com/eProsima/FastDDS-SH.git src/FastDDS-SHHaving ROS 1 (Melodic or superior) installed and the Integration Service
example_interfacesROS 1 package compiled. This package can be found in the main Integration Service repository, under the examples/utils/ros1/src/example_interfaces folder. To compile and install it:source /opt/ros/$ROS1_DISTRO/setup.bash cd ~/is-workspace/src/Integration-Service/example/utils/ros1 catkin_make -DBUILD_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=/opt/ros/$ROS1_DISTRO install
Having the ROS 1 System Handle installed. You can download it from the ROS1-SH dedicated repository into the
is-workspacewhere you have Integration Service installed:cd ~/is-workspace git clone https://github.com/eProsima/ROS1-SH.git src/ROS1-SHHaving ROS 2 (Foxy or superior) installed, along with the
example_interfacestypes package. To install it:apt install ros-$ROS2_DISTRO-example-interfacesHaving the ROS 2 System Handle installed. You can download it from the ROS2-SH dedicated repository into the
is-workspacewhere you have Integration Service installed:cd ~/is-workspace git clone https://github.com/eProsima/ROS2-SH.git src/ROS2-SH src/ros2-shHaving OpenSSL and WebSocket++ installed:
apt install libssl-dev libwebsocketpp-dev
Also, the Integration Service
WebSocketAddTwoIntsexample will be needed for the tutorial. This example application can be found in the main Integration Service repository, under the examples/utils/websocket/WebSocketAddTwoInts folder. To compile it, you can either compile the whole Integration Service project usingcolconwith the CMake flagBUILD_EXAMPLESenabled; or execute the following steps:cd ~/is-workspace/src/Integration-Service/examples/utils/websocket/WebSocketAddTwoInts mkdir build && cd build cmake .. -DBUILD_EXAMPLES=ON && make
Having the WebSocket System Handle installed. You can download it from the WebSocket-SH dedicated repository into the
is-workspacewhere you have Integration Service installed:cd ~/is-workspace git clone https://github.com/eProsima/WebSocket-SH.git src/WebSocket-SH
After you have everything correctly installed in your is-workspace, build the packages by running:
colcon build --cmake-args -DBUILD_EXAMPLES=ON -DMIX_ROS_PACKAGES="example_interfaces"
1.2.4.2. Deployment¶
Below we explain how to deploy a full example of this communication, calling the WebSocket service from each of the available clients.
Launch the WebSocket AddTwoInts server¶
To do so, open a terminal, go to the is-workspace folder and execute the following command:
cd ~/is-workspace
./build/WebSocketAddTwoInts/WebSocketAddTwoInts
The WebSocket server will start running, listening for incoming client connection petitions; after that, it will we able to dispatch service request petitions with a proper answer message.
Execute Integration Service¶
Open two terminals:
In the first terminal, source the ROS 1 installation and run the
roscore:source /opt/ros/$ROS1_DISTRO/setup.bash roscore
In the second terminal, go to the
is-workspacefolder, source the ROS 1, ROS 2 and local installations, and execute Integration Service with theintegration-servicecommand followed by the websocket_server__addtwoints.yaml configuration file located in thesrc/Integration-Service/examples/basicfolder.source /opt/ros/$ROS1_DISTRO/setup.bash source /opt/ros/$ROS2_DISTRO/setup.bash source install/setup.bash integration-service src/Integration-Service/examples/basic/websocket_server__addtwoints.yaml
Call the service from Fast DDS¶
In a new terminal, go to the is-workspace folder and execute the following command:
./build/DDSAddTwoInts/DDSAddTwoInts -m client -c <number_of_requests>
The DDSAddTwoInts example application will request to add two numbers an specific amount of times,
specified with the -c flag; if not present, ten requests will be performed by default.
For instance, if -c 4, should see something like this in your screen,
indicating that the WebSocket server is processing the requests:
AddTwoIntsService client running under DDS Domain ID: 0
AddTwoIntsService client performing 4 requests.
AddTwoIntsService client:
- Request 1 + 3
- Received response: 4
AddTwoIntsService client:
- Request 2 + 4
- Received response: 6
AddTwoIntsService client:
- Request 3 + 5
- Received response: 8
AddTwoIntsService client:
- Request 4 + 6
- Received response: 10
Call the service from ROS 1¶
In a new terminal, source your ROS 1 installation and invoke the service by executing the following instructions:
source /opt/ros/$ROS1_DISTRO/setup.bash
rosservice call /add_two_ints 3 4
You should receive the following output from the WebSocket server processing the petition:
sum: 7
Call the service from ROS 2¶
In a new terminal, source your ROS 2 installation and invoke the service by executing the following instruction:
source /opt/ros/$ROS2_DISTRO/setup.bash
ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 5, b: 17}"
You should receive the following output from the WebSocket server processing the petition:
waiting for service to become available...
requester: making request: example_interfaces.srv.AddTwoInts_Request(a=5, b=17)
response:
example_interfaces.srv.AddTwoInts_Response(sum=22)