Websockets are another useful communication capability that is built into Node-RED via the the websocket node. Websockets provide a duplex TCP connection and were designed to allow web browsers and servers to maintain a ‘backchannel’ that could be used to augment traditional HTTP interactions, allowing servers to update web pages without the client making a new pull request.
The websocket node comes in two flavours, input and output, allowing you to listen for incoming data (input) or to send (output) on a websocket. The output version is designed to check to see if the output payload originated at a websocket in a node, in which case it responds to the original sender. Otherwise it will broadcast the payload to all connected websockets.
In addition, both input and output websocket nodes can be configured as either server or client – in server mode they ‘listen on’ a URL, and in client mode they connect to a specified IP address.
To see how the websocket nodes work, you’ll use a public websockets echo server which runs on the public site: (https://www.websocket.org/echo.html).
Drag an inject, websocket in, websocket out and a debug node onto the workspace and connect them as shown in figure 3.18.
Figure 3.18 using websockets to communicate with a Node-RED flow
Configure the inject node to send a string payload of “Hello There” (Fig 3.19)
Figure 3.19 Configuring an inject node to send on a websocket
Configure the websocket nodes to connect to wss://echo.websocket.org as shown in Fig 3.20.
Figure 3.20 Configuring the websocket to send to a public echo server. Do the same for the websocket out node.
Deploy. When you click on the inject node you will see the message printed out as shown in Fig 3.21
Figure 3.21 Output from a websocket listening for incoming data
You can find the node-red description of this flow at:
Back to Lecture 3
© Lea, Blackstock, Calderon
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.