Search code examples
clientmqtt

Get a list of connected client IDs from MQTT client


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


Solution

  • 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:

    4-online-list

    5-online-event