Websocket connections inside a function node

kitori
Tue, 23 Apr 2024 12:57:35 +0000
General

Is it possible to use a Websocket connection inside a function node?
Native Websocket dont work:
WebSocket - Web APIs | MDN ,

This as well:

GitHub

GitHub - websockets/ws: Simple to use, blazing fast and thoroughly tested...

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js - websockets/ws

! function node "ws" will crash node red

[{"id":"1806fbe5e8308245","type":"function","z":"527fe7341bb5fe46","name":"WebSocket","func":"// Create WebSocket connection.\nconst socket = new WebSocket(\"ws://localhost:8080\");\n\n// Listen for messages\nsocket.addEventListener(\"message\", (event) => {\n  node.send(\"Message from server \", event.data);\n});\n","outputs":1,"timeout":0,"noerr":2,"initialize":"","finalize":"","libs":[],"x":930,"y":940,"wires":[["a3401fa602e2acc3"]]},{"id":"a3401fa602e2acc3","type":"debug","z":"527fe7341bb5fe46","name":"debug 16","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1100,"y":940,"wires":[]},{"id":"751f8cd47462a495","type":"function","z":"527fe7341bb5fe46","name":"ws","func":"// Create WebSocket connection.\nconst socket = new WebSocket(\"ws://localhost:8080\");\n\n// Listen for messages\nsocket.addEventListener(\"message\", (event) => {\n  node.send(\"Message from server \", event.data);\n});\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"WebSocket","module":"ws"}],"x":950,"y":980,"wires":[["a3401fa602e2acc3"]]},{"id":"4b7297e5521cfb6f","type":"inject","z":"527fe7341bb5fe46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":800,"y":980,"wires":[["751f8cd47462a495"]]}]

Similar question, same problem

Using Dynamic URL websocket node General

I would like to use a websocket interface in node-red. For this I need a dynamic url, because I need a token for access. This changes every 24 hours. Are there approaches to use a dynamic url for the websocket node? [FB_IMG_1707503199513]

7 posts - 4 participants