Accessing server side from client side

Hi, I'm new to Node Red an Node.js but I have got a couple of my own Nodes working now so understand the basics.

I have hit a challenge around showing a list of devices in the Node's Properties tab so that one can be selected for that Node. The code to do that is a separate module that is currently included into the server side code so how do I access it from the client side config code so that I can show the list?

I have looked at some examples and they are recreating some of the service access code using $.get or whatever, rather than the separate module. I don't like this very much - it's brittle and not DRY.

Ideally I would expose the separate module to the client side code through the Config Node but I'm not sure how to achieve this.

I am also confused that server side you use RED.nodes.getNode() to get a node, but this isn't available client side and you use RED.nodes.node() instead.

I guess the way to do this is for the client side/config code to include in my separate module and for me to instantiate it and pass the config token to it?

But for some reason myConfigNode.credentials.token doesn't appear to be set after getting myConfigNode using RED.nodes.node(node.configNode).

Some pointers on the best practise for this would be much appreciated, preferably with a link to a good example!

Thanks

The default example is the serialport node looking up what ports are available on the server side.. - on the client side the action is here https://github.com/node-red/node-red-nodes/blob/master/io/serialport/25-serial.html#L395 which calls the server side endpoint here - https://github.com/node-red/node-red-nodes/blob/master/io/serialport/25-serial.js#L461

1 Like

Ah yes - I see, an endpoint is created using 'RED.httpAdmin.get' for the client code to call.

Can you also offer any understanding on RED.nodes.getNode() vs RED.nodes.node()?

Are there any docs I can look at that would tell me more?

Thanks

An unintentional inconsistency since the very start of Node-RED.

The key thing to remember is the runtime and editor apis are different - you are accessing different things. In the runtime you will get the runtime node object. In the editor you'll get the node configuration.

3 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.