I am currently using EMQ as our MQTT broker. I need to get the latest "online" or simply the connection state of a client as quick as possible. Currently, I let my backend run a single client which subscribes to a $SYS topic, where the connection and disconnection event of a client will be published to as a message. When a client is connected, a connected message will be published to $SYS/.../connected. When it is disconnected, a disconnected message will be published to $SYS/.../disconnected.
The problem with this approach is that the disconnected messages will only be published immediately when the client is disconnected normally. Like, when the client closes the connection by itself intentionally. But if the disconnection is due to unreachable network or power source cut-off, then the message will be published after 3 or more minutes.
I also tried using the LWT (Last Will Testament) approach but the result is similar as the one above. So I guess that if the disconnection happens unintentionally, the client won't have a chance to tell the broker that it will disconnect itself with the broker. So, it will definitely take sometime for the broker to notice that the client is actually disconnected.
So, my question would be, how would I shorten that time? I once suspect that the "keep-alive" thing has something to do with this matter. But after a couple of experiences, it's not.