As a mqtt client connected to mosquitto is it possible to retrieve a list of client IDs who are also connected to the broker?

解决方案

method I: handle in client logic

as @user1048839 says, use client's LWT & online publish msg,

maintain client status on a custom topic.

subscript this topic & maintain client list self.

if pub retain msg, once sub will get the client list.

method II: change mosquitto broker code

official code not support online_list,

so I patched mosquitto 1.5.4, add 2 custom sys topic:

1. online list

mosquitto_sub -i DDD -v -t '$SYS/broker/chen_list'

$SYS/broker/chen_list

0 - CLOUD0_19108

1 - EEE

2 - DDD

2. online/offline event

mosquitto_sub -i DDD -v -t '$SYS/broker/chen_state/#'

$SYS/broker/chen_state/DDD 1

$SYS/broker/chen_state/EEE 1

$SYS/broker/chen_state/CLOUD0_19108 1

$SYS/broker/chen_state/EEE 0

$SYS/broker/chen_state/EEE 1

// if pub retain msg, sub this topic can get all clients online state (in payload).

test source code on github:

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐