How to publish a message to a specific client in M

2019-06-28 05:11发布

问题:

Currently we have an app that controls multiple devices through MQTT. Each device subscribes to a unique topic which is named after their device ID. For example, device A has device ID 123 so it will subscribe to topic 123. Then if the app wants to publish control message to device A, then it will publish a topic named 123, which is the device ID of device A.

By doing this, if we have 1000 devices then we will have 1000 topics. This is a really bad design. So we are thinking that maybe we can publish a topic to a specific client by setting the client ID that will receive the topic since each client that connects to the broker will have to set a client ID. However, we did not find any method that allows publishing to a specific client. And it seems that MQTT doesn't handle such thing. It only publishes to clients subscribing to the same topic.

So, is there any other way that we can do to to achieve one topic, but still able to publish message to specific clients?

Thanks!

回答1:

There is no way to publish a message to a single subscriber at the MQTT protocol level.

One of the key tenants of a pub/sub system is to totally decouple the publisher from the subscribers, there is no way for a publisher to know if there are any subscribers to a given topic let alone target one specifically.

Using a topic for each device is not a problem as their is virtually no overhead in the broker for each topic. You can also use ACLs to ensure that each client can only subscribe their own topic (while still being able to publish to others if needed)

You could use a single topic that all clients subscribe to and encode the target device in the payload and have the device decide if the message is for it's self. The down side to this is that you can't apply ACLs to this model.



标签: mqtt