Get list of connected client identifiers from MQTT client

How can an mqtt client connected to mosquitto get a list of client identifiers that are also connected to a broker?

+7
source share
6 answers

method I: process in client logic

as @ user1048839 says, use client-side LWT and online posting, maintain client status in a custom topic. Sign this topic and maintain a customer list of your own.

if pub retain message, one day sub will receive a list of clients.

Method II: change the mosquito broker code

the official code does not support online_list ,
so I patched mosquitto 1.5.4 by adding 2 custom sys themes:

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 the pub retain message, under this section you can get the status of all clients on the network (in the payload).

source on github:

4-online listing

5-online event

+1
source

one way to implement this is to allow the client to publish a message with the subject β€œstatus / client-identifier” and β€œ1” each time a broker connects and with a payload of β€œ0” when it disconnects.

Then, on the server side (broker), configure another client, subscribe to the topic "status / #" when he receives a message like this, save the client ID and payload (connected or not) to the database.

then you can read the database to know exactly which client is online or offline.

+8
source

Not.

It might be better to discuss this on the mosquitto mailing list: https://launchpad.net/~mqtt-users

+3
source

A good job for this is to have clients (if possible) identify Last will and testament (LWT). Your server will subscribe to a special topic where LWT will be published and will count all customers online if they do not publish this topic.

MQTT What is the purpose or use of Last Will Testament?

+2
source

Perhaps you can get this information using the BASH netstat, grep and awk commands if necessary. If Mosquitto uses port 1883, then the following will tell you what you want, I believe:

sudo netstat | grep: 1883

+1
source

So now I created a workaround using a PHP script: it starts the mosquitto broker, reads the output, and if someone connects or disconnects, it sends an xml string with the connected clients to the broker. (the posted code is a bit simplified, as I adidionally query the database for more information about the user)

 <?php require('SAM/php_sam.php'); if (!$handle = popen('mosquitto 2>&1', 'r')) { die('could not start mosquitto'); } function usersToXML($users) { $xml = '<?xml version="1.0"?><userlist>'; foreach($users as $user) { $xml .= '<user>' . '<id><![CDATA['. $user->id .']]></id>' . '</user>'; } $xml .= '</userlist>'; return $xml; } function updateBroadcast($users) { sleep(1); ob_start(); $conn = new SAMConnection(); $conn->Connect(SAM_MQTT, array( SAM_HOST => '127.0.0.1', SAM_PORT => 1883 )); $conn->Send('topic://broadcast', (object)array('body' => usersToXML($users))); $conn->Disconnect(); ob_end_clean(); } while($line = fread($handle, 2096)) { echo $line; if (preg_match('/New client connected from .+ as user_(\d+)./', $line, $regs)) { $user = (object)array('id' => $regs[1]); $connectedUsers[$user->id] = $user; updateBroadcast($connectedUsers); } else if (preg_match('/Received DISCONNECT from user_(\d+)/', $line, $regs) || preg_match('/Client user_(\d+) has exceeded timeout, disconnecting./', $line, $regs) || preg_match('/Socket read error on client user_(\d+), disconnecting./', $line, $regs)) { if (isset($connectedUsers[$regs[1]])) { unset($connectedUsers[$regs[1]]); updateBroadcast($connectedUsers); } } } pclose($handle); ?> 
0
source

Source: https://habr.com/ru/post/1402192/


All Articles