Allow to get comms connections by Module APIs to send notifications to client

Hello,

Is it possible to add getter for connections inside @node-red/runtime/lib/api/comms.js or is there some reason why it is not implemented?

The getter is as simple as getConnections: function() { return connections; }, inside module.exports.

With that feature now it is possible to send notifications to client while using Node-RED embeeded in express app.

const comms = RED.runtime.comms;
let connections = comms.getConnections();
connections.forEach(connection => {
        connection.send("notification/test123", {
            text: "Some error happened on server side",
            type: "error",
            timeout: 2000
        })
    })

As i searched through Node-RED code I did not find any way to do it without this modification, or maybe it is somehow possible?

If you want to emit an event to all connected editors you would do:

RED.events.emit("runtime-event", {
   id: "test123",
   payload: {
      text: "Some error happened on server side",
      type: "error",
      timeout: 2000
   }     
})

What's your scenario for wanting to do this? Node's shouldn't generally be sending the editor events like this.

1 Like

Ok, thank you!
I did not know that it is possible this way.

@bubus But I would like to know what your scenario is. If a node encounters an error it should be using the existing error logging mechanism that would allow the flow to handle the event. You cannot assume anyone has an editor open at that point in time.

It is not only for errors. In parallel with RED.log I want to inform user also by notification at some points that are triggered by RED.hooks, express middleware etc. -> for example when there is limit of used nodes on particular tenant and it is exceeded.

If they are more about 'edit time' notifications, then that is an appropriate use.

1 Like

Hello, just one more question is it possible to emit event only to specific user? Or to emit custom event to all users and catch it on client side?

I tried to catch custom event on client side with:

RED.comms.subscribe("customEvent/#")
RED.events.on("customEvent")

But could not manage to make it work. However if something like this is possible then in event handler (client side) I can do something like that:

if(RED.settings.get("user")?.username===msg.payload.username){
RED.notify(msg.payload.msg,{type:msg.payload.type}
}

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