onConnectionLost and onServerInitiatedDisconnect event behavior

Question

When is the onConnectionLost and onServerInitiatedDisconnect event triggered in HiveMQ Extension SDK?

Answer

onConnectionLost is called for the following reasons:

  1. The broker detects an I/O error or network failure.

  2. The client fails to communicate within the defined Keep Alive period.

  3. The client does not send a DISCONNECT packet before it closes the network connection.

onServerInitiatedDisconnect is called for the following reasons:

  1. The broker closes the network connection because of a protocol error.

  2. When a Client Take-Over happens.

  3. Client disconnected from the Control Center or by REST API.

The onConnectionLost is never called for a client that is disconnected because of the Client Take-Over because this disconnect is initiated by the broker => onServerInitiatedDisconnect.

Hint

clientService.isClientConnected(clientId) will always give you the status of the client: connected or not connected.
If the client is connected with cleanStart=false and its session still exists before the Client Take-Over then the output will be "connected" else the output will be "disconnected".