Reputation: 645
I am trying to set up a MQTT system for a project that connects many clients.
These clients are of different type and may have different functionality. Some client-types have information - like temperature - which is being published but at the same time feature states that need to be set - like switches - which can be set by other clients.
So using MQTT I came up with the following concept:
The client publishes the informations it has - in the above example this would be the temperature, so like:
customer/group/device/temperature
When the client connects to a broker, in order to receive a state for the switch from different clients it subscribes to:
customer/group/device/switch
I have been using a websocket based solution so far but since MQTT is a famous protocol for IoT devices I was asking myself how to implement one to one communications between clients and came up with the above example of how I would do that.
Upvotes: 3
Views: 1934
Reputation: 4695
Your solution is pretty OK. For example, client A
can publish to client B
using topic customer/group/B/switch
, then client B
replies to customer/group/A/switch
.
As an alternative, client A
can publish to customer/group/B/switch
and specify in the payload the topic where it expects replies, for example
{
"data":"your message",
"reply-to":"customer/group/A/segretreply-fromB-1345313"
}
(just an example of JSON payload).
You can use timestamps, MACs or other sources of "uniqueness" to build the reply-to topic. Client A
could subscribe to different reply-to topics to mantain "separate" channels for each other client.
The real problem is that MQTT is not a one-to-one communication, each client can, potentially, subscribe to #
and receive all messages.
You should have some sort of authorization in your broker to decide whether a client can publish/subscribe to a topic or not.
I suggest reading this excellent article MQTT Security Fundamentals: Authorization. Then you can setup topics and authorization rules to better fit your requirements.
I'm not affiliated with HiveMQ. The link provided is just for educational purposes, their tutorial are great.
Upvotes: 2