# Message tracing for beginners

**URL:** <https://discourse.nodered.org/t/message-tracing-for-beginners/92287>\
**Category:** Developing Nodes\
**Created:** [10 October 2024 10:58 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287 "2024-10-10T10:58:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [10 October 2024 10:58 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/1 "2024-10-10T10:58:01Z")

</div>

Hi there,

I want to trace messages through a flow as an aid for beginners to understand how messages are passed through NR flows. I have already done this on the [headless in-browser only NR](https://deadred.openmindmap.org/?t=0#group/841fe27cada39ec3) but now I want to build a plugin that does the same the serverful NR.

Having been pointed to the [hooks API](https://nodered.org/docs/api/hooks/messaging/) by Nick, I created a plugin with the following bit of code:

```auto
module.exports = function (RED) {
  function ConfigMsgTracerFunctionality(config) {
    RED.nodes.createNode(this, config)
    console.log( "REGISTERED TTPE MSGTTACER")

    RED.hooks.add("onReceive", (evnt) => {
      try {
        let nde = RED.nodes.getNode(evnt.destination.id)
        nde.status({ fill: "green", shape: "ring", text: "msg received" })
        setTimeout( () => {
          nde.status({})
        },1000)
      } catch(ex) {
        console.error(ex)
      }
    })
  }
  
  RED.nodes.registerType('MsgTracerCfg', ConfigMsgTracerFunctionality);
}

```

This works perfectly, just what I want except .... the hook is added each time the plugin is registered and that is unfortunately each time I do a deploy.

The question is: how do I prevent the hook from being added multiple times?

EDIT: what I also noticed that without the `try {...} catch { }`, I can actually prevent messages from being passed around in NR, i.e., an exception in the hook will prevent the router from continuing. That should probably not happen and instead the hook dispatcher should wrap those callbacks in it's own try/catch ....

/cc @knolleary

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [10 October 2024 12:06 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/2 "2024-10-10T12:06:04Z")

</div>

@marcus-j-davies has already created this kind of plugin 😉

Note: this kind of plugin should only be used for debugging because the hook takes a beating.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 October 2024 12:13 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/3 "2024-10-10T12:13:34Z")

</div>

@gregorius that isn't a plugin - that's a palette node.

Plugins are ways to add code to the runtime and/or editor without needing to register a node that the user has to add to their flows and deploy.

Here is how the Flow Debugger registers itself in the runtime:

> <https://github.com/node-red/node-red-debugger/blob/main/src/flow-debugger.ts>

And in the package.json file, it is identified under a `plugins` section:

> <https://github.com/node-red/node-red-debugger/blob/03ec572f899038a9386d774c7a86bbd8c7390441/package.json#L25-L27>

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [10 October 2024 12:27 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/4 "2024-10-10T12:27:29Z")

</div>

Cheers! That worked but I also did a remove of the hook just in case:

```auto
module.exports = function (RED) {
  function msgTracerOnReceiveHook(evnt) {
    try {
      let nde = RED.nodes.getNode(evnt.destination.id)
      nde.status({ fill: "green", shape: "ring", text: "msg received" })
      setTimeout(() => {
        nde.status({})
      }, 1000)  
    } catch (ex) {
      console.error(ex)
    }
  }

  RED.plugins.registerPlugin("msg-tracer-plugin", {
    onadd: () => {
      RED.hooks.remove('onReceive.msgTracer')
      RED.hooks.add("onReceive.msgTracer", msgTracerOnReceiveHook)
    }
  })
}

```

and in the package.json it's a plugin 👍

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [10 October 2024 12:28 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/5 "2024-10-10T12:28:30Z")

</div>

> [@GogoVega](#):
>
> @marcus-j-davies has already created this kind of plugin 😉

And now I know how he built it 😉

Thanks for the heads up but thanks to doing it myself, I learnt how to create a plugin - _properly_!

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [10 October 2024 16:03 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/6 "2024-10-10T16:03:47Z")

</div>

Mine was a plugin (in the end)  
that highlighted the wiring loom, as messages travelled along them.

It was a POC, never publicly released.

I still think it had massive value, but had concerns with UA performance.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [10 October 2024 16:25 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/7 "2024-10-10T16:25:01Z")

</div>

Hi @gregorius,  
You might also have a look at my [node-red-contrib-msg-profiler](https://github.com/bartbutenaers/node-red-contrib-msg-profiler) node, which does message tracing and profiling through a flow (based on hooks).  
P.S. it is _ **not** _ published on npm.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [10 October 2024 16:55 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/8 "2024-10-10T16:55:02Z")

</div>

> [@BartButenaers](#):
>
> You might also have a look at my [node-red-contrib-msg-profiler](https://github.com/bartbutenaers/node-red-contrib-msg-profiler)

looks good and something that will become more important - when more people start using NR!

My delving into the world of NR hooks happened because someone (on the [slack channel](https://node-red.slack.com/archives/C024S4W7M8D/p1728550983005319?thread_ts=1728546311.562409&cid=C024S4W7M8D)) was asking about having a monitor node to replay messages to test flows. But they also don't seem to have any financials to pay for the development of such a node ...

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 October 2024 17:05 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/9 "2024-10-10T17:05:03Z")

</div>

Just to repeat (and expand on) what I said in the very same slack conversation:

I strongly feel there is a place for _some_ form of these tools in the core of Node-RED; some basic capabilities that will greatly enhance the flow editing experience. Having lots of people create their own plugins is a great learning experience for them, but it doesn't moving the core Node-RED forward at all. We already have the Node-RED Flow Debugger plugin - a lot of these are natural extensions of what it does already - but we haven't had a single contribution to that plugin.

There's nothing wrong with that; its open source and no-one is under any obligation to contribute. It just puts the burden on a small few to try to move things forward when there's already a long wish list of items like this.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [10 October 2024 18:04 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/10 "2024-10-10T18:04:00Z")

</div>

I can understand your point but most of the stuff I'm doing is extremely niche, e.g., turning the editor into a dashboard or using it as a MindMap or using it for creating decorative flows (i.e. art!) or even converting flows into editable 3D flows. And I'd rather be doing that than creating MVPs for others for exactly zero financial benefit - personally I have exactly zero use-cases for a monitoring node.

When there is something that I think is useful, it remains in [pull request state](https://github.com/node-red/node-red/pull/4880) without feedback nor comment - so it doesn't seem to be important for others - fair enough, all good. If I do need that feature, then - thanks to the incredible wonderful flexibility of Node-RED - I can install my [own node](https://flowhub.org/f/390d041cf14946dc) & this is super simple (especially since I can use my own flowhub integration).

And similarly for msg tracing that is now part of the [0.8.1 version of the introspection package](https://flows.nodered.org/node/@gregoriusrippenstein/node-red-contrib-introspection) - there is now a checkbox that I can toggle message tracing on/off. If someone whats to put this in the debugger, then there is the [commit](https://github.com/gorenje/node-red-contrib-introspection/commit/1c736da91643a2ede48c8d702604e4b8413757bb) that basically describes the functionality.

But I have no desire to get involved in project management overhead for something that I do for fun and giggles.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [11 October 2024 12:27 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/11 "2024-10-11T12:27:52Z")

</div>

> [@gregorius](#):
>
> And similarly for msg tracing that is now part of the [0.8.1 version of the introspection package](https://flows.nodered.org/node/@gregoriusrippenstein/node-red-contrib-introspection) - there is now a checkbox that I can toggle message tracing on/off.

With version [0.8.4](https://www.npmjs.com/package/@gregoriusrippenstein/node-red-contrib-introspection/v/0.8.4) I've made this a little more useful by having a list of nodes and how many times they were called:

![msgtracer2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/328adef34559c8f38087ba052bc284604f5d940c.gif)

By clicking on the node, the node is highlighted and with a double-click the nodes edit panel is opened.

BTW yes this will slow down your editor if you have very busy flows and yes this shouldn't be done at home and kids: don't do this without parental consent. This is definitely a adults-only feature.

EDIT: Message tracing can be toggled on/off _without_ redeploying the flow and the hook is _removed_ in the backend when it's off - there is zero backend overhead when msg tracing is deactivated.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 October 2024 12:28 UTC](https://discourse.nodered.org/t/message-tracing-for-beginners/92287/12 "2024-10-25T12:28:50Z")

</div>

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