node-red-dashboard icon indicating copy to clipboard operation
node-red-dashboard copied to clipboard

Overriding the default onInput handler

Open bartbutenaers opened this issue 1 year ago • 2 comments

Hi guys,

I have a noob question about overriding the default oninput handler (see documentation).
I 'think' I do it similar as in the ui-chart node:

        // server-side event handlers
        const evts = {
            onInput: function (msg, send, done) {
                // My custom onInput handler
                ...
            },
            onSocket: {
               ...
            },
            beforeSend: function (msg) {
                ...
            }
        }

        // inform the dashboard UI that we are adding this node
        if (group) {
            group.register(node, config, evts)

But when I inject a message into my node, this happens:

  1. The message arrives in my Vue frontend code
  2. The beforeSend of my node is called
  3. My custom onInput handler above is being called

I had assumed that only 3 would happen, and not 1 and 2. Because 1 and 2 are triggered by the default onInput handler (which I have overridden)? Or perhaps I have not interpreted the documentation correctly?

Based on the msg.topic:

  • Some messages only need server side processing, so they don't need to be send to the frontend
  • Some messages need to be send to the frontend.

Can you please give me any tips about how I could achieve something like that.

Thanks!! Bart

bartbutenaers avatar Mar 19 '24 21:03 bartbutenaers

The beforeSend is poorly named- it's actually beforeSendToClient, rather than the send() within the context of Node-RED. So, I would expect the order to be: 2, 3, 1 as you've listed it.

joepavitt avatar Mar 20 '24 16:03 joepavitt

Hi @joepavitt,

Ah yes I see. In the old dashboard you had:

  • beforeEmit which was called before the message was emitted via websocket to the client, so you could change it if required.
  • 'beforeSend' which was called before the message was send on the output into the flow.

Now the order seems to be:

  1. beforeSend
  2. onInput
  3. msg-input in the Vue frontend
  4. beforeSend

The last one is a bit confusing for me. Seems the beforeSend is called a second time here:

image

How do we need to take into account that this function is being called in two different locations (i.e. before and after the frontend)? Because if I understand correctly, this function is not only used to preprocess messages before being send to the frontend?

And sorry but I still am not very sure what is the best way to prevent some messages (e.g. with topic xxx) to be pushed to the Vue frontend.

Thanks!!

bartbutenaers avatar Mar 23 '24 07:03 bartbutenaers