Why does this flow generate two mqttt messages for the topic?

Why does this flow generate two mqttt messages for the topic /eccentric/echo/data? The flow sends and receives /eccentric/echo/control, as expected, this in turn should trigger send and receive of /eccentric/echo/data, with one message as output, but two are received, and are they are not duplicates, given the time values are different in the payload. Something basic here, I am just missing?

Output...

3/28/2021, 7:13:49 PMnode: Message/eccentric/echo/data : msg : Object
object
topic: "/eccentric/echo/data"
payload: "{"time":1616983930593,"host":"eccentric"}"
qos: 0
retain: false
_msgid: "1f522bbe.b8f8d4"
3/28/2021, 7:13:50 PMnode: Message/eccentric/echo/data : msg : Object
object
topic: "/eccentric/echo/data"
payload: "{"time":1616983930582,"host":"eccentric"}"
qos: 0
retain: false
_msgid: "fc4c2120.b567c"

Flow...

[{"id":"16672c72.5fe314","type":"mqtt in","z":"c401a53d.622c8","name":"Broker (/+/echo/control)","topic":"/+/echo/control","qos":"2","datatype":"auto","broker":"98414585.30b018","x":120,"y":140,"wires":[["1c91c29.970263d"]]},{"id":"f2d91f9.fda72e","type":"mqtt out","z":"c401a53d.622c8","name":"Broker (/<Host>/echo/data)","topic":"","qos":"","retain":"","broker":"98414585.30b018","x":540,"y":140,"wires":[]},{"id":"1c91c29.970263d","type":"function","z":"c401a53d.622c8","name":"Receive","func":"const HOST = global.get('os').hostname().split('.')[0];\n\nmsg.payload = {\n    'time': (new Date()).getTime(),\n    'host': `${HOST}`\n}\n\nmsg.topic = `/${HOST}/echo/data`;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":140,"wires":[["f2d91f9.fda72e"]]},{"id":"b1ac118.75e957","type":"mqtt in","z":"c401a53d.622c8","name":"Broker (/+/echo/data)","topic":"/+/echo/data","qos":"2","datatype":"auto","broker":"98414585.30b018","x":120,"y":220,"wires":[["7056232f.8185a4"]]},{"id":"7056232f.8185a4","type":"debug","z":"c401a53d.622c8","name":"Message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":320,"y":220,"wires":[]},{"id":"ccea33ed.6c1328","type":"inject","z":"c401a53d.622c8","name":"Invoke","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":60,"wires":[["1ace4da5.86880a"]]},{"id":"1c68dc64.38cd74","type":"mqtt out","z":"c401a53d.622c8","name":"Broker (/<Host>/echo/control)","topic":"","qos":"","retain":"","broker":"98414585.30b018","x":450,"y":60,"wires":[]},{"id":"1ace4da5.86880a","type":"function","z":"c401a53d.622c8","name":"Send","func":"const HOST = global.get('os').hostname().split('.')[0];\n\nmsg.payload = {\n    'time': msg.payload,\n    'host': `${HOST}`\n}\n\nmsg.topic = `/${HOST}/echo/control`;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":230,"y":60,"wires":[["1c68dc64.38cd74"]]},{"id":"98414585.30b018","type":"mqtt-broker","name":"Broker","broker":"broker.dachshund-digital.org","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

I can't reproduce this, I'm getting one message from the data topic.

Do you publish to the topic on another location/flow tab/instance? Maybe accidentally copy/pasted parts of your flow?

I only get 1 message...

image

Possibly an issue with that broker and/or using a leading / in your topic names? (it is recommended you do NOT use a leading slash)

Try also restarting node-red? Also search your flows - do you subscribe to the same topics again elsewhere?

Not having a leading / is a best practice - that is not he same as saying using a leading / will introduce bugs.

Besides, this won't be a broker issue because you can see this is not two copies of the same message, rather two different messages - the time property is different in the payload.

I would suggest adding a Debug node alongside the MQTT Out node so you can confirm just how many messages you are actually publishing. It may be you are triggering it more than once somehow.

To clarify Nick. I suggested "Possibly" the use of a leading slash "and/or" the unknown broker (which wasnt specified - perhaps an old or odd broker with peculiar issues?) - just covering all bases.

Right - but equally, we can pretty much rule those things out instantly based on the information provided - the messages have different payloads. So there's no need to plant seeds of doubt.

I did miss the timestamps on the messages. After you pointed that out, it does look more likely the OP has duplicate code/flow or is publishing twice or some other oddity under the hood (like some external is watching and responding).

By the way, I have used leading slashes for some time... with no issues, but suspect I will change that as well. Hum, I didn't recall that being something considered a bad practice, when I started using MQTT. I do recall that $ is reserved, and using # is considered a bad use case, because topics should be specific, short and explicit. And if memory serves using # can have some performance impact as well.

Without any change to the environment, NR not restarted. A bit more information... if I break the flow, into 2 separate flows, adding an inject to the 2nd flow, the issue disappears for the 'data' flow. The 'control' sequence generates two messages. So I then changed the 'echo' to 'echoo' and the duplicate message issue resolved... so somewhere in the environment something is matching the 'control' topic. Should be able to spot it in the logging/debug details.

Thanks for the suggestions guys, greatly appreciated.

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