Editor-related events

Hello Everyone,

We selected Node-RED as a workflow designer and manager to embed in a web application. It's a potent and flexible tool, and I enjoy working on it.

We're embedding Node-RED in an express app that will expose the editor and the admin API to be consumed by other APIs on a separate express instance. The flow definitions, contexts, sessions and credentials will be saved to a database using a set of APIs. So far, I have managed to save everything by integrating a self-made storage module.

One of the issues I encountered was that I could not intercept the deletion of a workflow. I want to ensure that the stored flow definitions match what the users view in the editor. The saveFlows method in the storage module only passes the updated/new definitions as a parameter, and I'm unable to identify what has been deleted. Looking at the documentation I saw that I can subscribe to a flows:removed event. I tried, but it's not firing (among other events I tried, like flows:change and deploy).

 RED.init(super._server, redSettings);

  super.expressApp.use(redSettings.httpAdminRoot, RED.httpAdmin);
  super.expressApp.use(redSettings.httpNodeRoot, RED.httpNode);

  RED.events.on('flows:remove', (node) => {
    console.log(node);
  });
  RED.events.on('flows:change', (node) => {
    console.log('CHANGED!!!!');
  });

  RED.events.on('deploy', (node) => {
    console.log('deploy');
  });

As far as I understand, there are two types of events in Node-RED: runtime events and editor events. The events I'm trying to subscribe to are editor-related events, and for those, I need to integrate the @node-red/editor-api.

I got stuck there: I can't find an example, tutorial or documentation that will guide me in subscribing to these events. Also, GitHub Copilot seems confused when dealing with this topic :slight_smile:

Does someone have some resources or documentation to share, or can give me a brief heads-up on where to look to intercept those events?

Version of Node.JS: 20.11.0
Version of Node-RED: 4.0.5

Thanks, and Happy Coding.
Edoardo.

Hi and Welcome,
As you have noticed these events are for the editor - so your code should be into the editor.

A node developer would develop a plugin for this kind of job which I advise you to do - not sure modifying the core (editor-client) directly is the right long term solution.

The doc for these events:
https://nodered.org/docs/api/ui/events/

Plugin example:

1 Like

Sounds kind of interesting since, by the sounds of it, you're not using Node-RED for what it was meant for.

Having read your description a couple of times, I'm confused about what backend parts of Node-RED you're using and what frontend parts (i.e. you speak of editor events). Are you actually using the Node-RED backend to execute flows? Or are you just using the Node-RED editor as design tool and then saving the json files somewhere to disk?

I once spent some time working on a serverless Node-RED version that only runs in the browser. It was fun to dissect Node-RED into its frontend parts and backend parts. What came out of it was a complete Node-RED editor that runs in the browser and executes flows in the browser (but only a limited set of nodes). It gives people feel of what Node-RED is about without having to install it somewhere.

Thanks for your quick reply, GogoVega. It is very much appreciated. I looked at the linked project and tried to mimic a plugin in my environment. Everything seems set up correctly, but I can't plug the plugin into my Node-RED instance. I'll hammer it and search for documentation about developing a plugin, and if I cannot crack it in any way, I'll come back asking for help.
In time, I'll learn enough about Node-RED to return the favour.

Thanks for your reply and suggestion, Gregorius. The idea is to create a workflow service integrated into our application for drawing and executing workflows. Users will use Node-RED UI embedded in the admin web interface to draw workflows, and the main application's backend will delegate the execution of workflows to the workflow service that embeds Node-RED. Most of the nodes that will be available will be custom ones that will trigger on the backend of the main application server API and execute actions tied to the business logic. A little convoluted, but it will allow me to decouple the workflow definition and execution from the business logic, allowing me a reasonable degree of flexibility.
Also, I plan to save all the flow definitions and execution data using the main application server configuration endpoints (that will, in the end, save these data into a MongoDB database). This way, I can apply the same security and accessibility granted to other application configurations to the workflow definitions and runtime data.
I hope this answers your questions and clarifies the purpose of Node-RED in the general architecture.

You probably won't find anything because extending the editor is not something that is meant to be done.

Perhaps you find this bit of code already but try this:

RED.events.DEBUG = true

that should log all events to the browser console.