# AfterDisconnect is not triggering after network disconnect

**URL:** <https://community.hivemq.com/t/afterdisconnect-is-not-triggering-after-network-disconnect/2888>\
**Category:** HiveMQ Client Library\
**Created:** [May 21, 2024, 11:51am UTC](https://community.hivemq.com/t/afterdisconnect-is-not-triggering-after-network-disconnect/2888 "2024-05-21T11:51:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tverfyll](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@Tverfyll](https://community.hivemq.com/u/Tverfyll)\
**Post date:** [May 21, 2024, 11:51am UTC](https://community.hivemq.com/t/afterdisconnect-is-not-triggering-after-network-disconnect/2888/1 "2024-05-21T11:51:13Z")

</div>

Hi!

i have made a Client with the [hivemq-mqtt-client-dotnet](https://github.com/hivemq/hivemq-mqtt-client-dotnet) library in c#.

The initial connect works fine, and OnMessageReceived event get triggered on incomming messages.

The problem is that when the connection failes, the client never notices.  
if i unplug my ethernet-cable, the client.AfterDisconnect never triggers.  
client.IsConnected() - flag also stays true.

So for now i have no working reconnect since i’m not able to detect when connection failes.

is there any way to detect when connection to the broker is missing?

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [May 21, 2024, 3:45pm UTC](https://community.hivemq.com/t/afterdisconnect-is-not-triggering-after-network-disconnect/2888/2 "2024-05-21T15:45:30Z")

</div>

Hi @Tverfyll

Greetings! We’re thrilled to have you here, showing interest in MQTT and the HiveMQ broker. Welcome to the HiveMQ Community! We’re always happy to see new users.

You mentioned that you made a client using the [hivemq-mqtt-client-dotnet](https://github.com/hivemq/hivemq-mqtt-client-dotnet) library in C#. However, you didn’t provide a link to the project. Was that an oversight?

Thanks,  
Dasha from the HiveMQ Team

---

<div class="post-metadata">

**Author:** ![Tverfyll](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@Tverfyll](https://community.hivemq.com/u/Tverfyll)\
**Post date:** [May 22, 2024, 7:15am UTC](https://community.hivemq.com/t/afterdisconnect-is-not-triggering-after-network-disconnect/2888/3 "2024-05-22T07:15:26Z")

</div>

Hi @Daria_H

i have now uploaded the relevant code to [github/Tverfyll/HiveMQ](https://github.com/Tverfyll/HiveMQ). Hope this helps.

---

<div class="post-metadata">

**Author:** ![AaronTLFranz](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/aarontlfranz/32/638_2.png) [@AaronTLFranz](https://community.hivemq.com/u/AaronTLFranz)\
**Post date:** [May 23, 2024, 2:56pm UTC](https://community.hivemq.com/t/afterdisconnect-is-not-triggering-after-network-disconnect/2888/4 "2024-05-23T14:56:39Z")

</div>

Hello @Tverfyll ,

With some additional review of the C# client library, we do have an example available within our GitHub, [here](https://github.com/hivemq/hivemq-mqtt-client-dotnet/blob/main/Examples/Reconnect/Program.cs), that provides example reconnect strategies on the event of a disconnect.

It is worth mentioning that in this example, the handler `client.AfterDisconnect` is utilized for reconnect strategies, as this is called on a client disconnect.

Let us know if this answers your question, or if there is additional assistance we can provide!

Best,  
Aaron from the HiveMQ Team

---

<div class="post-metadata">

**Author:** ![Tverfyll](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@Tverfyll](https://community.hivemq.com/u/Tverfyll)\
**Post date:** [May 23, 2024, 7:56pm UTC](https://community.hivemq.com/t/afterdisconnect-is-not-triggering-after-network-disconnect/2888/5 "2024-05-23T19:56:21Z")

</div>

Hi @AaronTLFranz,

i have seen the reconnect example, but my code never gets there.

My issue is that the handler `client.AfterDisconnect` only triggers in cases with normal disconnect, either from the client or the broker.

In cases where the server looses internet connection, the C# Client library think it’s still connected.  
Even the `client.IsConnected()` returns `true` with the ethernet cable disconnected.

Is there a connection watchdog or something that i’m missing?

Tverfyll
