MQTT and Wifi connections

Have a look at the MQTT spec. The /connected topic carries a last will message which will be sent out by the MQTT broker automatically:

Indicates if the device is currently
connected to the MQTT server or not.
Uses “false” as the last will message,
which will be set by the mqtt server
automatically if the device disconnects.