From what I read in MQTT protocol message payload, it doesn't seem to support telling who is the publisher on a published message. But is it possible that a MQTT subscriber to know which publisher the message are from?
A 'msg.publisher' workaround maybe?
#!/usr/bin/env python
import mosquitto
def on_message(mosq, obj, msg):
print "Publisher: %s, Topic: %s, "Msg: %s" % (msg.publisher, msg.topic, msg.payload)
cli = mosquitto.Mosquitto()
cli.on_message = on_message
cli.connect("127.0.0.1", 1883, 60)
cli.subscribe("dns/all", 0)
cli.subscribe("nagios/#", 0)
while cli.loop() == 0:
pass
You're right, the MQTT specification has no field in the PUBLISH packet to specify which publisher a certain message comes from.
I can think of two possible "work-around" implementations:
1) Add the publisher information in the payload of the message. Application level parsing would allow you to retrieve the publisher ID from the message payload.
2) Add the publisher information in the topic. You could concoct a clever topic hierarchy with a level dedicated to the publisher.
For example: data/ Your subscriber could then subscribe to data/+ and parse the last level to retrieve the publisher ID.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With