I'm designing a system with many devices using MQTT to connect to a central broker.
There are some masters that can send requests to certain devices that are slaves. The request from one master is usually directed to one slave.
The topic for requests could be: mysystem/slaveId/req
, so the slaves can subscribe to this topic and master publishes to the same topic. Only the addressed slave will receive the request, as it should be.
My question is how to decide the topic for responses. I'm in doubt between: mysystem/slaveId/res/masterId
or simply mysystem/slaveId/res
.
In the first case, I should send masterId
to the slave in the payload of request, so the slave will be able to construct the response topic name. The answer will be received only by the master that send the request.
In the second case, all the active masters will receive the response, because the topic doesn't contain the masterId
. The master that send the request should detect its response by looking at a requestId
in the payload (that was sent in the request payload too).
I think the first choice is better, however the Device Shadow service of AWS uses topic names similar to the second choice. For example the device that replies to a get request publishes a message to $aws/things/myLightBulb/shadow/get/accepted
. So every devices will receive the message, not only the one that requested.
I think AWS makes good choices, so I am tempted to follow it... however I'm not sure if I understood what they do.