1
votes

A question about MQTT publish and subscribe, different topics from same device. Is it considered bad form or practice in using different topics for publish and subscribe, or should the same topic be used and the payload delta used to qualify functionality not just via topic alone? In other words:

Method 1:
Device 1, Publish Topic A, Payload Null
Device 1, Subscribe Topic B, Payload Null
Method 2
Device 1, Publish Topic A, Payload 'some task'
Device 1, Subscribe Topic A, Payload 'some task task'

As far as I can tell, given the examples I have seen, method 1 is the best practice methodology. But has anyone even used method 2 or something like it?

for two different devices, method 2 would make sense, but on a single device, only method 1 seems appropriate. Not sure I see a scenario that a subscriber would subscribe to its own publisher for the exact same topic.

1
I'm sorry, but It is not clear what you are asking here. Can you give a real example?hardillb

1 Answers

0
votes

I have been using MQTT for many different things, including IPC for a multiprocessing program running on a single computer.

Method 1 is by far the best way to exploit MQTT's functionality. In a use case with multiple devices attaching to a server or servers, the security model built into the Mosquitto implementation and wildcard subscriptions take care of many things that would otherwise be difficult to implement.

For example, the topics can be broken into this kind of format:

devicesout/<username>
devicesin/<username>

Devices can be restricted to publishing on devicesin/<username> and subscribing to devicesout/<username> by the security configuration. The server connection(s) can be wildcarded in security, subscribe to /devicesin/#, and publish to any devicesout/<username>. When messages come in, the topic contains the device's <username>.