Send data from Runtime to Editor

Hi,

I would like to push messages from the runtime to the editor and use notify (or other method) to display them.

I have stumbled across [CommsConnection(Global - Documentation) which takes about WebSocket between the runtime to the editor.
Sadly I wasn't able to figure out how to integrate with this module on the run time end.

I'm using NodeRed 2.2.2 as an embedding application.

Any ideas will be greatly appreciate.
Thanks.

Really, there is, and should be, a deliberate gap between the runtime and the Editor. It is the intent of the project to further separate them in the future.

I've not myself seen CommsConnection mentioned anywhere else so I can't comment about it specifically. However, the recommended approach of communication between a custom node's runtime and the Editor is to set up an API in the runtime and access that from the Editor using jQuery's AJAX methods or Fetch.

Obviously though, this is a pull rather than a push.

Hi @dannyhuly,

You can find an example in one of my nodes.

  1. On the server side (see here) I push my data into the Node-RED standard websocket:

    RED.comms.publish("xterm_shell", JSON.stringify( { terminalId: terminalId, content: "Pseudoterminal has been stopped (by " + reason + ")", type: "info" } ));
    
  2. On the client side (see here) I read that data, to use it in the flow editor:

    RED.comms.subscribe('xterm_shell', globalServerDataHandler);
    

Good luck with it!
Bart

It might be helpful here to have a comment from @knolleary as to whether that is an acceptable use of the API as I don't think it is documented and we are generally advised not to use undocumented features of the API?

Had I known about this, it would greatly simplify comms between the uibuilder back-end and Editor front-end.

I was able to achieve my desired behavior by using the plugin API (suggested in @BartButenaers example).

Thanks for the help.

Heres is a POC:

  1. When the runtime start it will publish a tick message every sec.
  2. When the editor loads in the browser it will subscribe to the tick messages and will console them.

package.json

{
    "name": "tick-to-editor",
    "node-red": {
        "nodes": {
            "tick-to-editor": "tick-to-editor.js"
        }
    }
}

tick-to-editor.js

module.exports = function (RED) {
    setInterval(() => {
        RED.comms.publish("tick", `${Date.now()}`);
    }, 1000);
}

tick-to-editor.html


<script type="text/javascript">
    RED.comms.subscribe('tick', function(eventName, msg) {
        console.log(msg);
    });
</script>
2 Likes

Why do you want to do this?

He Colin,
He is just sharing his POC which he used to test to push some dummy data across the websocket channel. I don't think this simple experiment will grow to become to become an alternative for Tik-Tok :wink:

1 Like

OK, understood.

1 Like

Final warnings on this.

The API is completely unpublished so there is no guarantee that this API will remain in the future nor that it might not change.

Also, be very careful on naming your events since again, there is no guarantee what names might be in use or may be used in the future.


And a final comment - a real shame you can't use the same approach to send messages from the Editor to the runtime :slight_smile:

1 Like

I will echo this point @dannyhuly - if you choose to go down this route, make sure you give your events names unique to your node. Do not use single word names.

2 Likes

@TotallyInformation you have a good point and I'm still on the fence about using the Cooms or just user WebSocket of my own. But I do like the Cooms solution because Its uses native RedNode infrastructure which simplify the code.

@knolleary thanks for the heads up. In my real code the message name is more complex and follows the NodeRed event name convention <COMPONENT_NAME>:<ACTION>.

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