Mqtt How a client can get to know that another client is connected or not

旧时模样 提交于 2019-11-29 18:12:07

The short answer is you don't (at a protocol level).

Publishers and subscribers are totally unaware of each other, messages are sent to topics not to specific subscribing clients.

The slightly longer version:

You could do something with retained messages and LWT (Last Will & Testament)

e.g.

Each client publishes a retained message to the following topic

online/<clientID>

With a payload of true when it connects. It also sets up LWT that will publish a payload of false if the client is disconnected by a network fault. If the client cleanly disconnects then it will need to publish the false payload as it's last action before going offline.

To tell if client is online you subscribe to the wildcard topic of online/+ and then check the last segment of the topic for clientid and the payload for the state.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!