# How can I find out \*who\* is publishing a message in MQTT?

**URL:** <https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732>\
**Category:** General\
**Created:** [3 October 2023 02:21 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732 "2023-10-03T02:21:36Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [3 October 2023 14:35 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/21 "2023-10-03T14:35:37Z")

</div>

> [@TotallyInformation](#):
>
> Looks like I need to revisit some ideas I had previously about creating something in Node-RED. 🙂

That would be useful 👍

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 October 2023 19:27 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/22 "2023-10-03T19:27:25Z")

</div>

No visualisation yet but something to grab whole topic hierarchies - put this into a function node:

```auto
// Just a safety measure to prevent too much recursion
// Change if you have crazy deep topic hierarchies
const maxLevels = 9

function doTopic(topicsPart, keys, level = 0) {
    if (level > maxLevels) return // no infinite loops!

    let key
    if (Array.isArray(keys) && keys.length > 1) {
        // Grab the top level key (make sure it is a string)
        // MQTT topic parts could be number - don't want an accidental array
        key = `${keys.shift()}`

        // Make sure that the property exists as the next level
        if (!topicsPart[key]) topicsPart[key] = { _count: 1 }
        else topicsPart[key]._count++

        // Recurse to the next level
        doTopic(topicsPart[key], keys, level++)
    } else {
        // Must be at the end of the topic chain
        if (!Array.isArray) keys = [keys] // make sure it is an array

        // MQTT topic parts could be number
        key = `${keys[0]}` // make sure it is a string
        
        if (!topicsPart[key]) topicsPart[key] = { _count: 0 }
        topicsPart[key]._count++
        topicsPart[key]._value = msg.payload
        topicsPart[key]._userProperties = msg.userProperties
    }
}

const topics = flow.get('mqtt') ?? {}

const splitTopic = msg.topic.split('/')

doTopic(topics, splitTopic)

// flow.set('mqttTopics', topics)
flow.set('mqtt', topics)

// Don't do this with bazilions of topics!
return msg

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [3 October 2023 19:46 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/23 "2023-10-03T19:46:32Z")

</div>

Working here ok Julian 👍

![mqtt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f7b17ced4229108707dae359c07f9e22a932baa.jpeg)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 October 2023 19:57 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/24 "2023-10-03T19:57:01Z")

</div>

I just need to get round to creating a visual UI now. 🥴

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 October 2023 20:00 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/25 "2023-10-04T20:00:15Z")

</div>

A bit of progress made on a visualisation:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/2722443d2c3ef3313e9978578f932b9cd46aa03e.png)

This is just the tree view. The values will be moved to a details pane, out of the tree. Also, this is just the initial display, it is built from the cached data in Node-RED. A bit of tweaking of the CSS still needed.

The cache is triggered when you first load the page. I'm using UIBUILDER of course, so the cache replay is automatic.

I then need to do the individual record updates.

Here is the flow that generates everything.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/06f6923a50c81a8de749fd6e835ccb57e1cbb913.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [4 October 2023 21:08 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/26 "2023-10-04T21:08:35Z")

</div>

Looks good.  
I was thinking of doing something like that (but not made any progress).  
I intended to show a count of messages per topic rather than the most recent value because occasionally I suspect something has gone wrong and MQTT may be in a loop.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 October 2023 21:28 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/27 "2023-10-04T21:28:08Z")

</div>

> [@jbudd](#):
>
> I was thinking of doing something like that (but not made any progress

I'd been thinking about it on and off since MQTT v5 landed and the author of MQTT Explorer had announced that he wasn't continuing development.

This was the impetus I needed. Along with the fact that UIBUILDER now makes doing dynamic interfaces so much easier even without a framework. No need to mess with the weirdness of the DOM. 🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 October 2023 20:37 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/28 "2023-10-05T20:37:19Z")

</div>

More progress, actually a lot easier than expected:

![Animation1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/3237401517b1d6661b6ddda0196de58fe4df43ef.gif)

Less than 150 lines of front-end code including plenty of blank lines and comments.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [6 October 2023 03:43 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/29 "2023-10-06T03:43:23Z")

</div>

So sorry, this is something you wrote?

Wow!

I could only aspire to that.

(Where could I get it?)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 October 2023 10:50 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/30 "2023-10-06T10:50:26Z")

</div>

> [@Trying\_to\_learn](#):
>
> this is something you wrote?

Something I'm working on that was triggered by this thread. 🙂

> [@Trying\_to\_learn](#):
>
> I could only aspire to that.

Actually, thanks to the help that both Node-RED and UIBUILDER give, it isn't anywhere near as bad as it might appear. 🙂

> [@Trying\_to\_learn](#):
>
> Where could I get it?

It is still a work in progress - rather than pollute this thread even further, why don't I start a new thread. Eventually I will publish the full example in the Flows but people might be interested in following along.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 October 2023 11:12 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/31 "2023-10-06T11:12:10Z")

</div>

The other thread:

> [@🕸️ MQTT v5 Viewer - View a range of topics with values and extended MQTT v5 data](https://discourse.nodered.org/t/mqtt-v5-viewer-view-a-range-of-topics-with-values-and-extended-mqtt-v5-data/81825):
>
> I will publish this as a Flow once I'm happy that it is a bit more complete. However, as some have expressed an interest ... A [thread was raised on the forum recently](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732) about being able to track who/what updated an MQTT topic. It was suggested that MQTT v5 userProperties would be good for this. However, there really doesn't seem to be a decent viewer that includes MQTT v5. MQTT Explorer is kind of the gold standard viewer but it isn't being developed any more. So for some time I've been consideri…

---

<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:** [20 October 2023 11:12 UTC](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732/32 "2023-10-20T11:12:25Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/how-can-i-find-out-who-is-publishing-a-message-in-mqtt/81732.md?page=1)
