Created
August 3, 2023 02:19
-
-
Save CaptainPalapa/88b601384eef38870a51836eef4f0cb4 to your computer and use it in GitHub Desktop.
Setting up websocket from dynamic URL in node-red
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": "0a8a0f7d0b4143ac", | |
"type": "tab", | |
"label": "MyBot - Socket", | |
"disabled": true, | |
"info": "", | |
"env": [] | |
}, | |
{ | |
"id": "http-request", | |
"type": "http request", | |
"z": "0a8a0f7d0b4143ac", | |
"name": "Slack: Get WS URL", | |
"method": "POST", | |
"paytoqs": "ignore", | |
"url": "https://slack.com/api/apps.connections.open", | |
"tls": "", | |
"persist": false, | |
"proxy": "", | |
"insecureHTTPParser": false, | |
"authType": "", | |
"senderr": false, | |
"headers": [], | |
"x": 160, | |
"y": 320, | |
"wires": [ | |
[ | |
"1895ea2c15e7bc86" | |
] | |
] | |
}, | |
{ | |
"id": "generate-websocket-url", | |
"type": "function", | |
"z": "0a8a0f7d0b4143ac", | |
"name": "Generate WebSocket URL", | |
"func": "const fnFlow = flow.get(\"fnFlow\");\nconst dynamicInfo = msg.payload;\nflow.set(\"websocket\", fnFlow.createWebsocket(dynamicInfo.url));\n\nreturn msg;", | |
"outputs": 1, | |
"noerr": 0, | |
"initialize": "", | |
"finalize": "", | |
"libs": [], | |
"x": 160, | |
"y": 440, | |
"wires": [ | |
[] | |
] | |
}, | |
{ | |
"id": "websocket", | |
"type": "function", | |
"z": "0a8a0f7d0b4143ac", | |
"name": "WebSocket Connection", | |
"func": "// Fetch the WebSocket connection\nconst ws = flow.get(\"websocket\")\n\n// Handle incoming WebSocket messages\nws.on(\"message\", function (message) {\n // Do something with the received message\n node.warn(JSON.parse(message));\n node.send({ payload: message });\n});\n\n// Handle WebSocket close event\nws.on(\"close\", function () {\n // Optionally, handle the close event\n node.warn(\"WebSocket connection closed.\");\n});\n\n// Handle WebSocket error event\nws.on(\"error\", function (error) {\n // Optionally, handle the error event\n node.error(\"WebSocket connection error: \" + error.message);\n});\n\n// Save the WebSocket connection in context to keep it open\ncontext.set(\"websocketConnection\", ws);\n\n// Do not send anything to the output\nreturn null;", | |
"outputs": 1, | |
"noerr": 0, | |
"initialize": "", | |
"finalize": "", | |
"libs": [], | |
"x": 470, | |
"y": 280, | |
"wires": [ | |
[ | |
"21210eee98092063" | |
] | |
] | |
}, | |
{ | |
"id": "b608c6dbebb2cfe0", | |
"type": "inject", | |
"z": "0a8a0f7d0b4143ac", | |
"name": "", | |
"props": [ | |
{ | |
"p": "payload" | |
}, | |
{ | |
"p": "topic", | |
"vt": "str" | |
} | |
], | |
"repeat": "", | |
"crontab": "", | |
"once": false, | |
"onceDelay": 0.1, | |
"topic": "", | |
"payload": "", | |
"payloadType": "date", | |
"x": 160, | |
"y": 200, | |
"wires": [ | |
[ | |
"ed36fbb616227da1" | |
] | |
] | |
}, | |
{ | |
"id": "ed36fbb616227da1", | |
"type": "change", | |
"z": "0a8a0f7d0b4143ac", | |
"name": "Set up Request", | |
"rules": [ | |
{ | |
"t": "set", | |
"p": "payload", | |
"pt": "msg", | |
"to": "{}", | |
"tot": "json" | |
}, | |
{ | |
"t": "set", | |
"p": "headers", | |
"pt": "msg", | |
"to": "{\"Content-type\":\"application/x-www-form-urlencoded\",\"Authorization\":\"Bearer [token here]\"}", | |
"tot": "json" | |
} | |
], | |
"action": "", | |
"property": "", | |
"from": "", | |
"to": "", | |
"reg": false, | |
"x": 160, | |
"y": 260, | |
"wires": [ | |
[ | |
"http-request" | |
] | |
] | |
}, | |
{ | |
"id": "6f518d25c5e6542f", | |
"type": "require", | |
"z": "0a8a0f7d0b4143ac", | |
"name": "require ws", | |
"module": "ws", | |
"field": "ws", | |
"fieldType": "flow", | |
"x": 110, | |
"y": 60, | |
"wires": [ | |
[ | |
"778b9695920de8dc" | |
] | |
] | |
}, | |
{ | |
"id": "1895ea2c15e7bc86", | |
"type": "json", | |
"z": "0a8a0f7d0b4143ac", | |
"name": "", | |
"property": "payload", | |
"action": "", | |
"pretty": false, | |
"x": 160, | |
"y": 380, | |
"wires": [ | |
[ | |
"generate-websocket-url" | |
] | |
] | |
}, | |
{ | |
"id": "778b9695920de8dc", | |
"type": "function", | |
"z": "0a8a0f7d0b4143ac", | |
"name": "Define flow function library...", | |
"func": "\nreturn msg;", | |
"outputs": 1, | |
"noerr": 0, | |
"initialize": "// Code added here will be run once\n// whenever the node is started.\nconst fnFlow = {\n \"test1\": function () {\n return \"Test 1!\";\n }\n , \"createWebsocket\": function (url) {\n // Variable \"ws\" is set in the \"require ws\" node\n var websocket = flow.get(\"ws\");\n // Establish the WebSocket connection\n return new websocket(url);\n }\n , \"getUsername\": function(userId){\n node.warn(`Not yet implemented....`);\n node.warn(`Finding name for ${userId}`);\n }\n};\nflow.set(\"fnFlow\", fnFlow);\n", | |
"finalize": "", | |
"libs": [], | |
"x": 360, | |
"y": 60, | |
"wires": [ | |
[] | |
] | |
}, | |
{ | |
"id": "21210eee98092063", | |
"type": "debug", | |
"z": "0a8a0f7d0b4143ac", | |
"name": "debug 1", | |
"active": true, | |
"tosidebar": true, | |
"console": false, | |
"tostatus": false, | |
"complete": "false", | |
"statusVal": "", | |
"statusType": "auto", | |
"x": 470, | |
"y": 360, | |
"wires": [] | |
}, | |
{ | |
"id": "2d1bf8eab4f9bcd4", | |
"type": "complete", | |
"z": "0a8a0f7d0b4143ac", | |
"name": "Websocket Ready", | |
"scope": [ | |
"generate-websocket-url" | |
], | |
"uncaught": false, | |
"x": 470, | |
"y": 200, | |
"wires": [ | |
[ | |
"websocket" | |
] | |
] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment