如何在Mosquitto MQTT中向特定客户端发布消息 [英] How to publish a message to a specific client in Mosquitto MQTT

查看:728
本文介绍了如何在Mosquitto MQTT中向特定客户端发布消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们有一个可以通过MQTT控制多个设备的应用程序.每个设备都订阅一个唯一的主题,该主题以其设备ID命名.例如,设备A的设备ID为123,因此它将订阅主题123.然后,如果该应用要向设备A发布控制消息,则它将发布名为123的主题,这是设备A的设备ID.

通过这样做,如果我们有1000个设备,那么我们将有1000个主题.这是一个非常糟糕的设计.因此,我们认为也许可以通过设置将接收该主题的客户端ID将主题发布到特定的客户端,因为连接到代理的每个客户端都必须设置一个客户端ID.但是,我们找不到任何允许发布到特定客户端的方法.看来MQTT不能处理这种事情.它仅发布给订阅同一主题的客户.

那么,我们还有其他方法可以实现一个主题,但仍然能够将消息发布给特定的客户吗?

谢谢!

解决方案

无法在MQTT协议级别将消息发布到单个订户.

发布/订阅系统的主要租户之一是使发布者与订阅者完全脱钩,发布者无法知道给定主题是否有任何订阅者,更不用说专门针对某个主题了.

为每个设备使用一个主题不是问题,因为在代理中,每个设备实际上没有任何开销.您还可以使用ACL来确保每个客户端只能订阅自己的主题(同时仍可以根据需要发布给其他人)

您可以使用所有客户端都订阅并在有效负载中对​​目标设备进行编码的单个主题,然后让设备确定消息是否是针对其自身的.不利的一面是您不能将ACL应用于此模型.

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!

解决方案

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.

这篇关于如何在Mosquitto MQTT中向特定客户端发布消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆