From NR function to node.js runtime

Hello,
I have developed a quite large NR function to syntactically analyse and verify the compliance to an agreed standard (see wis2-notification-message/WIS2_Message_Format_README.adoc at main · wmo-im/wis2-notification-message · GitHub) of a large geoJSON payloads. As the code might be useful to others, in my community, not using NR I would like to publish that code as a node.js runtime.
Is there guidelines to do that?
Typically stuff like msg.payload is not a node.js feature...
As this NR function is still code in progress, I would like to have a simple way to "export" that code to a node.js code. Is that doable?
Thanks!

You might take a look at my wiser heating node on GitHub. It consumes a wiser heating node.js package that contains the core processing. So you can extract the core processing to a dependency then require that dependency in your node and publish both on npmjs.org.

Absolutely :slight_smile:

Thanks.
Is this the one: GitHub - TotallyInformation/node-red-contrib-drayton-wiser: Monitor and control Drayton Wiser smart heating systems from Node-RED ?
If I understand your suggestion, it would be to get the runtime as a node.js, then create a NR consuming that runtime and from that creating a node that will in fact replace my current function node in NR?
I had a first look. Seems above my pay grade to get the logic...
I'll have to read (and understand!!) this Creating Nodes : Node-RED :slight_smile:
Correct?

Yep. The original package is there as well:
TotallyInformation, wiser repos

Not necessarily the whole runtime, the core processing. What would be useful as a standalone package or, as I did it, a standalone class.

That's the place to start on the node-red side. Try creating a simple test node first perhaps. For example, I created this one that started with a single simple node that helped me understand the different elements of a node and eventually added some others that let me experiment. TotallyInformation/node-red-contrib-jktesting: Test nodes for Node-RED for learning and experimenting, best practice, lots of hints and notes (github.com)

I had a deeper look. For what I would like to do, it seems quite an overkill.
I have one function. In this function, I am analysing a geoJSON received in msg.payload. Transforming this function in a full fledge node where there is nothing to configure, no documentation to write,... basically nothing but the function is interesting from a learning point of view, but otherwise, it seems like a sledgehammer to kill a fly.
My initial idea from a NR function node to a node.js runtime seems to be, IMHO, simpler. Replacing the msg.payload by a file read and changing the "return" part and done.
I was hoping that guidelines or good practices to do this would exist.

If you don't want the overheads of writing a node and the requirements are simple, the alternative is to take the core function in a package and consume it direct into a function node.

A google search "how to publish an npm package" would give you a lot of results.
Short version of the procedure.

Guys, thank you for your answers.

I have tried to write here a minimum viable flow explaining what I am trying to do.

[
    {
        "id": "70bd3722c7ac590c",
        "type": "function",
        "z": "23a4b7ed06023116",
        "name": "function 1",
        "func": "let topic = msg.topic;\nlet errors = [];\n\n// Check if topic is correct\n\nif (!(topic.match('^origin/a.*$') || topic.match('^cache/a.*$'))) reject(\"topic in incorrect\")\n\nif (errors.length != 0) {\n    let err = { \"errors\": errors };\n    msg.payload._comment = err;\n}\n\nreturn msg;\n\nfunction reject(reason) {\n    errors.push(reason);\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 780,
        "y": 540,
        "wires": [
            [
                "5023e9af514e9d21"
            ]
        ]
    },
    {
        "id": "5023e9af514e9d21",
        "type": "debug",
        "z": "23a4b7ed06023116",
        "name": "debug 25",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 960,
        "y": 540,
        "wires": []
    },
    {
        "id": "66a1a4c174b6431e",
        "type": "inject",
        "z": "23a4b7ed06023116",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "origin/a",
        "payload": "{ \"test\": 1 }",
        "payloadType": "json",
        "x": 590,
        "y": 540,
        "wires": [
            [
                "70bd3722c7ac590c"
            ]
        ]
    }
]

This is a valid nodered flow with a basic function.

If I want to transform this function into a standalone runtime, I need to change a few things.

Eg. initialising msg.payload with the geoJSON to test and msg.topic with some string.

My question is about the "best" way to do it. And if there is some documentation about such a thing.

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