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

That would be useful :+1:

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

// 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
2 Likes

Working here ok Julian :+1:

mqtt

1 Like

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

1 Like

A bit of progress made on a visualisation:

image

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.

2 Likes

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.

3 Likes

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. :slight_smile:

More progress, actually a lot easier than expected:

Animation1

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

4 Likes

So sorry, this is something you wrote?

Wow!

I could only aspire to that.

(Where could I get it?)

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

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

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.

3 Likes

The other thread:

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