'Paho MQTT client: how to ignore messages published by myself?
My Paho MQTT client does the following:
- Subscribe to
mytopic/# - Do something
- Publish to
mytopic/#
Problem:
The published message in step 3 arrives at step 1. I'd like to avoid adding a sender-attribute to the payload.
Is there a proper way of ignoring self-published messages? Something like the following (pseudocode):
def on_message(self, client, userdata, message):
if client.id == message.sender_client_id: # Is there anything like the sender_client_id?
return
Any idea? Thanks!
Solution 1:[1]
As of the MQTT v5 spec you can tell the broker not to send your own messages back to you as part of the subscription message.
This removes the need to add the identifier so you can then choose to ignore it.
This does of course rely on both the broker and the MQTT client supporting MQTT v5
Solution 2:[2]
This logic should work:
- Assign an id to every client
- every client publish on mytopic/{id}
- every client sub to mytopic/#
ignore messages where message.topic starts with mytopic/{id}
Solution 3:[3]
def on_message(self, client, userdata, message):
if client.id == message.sender_client_id: # Is there anything like the sender_client_id?
return
In your pseudocode, you are asking for the client's identity but this is exactly opposite to the MQTT specification. In MQTT, two different clients are unaware of each other's identity, they only communicate via the MQTT broker by subscribing to the topics.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hardillb |
| Solution 2 | hardillb |
| Solution 3 | Rajat Bansal |
