arduino-mqtt icon indicating copy to clipboard operation
arduino-mqtt copied to clipboard

onMessage called every time that publish to a topic

Open alirezaimi opened this issue 4 years ago • 1 comments

Hi every time that my esp board publish a message to mqtt server, the onMessage method called, why ?!

how can i detect who send the message and filter it ?

alirezaimi avatar Jul 29 '21 07:07 alirezaimi

Hi, I have same issue. when publish a message, onMessage called and QoS=-1. Ho to solve this?!

hgscania avatar May 11 '22 10:05 hgscania

Hi,

is it possible that you subscribed to the same topic you're publishing? That would certainly trigger such a feedback loop.

newHagen avatar Oct 10 '22 15:10 newHagen

Closing as probably solved.

256dpi avatar Feb 04 '23 10:02 256dpi

No ! How can this problem solved ?! it's open for solving and not solved !

alirezaimi avatar Feb 06 '23 11:02 alirezaimi

As @biteater pointed out, the only reason for the onMessage callback to be called is because of a subscription to a topic. And if you publish to the same topic you will also receive your own message. That is how MQTT works, there is nothing we can do about it.

256dpi avatar Feb 06 '23 11:02 256dpi

And this is not a loop ?! I must publish to a topic other than a topic that subscribe to it ? this is the right way ?

alirezaimi avatar Feb 06 '23 11:02 alirezaimi

Yes, exactly.

If you subscribe to 'foo' and you submit data to 'foo', this will trigger the onMessage-callback because a message has been published to this topic. The only way to prevent this is to not subscribe to the very same topic you are publishing to.

Subscribe to 'foo' and publish to 'bar' and everything is fine.

To stay within the same namespace for a device you might want to use topics like 'foo/status' to publish your data and subscribe to 'foo/cmd' to receive data or commands on your device.

newHagen avatar Feb 06 '23 11:02 newHagen