Dynamically change config parameter of a node

Introduction

The configuration of a node is set on node creation, so there is currently (<= 0.15) no general way to change the configuration dynamically.

So you have to solve the following problems:

Requirements

  1. Access a configuration from another node The function node is in a sandbox and you are only allowed to access RED.util.

  2. Activate new user configuration The node needs to be recreated like you do with the deployment. From the Editor this is controlled by a HTTP header to the Admin-API.

  3. Save your user configuration The flow contains the settings and need to be modified.

The following flow to change the settings in a flow dynamically over the admin API here: Please see the comment note, as you will need to setup the node id of the tcp node and the id of the flow is necessary. these are manual steps, as I could not found methods I can use to get them automatically. The flow uses JSONPath node to extract relevant content for debug message but this node can be removed safely.

Setup the flow

Import the following flow and read the comment nodes for further information. The example flow dynamically reconfigures a tcp-node but can be modified for other nodes as well.

example flow to dynamically reconfigure a tcp-node

Then you need to setup the flow-id in the injection flow. Set the Topic accordingly. You can get the flow-id by typing the following in the editor on the javascript console:

RED.workspaces.active()
"4c8b228.985ecdc"

You need the id of the tcp-out node as well and change it in the function node After deployment the tcp out node settings will be changed on click on inject-node in the editor

Notes

In any case this has an impact on the engine/runtime and should be used carefully.

You may take a look at node-red-contrib-config as well which can be used to extend the flow with better auto configuration possibilities as you can prepare flow and global context variables.

In the following image you see how in the tcp port increases on each click on "inject".

Log output with changed configuration settings

This flow is a summary of https://groups.google.com/forum/#!topic/node-red/kzyRVubbH60

[{"id":"258edea1.3673a2","type":"debug","z":"4c8b228.985ecdc","name":"","active":true,"console":"false","complete":"payload","x":1390,"y":540,"wires":[]},{"id":"20ce9864.df2028","type":"tcp out","z":"4c8b228.985ecdc","host":"localhost","port":102,"beserver":"client","base64":false,"end":false,"name":"tcp out, node id 20ce9864.df2028","x":920,"y":320,"wires":[]},{"id":"39dd64a2.77696c","type":"inject","z":"4c8b228.985ecdc","name":"Enter flow id in topic here !","topic":"4c8b228.985ecdc","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":410,"y":440,"wires":[["af7d7058.5db5c"]]},{"id":"af7d7058.5db5c","type":"http request","z":"4c8b228.985ecdc","name":"Load flow","method":"GET","ret":"obj","url":"http://localhost:1880/flow/{{topic}}","tls":"","x":640,"y":440,"wires":[["116be983.0bdd06"]]},{"id":"cf67e61a.80d488","type":"jsonpath","z":"4c8b228.985ecdc","expression":"$.nodes[?(@.type=='tcp out')]","split":false,"name":"Filter TCP Out node","x":1180,"y":540,"wires":[["258edea1.3673a2"]]},{"id":"116be983.0bdd06","type":"function","z":"4c8b228.985ecdc","name":"Modify tcpout settings","func":"// increment the connection port\n// of the tcp out node with id 20ce9864.df2028\n// increment is done on every input of inject\n// the flowid is set in the topic of the inject \n// node\n\nvar fn = msg.payload.nodes;\nvar sn = '20ce9864.df2028';\nmsg.headers = {\n    'Content-Type': 'application/json',\n    'Accept' : 'application/json'};\nmsg.url = \"http://localhost:1880/flow/\" + msg.topic;\nmsg.method =\"PUT\";\nfor(var i=0;i<fn.length;i++) {\n    if (fn[i].id == sn) {\n        fn[i].port = fn[i].port*1 + 1;\n        return msg;\n    }\n        \n}\n\n\nreturn msg;","outputs":1,"noerr":0,"x":880,"y":440,"wires":[["cf67e61a.80d488","c888e5aa.be7428"]]},{"id":"c888e5aa.be7428","type":"http request","z":"4c8b228.985ecdc","name":"Save flow","method":"use","ret":"obj","url":"","tls":"","x":1160,"y":440,"wires":[[]]},{"id":"19982e95.1c8141","type":"comment","z":"4c8b228.985ecdc","name":"Modify tcpout settings","info":"A Flow which modifies settings of another\nnode via the Admin API of NR (HTTP)\n\n1. You need to setup the flow id in the injection flow.\n2. Set the Topic accordingly. You can get the flow id by typing the following in the editor on the javascript console\n ```\nRED.workspaces.active()\n\"4c8b228.985ecdc\"\n```\n3. You need the id of the tcp out node as well and change it in the function node\n4. After deployment the tcp out node settings will be changed on click on inject in the editor\n \n\n\n\n","x":450,"y":300,"wires":[]}]

Flow Info

Created 8 years, 8 months ago
Rating: 4 1

Owner

Actions

Rate:

Node Types

Core
  • comment (x1)
  • debug (x1)
  • function (x1)
  • http request (x2)
  • inject (x1)
  • tcp out (x1)
Other

Tags

  • config
  • dynamic
  • reconfigure
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option