Partners who use EG series edge computing gateways often encounter the need to use WEBSOCKET to send and receive information, WEBSOCKET is a protocol on TCP that can help users realize information push, using WEBSOCKET needs to create a connection first, and then the client and the server will be able to send data at any time, and the server can actively push the data to the client without the client constantly sending requests, realizing true two-way communication. This protocol is often used in real-time data monitoring in the Internet of Things (IoT), which allows IoT applications to be more interactive and real-time.
Today, let’s learn how the EG Series Edge Computing Gateway uses WEBSOCKET to send and receive information! Before we get started, we need to make sure that we have done the preparatory work, including the connection and configuration of the edge gateway, and familiarize ourselves with the Node-RED.
Overview
The WEBSOCKET function is divided into two nodes according to the data flow direction : WEBSOCKET IN indicates the data received by the gateway and WEBSOCKET OUT indicates the data sent by the gateway.
Secondly, the listening mode gateway acts as the WEBSOCKET server and the connection mode gateway acts as the WEBSOCKET client.
Input
msg.payload
# By default, msg.payload will be sent through the set WEBSOCKET. If the message arriving at this node comes from the WEBSOCKET IN node, the message will be sent back to the original client. Otherwise, the message will be broadcast to all clients.
Output
msg.payload
# By default, data received from WEBSOCKET is present in msg.payload
Functions
1. Listening
Listening mode, WEBSOCKET works in server mode, it will create WEBSOCKET server according to the specified path.
2. Connection
In connection mode, WEBSOCKET works in client mode and initiates a WEBSOCKET connection to the set URL.
Example
1. WEBSOCKET builds Server to connect the Client.
[{"id":"13410716.69c9d9","type":"websocket in","z":"b5e5e7b9b71494ea","name":"","server":"89db22b6.9aa36","client":"","x":500,"y":380,"wires":[["c2541f10.59544"]]},{"id":"c2541f10.59544","type":"debug","z":"b5e5e7b9b71494ea","name":"调试","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":380,"wires":[]},{"id":"6788839e.04576c","type":"inject","z":"b5e5e7b9b71494ea","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hello world","payloadType":"str","x":340,"y":320,"wires":[["438c232a.06c2cc"]]},{"id":"438c232a.06c2cc","type":"websocket out","z":"b5e5e7b9b71494ea","name":"","server":"","client":"63620788.bda128","x":560,"y":320,"wires":[]},{"id":"c88f97a9.4410f8","type":"comment","z":"b5e5e7b9b71494ea","name":"WEBSOCKET自建Server和Client互连","info":"","x":410,"y":260,"wires":[]},{"id":"759c0b2b.8a0484","type":"websocket in","z":"b5e5e7b9b71494ea","name":"","server":"","client":"1d80bd86.93f372","x":560,"y":500,"wires":[["1f7a7454.cb65ec"]]},{"id":"1f7a7454.cb65ec","type":"debug","z":"b5e5e7b9b71494ea","name":"调试","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":500,"wires":[]},{"id":"aa2fe781.e92b28","type":"inject","z":"b5e5e7b9b71494ea","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"goodbye","payloadType":"str","x":340,"y":440,"wires":[["f8bdbc9b.d82dd"]]},{"id":"f8bdbc9b.d82dd","type":"websocket out","z":"b5e5e7b9b71494ea","name":"","server":"40bd4295.3e4ecc","client":"","x":500,"y":440,"wires":[]},{"id":"89db22b6.9aa36","type":"websocket-listener","path":"/ws1","wholemsg":"false"},{"id":"63620788.bda128","type":"websocket-client","path":"ws://localhost:1880/ws1","tls":"","wholemsg":"false","hb":"0"},{"id":"1d80bd86.93f372","type":"websocket-client","path":"ws://localhost:1880/ws2","tls":"","wholemsg":"false"},{"id":"40bd4295.3e4ecc","type":"websocket-listener","path":"/ws2","wholemsg":"false"}]
2. WEBSOCKET builds Server to send and receive information.
[{"id":"9c86d286f135539e","type":"group","z":"b5e5e7b9b71494ea","name":"服务端-数据原路返回","style":{"label":true},"nodes":["4cb19ae6d5bc97bb","06f56bbc8337db06","587f22bd305d76ce"],"x":314,"y":219,"w":492,"h":82},{"id":"4cb19ae6d5bc97bb","type":"websocket in","z":"b5e5e7b9b71494ea","g":"9c86d286f135539e","name":"","server":"89db22b6.9aa36","client":"","x":400,"y":260,"wires":[["06f56bbc8337db06"]]},{"id":"06f56bbc8337db06","type":"function","z":"b5e5e7b9b71494ea","g":"9c86d286f135539e","name":"函数计算 1","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":260,"wires":[["587f22bd305d76ce"]]},{"id":"587f22bd305d76ce","type":"websocket out","z":"b5e5e7b9b71494ea","g":"9c86d286f135539e","name":"","server":"89db22b6.9aa36","client":"","x":720,"y":260,"wires":[]},{"id":"89db22b6.9aa36","type":"websocket-listener","path":"/ws1","wholemsg":"false"},{"id":"d07e89f720b33486","type":"group","z":"b5e5e7b9b71494ea","name":"客户端","style":{"label":true},"nodes":["1af13bea201b6062","c327cadb0afa0aef","33bf2db84056de7c","cc47f6b7286a57d1"],"x":334,"y":399,"w":472,"h":142},{"id":"1af13bea201b6062","type":"websocket out","z":"b5e5e7b9b71494ea","g":"d07e89f720b33486","name":"","server":"","client":"63620788.bda128","x":660,"y":440,"wires":[]},{"id":"c327cadb0afa0aef","type":"inject","z":"b5e5e7b9b71494ea","g":"d07e89f720b33486","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":430,"y":440,"wires":[["1af13bea201b6062"]]},{"id":"33bf2db84056de7c","type":"websocket in","z":"b5e5e7b9b71494ea","g":"d07e89f720b33486","name":"","server":"","client":"63620788.bda128","x":480,"y":500,"wires":[["cc47f6b7286a57d1"]]},{"id":"cc47f6b7286a57d1","type":"debug","z":"b5e5e7b9b71494ea","g":"d07e89f720b33486","name":"调试 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":500,"wires":[]},{"id":"63620788.bda128","type":"websocket-client","path":"ws://localhost:1880/ws1","tls":"","wholemsg":"false","hb":"0"}]
All is today’s sharing, anyway I hope that through this article, readers can be clear about how to use WEBSOCKET to send and receive messages on EG Series Edge Gateways.
Zongheng Intelligence Control Technology Co., Ltd. combines traditional industry infrastructure with new technologies such as IoT, edge computing, AI, and provides the edge gateway, IOT data center, Industrial Computer, remote IO, AI edge box, and other products and solutions for the industry digital transformation integrators.