Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MQTT subscriber to know who is the publisher

Tags:

mqtt

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
like image 532
Dennis Avatar asked Mar 14 '23 18:03

Dennis


1 Answers

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.

like image 111
Pierre-Luc Avatar answered Apr 09 '23 18:04

Pierre-Luc