# How Change node properties with a function block

**URL:** <https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183>\
**Category:** General\
**Created:** [9 August 2018 13:21 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183 "2018-08-09T13:21:43Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![glconceicao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/glconceicao/32/1657_2.png) [@glconceicao](https://discourse.nodered.org/u/glconceicao)\
**Post date:** [9 August 2018 13:21 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/1 "2018-08-09T13:21:43Z")

</div>

I want change URL of WebSocket node with a function block.

my URL on WebSocket was variable, and necessary to change, it is possible?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/91588c06fbfff09958eb86e27b152a38fb3ee9af.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [13 August 2018 23:15 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/2 "2018-08-13T23:15:06Z")

</div>

You cannot dynamically change the the url of a websocket endpoint, it is only examined at Node-RED startup.

There really aren't many cases where you would actually want this anyway so you might want to explain why you are wanting to do this, perhaps we can think of alternative approaches.

---

<div class="post-metadata">

**Author:** ![glconceicao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/glconceicao/32/1657_2.png) [@glconceicao](https://discourse.nodered.org/u/glconceicao)\
**Post date:** [14 August 2018 10:35 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/3 "2018-08-14T10:35:33Z")

</div>

ok.

i'm monting a equipament that have a HMI and a raspberry, the HMI make de connection with the raspberry via modbus, and read/write variables the system, and raspberry make the connection with the plataform. but the URL must be change, because if i provide my solution for other people, these people must to put on other url of your platoform.  
i provided variables on the HMI for change the URL, and the raspberry read this information and change de url on websocket, but before change the url, the raspberry open the web socket, generating error, because the url is not define.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [14 August 2018 21:18 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/4 "2018-08-14T21:18:07Z")

</div>

If I've read this right, you want to allow people to change the url _before_ they start the Node-RED based system. Not dynamically. That's because they just need to be able to ensure that the url doesn't clash on their system.

Actually, even that isn't an issue unless you are giving them a FLOW to use in their own instance of Node-RED since they must choose an unused PORT for Node-RED so they would never have clashing URL's in any case.

If you are giving them a flow to use and they may have created their own URL's in their instance of Node-RED, what you need to do is allow a global configuration to be set in their `settings.js`. To do that, you will need a custom node I think. However, someone got there already since node-red-contrib-uibuilder allows the user to configure a URL and creates a websocket connection on a matching path. While this is generally designed to be used with front-end code and my uibuilderfe.js library, it wouldn't be hard to adjust for other uses.

---

<div class="post-metadata">

**Author:** ![glconceicao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/glconceicao/32/1657_2.png) [@glconceicao](https://discourse.nodered.org/u/glconceicao)\
**Post date:** [17 August 2018 18:04 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/5 "2018-08-17T18:04:13Z")

</div>

I solved this problem.

the firt thing i did, was on my dashboard, i create one variable with url on JSON file.

then on node of websocket i call this file, selecting the variable of url.

---

<div class="post-metadata">

**Author:** ![odys](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/odys/32/10626_2.png) [@odys](https://discourse.nodered.org/u/odys)\
**Post date:** [3 August 2019 10:19 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/6 "2019-08-03T10:19:21Z")

</div>

Hey @TotallyInformation,

I am new node-red user (loving it so far) and I want to use it to build a simple PoC for a device service for the EdgeX project by the linux foundation.

My issue is that I need the endpoints of the device service to include a `device_id` like `{.../device/{device_id}/command/:command`. This`device_id` is provisioned to the device at the bootstraping phase, so I can't hardcode it.

My first solution is to use ${env\_var} by provisioning the device, setting the var and the restarting the node-red instance. I can't say that I like this approach. Do you think that there is a better alternative?  
(I have searched everywhere for a solution, results are very sparse).

Thank you so much for your time and effort. I really appreciate it.

Best,  
Odysseas

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 August 2019 22:11 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/7 "2019-08-03T22:11:48Z")

</div>

It is generally better to start a new thread for a new question, you will normally get better responses.

In this case, why not make the device id a variable like the command?

Also, if the device ID's are available before Node-RED starts up, you can set them into global variables in settings.js.

---

<div class="post-metadata">

**Author:** ![odys](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/odys/32/10626_2.png) [@odys](https://discourse.nodered.org/u/odys)\
**Post date:** [4 August 2019 11:06 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/8 "2019-08-04T11:06:15Z")

</div>

Hey @TotallyInformation,

Thank you so much for your time.

So I could use a id\_variable and check the id\_variable in a function node, because in case the ID is wrong, it should return an error. This would be the way to go?

Moreover, should I wanted to use env\_var it would be /device/${device\_id}/.... and the env would be set in the settings.js, am I right?

Thanks again for the thorough response.

Cheers!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 August 2019 13:17 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/9 "2019-08-04T13:17:42Z")

</div>

> [@odys](#):
>
> endpoints of the device service

Let's just back-track a second. When you say the above, exactly what are you trying to do here? Is this in Node-RED (e.g. you are using the http-in node) or on your devices (e.g. you are using the http-request node)?

One point about using env variables inside node settings - generally I believe that the env variable has to be the whole setting entry and not just part of it.

---

<div class="post-metadata">

**Author:** ![odys](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/odys/32/10626_2.png) [@odys](https://discourse.nodered.org/u/odys)\
**Post date:** [4 August 2019 13:32 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/10 "2019-08-04T13:32:04Z")

</div>

Hey,

What I want is to set the http in endpoint dynamically, after the start of the node-red services (and thus the flows).

In detail, the service during bootstraping will request an id from a central registry, and that id will be used in the endpoint url.

Thanks!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 August 2019 13:40 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/11 "2019-08-04T13:40:56Z")

</div>

> [@odys](#):
>
> What I want is to set the http in endpoint dynamically

OK, so what is stopping using the following URL definition:

```auto
.../device/:device_id/command/:command

```

Now you have 2 variables to work with in Node-RED and your devices can use _any_ device ID.

You will likely want to add some logic to restrict what ID's are valid. For that, since you know what they are before Node-RED starts up, I would create a JSON file (containing an array of ID's) or even a simple text file with one ID per line. You can then use that in Node-RED, read it on startup (either a flow on the first tab or add some logic to your settings.js file) into a global variable. Then you can use that to only allow valid ID's through.

Or have I misunderstood something?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 August 2019 13:51 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/12 "2019-08-04T13:51:44Z")

</div>

beat me to it 🙂

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/afa93a2b70ecf52c0d4a60c91a686283e75b6728.png)

You can access the variables in `msg.req.params.id` and `msg.req.params.command`

sample flow. (triggered by browsing [http://localhost:1880/device/1/command/start](http://localhost:1880/device/1/command/start) ..

```auto
[{"id":"99225691.2177d8","type":"http in","z":"56fa9896.d15ed8","name":"","url":"/device/:id/command/:command","method":"get","upload":false,"swaggerDoc":"","x":260,"y":140,"wires":[["4d5394ac.ec39bc"]]},{"id":"938c0707.72d728","type":"debug","z":"56fa9896.d15ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":450,"y":200,"wires":[]},{"id":"4d5394ac.ec39bc","type":"change","z":"56fa9896.d15ed8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"{id:\" & req.params.id & \",command:\\\"\" & req.params.command & \"\\\"}\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":200,"wires":[["938c0707.72d728","495784ba.799c6c"]]},{"id":"495784ba.799c6c","type":"http response","z":"56fa9896.d15ed8","name":"","statusCode":"","headers":{},"x":310,"y":260,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 August 2019 13:54 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/13 "2019-08-04T13:54:33Z")

</div>

> [@Steve-Mcl](#):
>
> beat me to it

makes a change 😉

---

<div class="post-metadata">

**Author:** ![odys](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/odys/32/10626_2.png) [@odys](https://discourse.nodered.org/u/odys)\
**Post date:** [4 August 2019 18:57 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/14 "2019-08-04T18:57:26Z")

</div>

Wow guys @Steve-Mcl, @TotallyInformation

I don't know what to say. I have been interacting with various communities these days as I am making a PoC for my Master's thesis, but this has been by far the quickest and most helpful reply. I am really humbled for your time.

I will follow your advice and use a function node to filter out the right ID. In essence, during a bootstraping flow, the device receives an ID which is then used as a global.variable in the API endpoints flow.

Thank you guys!

Btw, it's possible to use a http request node to upload a file using POST, right?

Cheers!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 August 2019 19:03 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/15 "2019-08-04T19:03:47Z")

</div>

> [@odys](#):
>
> Btw, it's possible to use a http request node to upload a file using POST, right?

[https://flows.nodered.org/?term=upload&type=flow&num\_pages=1](https://flows.nodered.org/?term=upload&type=flow&num_pages=1)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 August 2019 19:15 UTC](https://discourse.nodered.org/t/how-change-node-properties-with-a-function-block/2183/16 "2019-08-04T19:15:56Z")

</div>

> [@odys](#):
>
> Btw, it's possible to use a http request node to upload a file using POST, right?

Yes it is... No need to use any contrib nodes from the library. The sidebar help gives you the basic message structure needed at the bottom
