pstanton
pstanton

Reputation: 36689

MQTT - listen to ping, disconnect and connect events

I have "server side" mqtt client which I use to monitor and manage remote mqtt clients. I would like to extend this server module to keep tabs on the connectivity of the remote clients.

During normal operation, the remote clients regularly PING the broker, as per the broker logs:

1532924170: Received PINGREQ from c51
1532924170: Sending PINGRESP to c51

and when a disconnection occurs, the broker logs show this too:

1532924976: Client c51 has exceeded timeout, disconnecting.
1532924976: Socket error on client c51, disconnecting.

as well as the subsequent re-connection:

1532924978: New client connected from X.X.X.X as c51 (c1, k30).
1532924978: Sending CONNACK to c51 (0, 0)

I would like to monitor these 3 events from the mqtt-client held by the server module. Is this possible? If not, what alternative approach to "health" monitoring can you recommend?

Upvotes: 3

Views: 4092

Answers (1)

hardillb
hardillb

Reputation: 59836

No, you can not read these from a connected client.

The only pure MQTT approach is to make use of the Last Will and Testament (LWT) feature. You have the client set up a LWT publish a retained message to a client specific topic that marks it as offline. Then as the client connects it should publish a retained message to show you are online. If you disconnect cleanly (not triggered by keep alive time out you should manually publish the LWT message as the last thing before disconnecting).

It's also worth pointing out that ping messages only get sent if no other messages have been sent between the client and the broker in the keep alive period.

Upvotes: 2

Related Questions