Is there any good tutorial on custom nodes?
I checked to docs and this is a very basic example to lowercase an input and send it to the output. I tried to keep everything in mind, but now I need a static TCP connection for an API and listen for input stream. Then sort out the messages and send them from different nodes.
There is ONE very old YouTube video about this but nothing else. Tried to find some similar projects.
Is there anything more? I need more input to start my project.
Posted Jun 27, 2021, 8:51 PM UTC
You know, most of what people think they need custom nodes for can actually be handled by nodes in the default palette...
For instance in your example, I'm noticing a TCP input connection (default pallette node), the output of which is routed in various ways within the flow, which can usually be done with a combination of switch and change nodes...
Sure, for an easy API, this would be the best way. I have a komplex Byte protocol on TCP with a lot of stuff an different data types. The most complex part for me: is the TCP connection part of my config node? How other output nodes can use the same client on an easy way?
Yes, the one time I always need to break out the function node is for bit-wise operations.
First of all, just as a note, there are TCP in/out nodes and HTML in/out nodes. If you're building an HTML-based api you should probably use the latter.
On to your question: I'm not sure if there are "configuration" nodes per se for the TCP and HTML in/out nodes. However, to "re-use" them you can simply have multiple output nodes (these are for your downstream processing) and wire those directly to a TCP out or HTML out.
So in other words, you have a single TCP out / HTML out with multiple upstream nodes wired to it. Your flow looks like:
TCP in / HTML in ---> (Processing steps, possibly involving multiple branching pathways) ---> TCP out / HTML out
Most nodes like this will indeed have the TCP socket in the configuration node. From there you can go different directions. Put all logic in the configuration node and just make calls from other nodes to it. Or let the config node only handle the connection and let the nodes handle the protocol part. Best thing is to start by thinking how you handle exceptions/connection drops/..., and that will already shape a lot where you put what functionality. I would say that hardest part is always the error handling, if you want to make something that is robust and won't give more issues than solutions.
And there is indeed a place where you can use the standards nodes, but sometimes a custom node is a good thing. It's easier to reuse and better for more complex logic. It all depends on the protocol. In the end everybody also uses the mqtt node instead of using the tcp node.
Great answer! Thank you so much!
Can you? Yes, but depending on the requirements, it's not always feasible to keep it that way. For a one-off setup? Sure, go ahead.
I went down the rabbit hole of making custom nodes for something that I absolutely could have hacked together in NodeRED if all I needed it for was one-off setup. After all, I'm in control of the firmware for the actual devices I'm dealing with, which communicate over MQTT in a format and structure I control, so sure, NodeRED would be perfectly capable of handling all this.
However, the entire point was to use NodeRED as a high-level UI for something that has to deployed in more than one location with completely different configurations, where simple custom business logic and behavior can be defined, and not necessarily by me or someone knowledgeable about the inner workings of the platform. You can do a lot with function blocks, but reusing them is very messy and makes the entire structure unclear. And then you're not even touching longer-term complications like the evolution of an API, dealing with versioning on that level, device management (discovery, replacement etc), ...
Looking back, I am amazed by how well writing my own nodes worked out, zero regrets here.
We might be arguing for the same thing here...
We should differentiate (a) the default palette nodes, (b) function blocks and (c) custom nodes.
The goal you describe here:
...which I agree is the purpose of Node-RED, is best serviced by maximizing (a) while minimizing (b) and (c). That's my main point.
The reason for my comment is that I see a number of people who come to this sub with a ream of JavaScript where they've basically duplicated the functionality of a
switchorchangenode, etc.Or conversely, they describe some basic functionality that might require the simple application of 2 or 3 default nodes, but want to be able to download some custom node that will do it all for them.
I (think I) agree with you that both of the latter approaches leads to Node-RED designs that are not particularly portable and will tend to be "brittle," i.e. break on minor changes.
:D The point of nodered for some people is to NOT have to DIY.
That's exactly my point. I'm telling OP they might not need a custom node because of the built-in capabilities of NR.
Oh, I was trying to make your point more poingant, not intending to counter it... sorry.
Yoinks. My bad. Carry on.