MQTTnet
MQTTnet copied to clipboard
Several reconnecting with ManagedMqttClient
Describe your question
Hello. I've developed an open-source project for monitoring and interacting with Windows Desktop in general. This is a side project which I started to help a friend but it got heavily used in Home Assistant community as there is a lack of a proper Windows IOT tool.
I would like to request your help with my MQTT issues. I'm using your library to connect to MQTT server and publish my monitors values. However, several users (specially my friend which beta test with 10 computers on his company) have reported me disconnections (or reconnections) for timing out, while other MQTT clients in their networks are fine.
Here is how I'm using MQTTnet:
- https://gitlab.com/iotlink/iotlink/-/blob/feature/mqtt-connection-rewrite/IOTLink/Service/MQTT/MQTTClient.cs
- https://gitlab.com/iotlink/iotlink/-/blob/feature/mqtt-connection-rewrite/IOTLink/Service/MQTT/MQTTClientManager.cs
The MQTTClient.cs class should handle the MQTT connection itself using your client and send/subscribe/everything. The MQTTClientManager.cs is just a manager which should verify and recreate the entire MQTTClient if something goes south.
Here is an example of a log file:
[WORKGROUP\DESKTOP-SALA][2020-04-15 22:20:37 -03:00][INFO][IOTLink.IOTLinkService]: Windows Service is started.
[WORKGROUP\DESKTOP-SALA][2020-04-15 22:20:37 -03:00][INFO][IOTLinkService.Service.MQTT.MQTTClient]: MQTTClient::Connect() - Trying to connect to broker: tcp://192.168.20.2:1883.
[WORKGROUP\DESKTOP-SALA][2020-04-15 22:20:37 -03:00][SYSTEM][IOTLinkService.Service.MQTT.MQTTClient]: ALL YOUR MQTT TOPICS WILL START WITH iotlink/workgroup/desktop-sala/
[WORKGROUP\DESKTOP-SALA][2020-04-15 22:20:42 -03:00][INFO][IOTLinkService.Service.MQTT.MQTTClient]: MQTTClient::OnConnectedHandler() - MQTT Connected
[WORKGROUP\DESKTOP-SALA][2020-04-15 22:25:49 -03:00][INFO][IOTLinkService.Service.MQTT.MQTTClient]: MQTTClient::OnConnectedHandler() - MQTT Connected
[WORKGROUP\DESKTOP-SALA][2020-04-15 22:32:37 -03:00][INFO][IOTLinkService.Service.MQTT.MQTTClient]: MQTTClient::OnConnectedHandler() - MQTT Connected
[WORKGROUP\DESKTOP-SALA][2020-04-15 23:06:15 -03:00][INFO][IOTLinkService.Service.MQTT.MQTTClient]: MQTTClient::OnConnectedHandler() - MQTT Connected
[WORKGROUP\DESKTOP-SALA][2020-04-15 23:13:13 -03:00][INFO][IOTLinkService.Service.MQTT.MQTTClient]: MQTTClient::OnConnectedHandler() - MQTT Connected
[WORKGROUP\DESKTOP-SALA][2020-04-15 23:14:32 -03:00][INFO][IOTLinkService.Service.MQTT.MQTTClient]: MQTTClient::OnConnectedHandler() - MQTT Connected
[WORKGROUP\DESKTOP-SALA][2020-04-15 23:14:55 -03:00][INFO][IOTLinkService.Service.MQTT.MQTTClient]: MQTTClient::OnConnectedHandler() - MQTT Connected
[WORKGROUP\DESKTOP-SALA][2020-04-15 23:15:18 -03:00][INFO][IOTLinkService.Service.MQTT.MQTTClient]: MQTTClient::OnConnectedHandler() - MQTT Connected
[WORKGROUP\DESKTOP-SALA][2020-04-15 23:15:42 -03:00][INFO][IOTLinkService.Service.MQTT.MQTTClient]: MQTTClient::OnConnectedHandler() - MQTT Connected
And here it is possible to see the MQTT Server logs:

If you can take a quick look on these files or guide me how can I try to find how to fix this issue I would be grateful as the IOTLink users as well. I could not find any contact details of yours, so that is why I'm opening an issue. If you want to contact me directly, my Discord is alexslx#0984.
Just to be clear as a developer, I'm not asking for you fix the code for me or something else that might sound like this, just a little help using MQTTnet.
I'm sorry in advance if this issue has bothered you or your project anyway.
Which project is your question related to?
- Client
- ManagedClient
hi alex what is the purpose of your MQTTClientManager.cs? the managed client reconnects on its own. I would remove it.
from your logs i cannot see whats wrong maybe plugin mqttnets trace https://github.com/chkr1011/MQTTnet/wiki/Trace
CleanEvents can be:
OnMQTTConnected = null
and finally you dont await any of your calls. thats propably a big source of issue. you should await all methods that return a task. and that method should then also return a task. async void is a big nono
I assume this issue is closed.