Skip to content

Commit b1d650e

Browse files
author
Alex J Lennon
committed
Fix MQTT disconnection issue by adding KeepAlive configuration
- Add KeepAlivePeriodSeconds to MqttConfig (default: 60 seconds) - Configure MQTT client with KeepAlive period in initial connection - Configure MQTT client with KeepAlive period in reconnection logic - Update appsettings.json with KeepAlivePeriodSeconds setting Issue: - MQTT broker was disconnecting the client after ~3 seconds of inactivity - Client was not sending keepalive packets (PINGREQ) to maintain connection - This caused repeated connect/disconnect cycles Solution: - Added WithKeepAlivePeriod() to MQTT client options - Client now sends PINGREQ packets every 60 seconds (configurable) - Prevents broker from disconnecting idle clients - Standard MQTT keepalive behavior The keepalive period can be configured via: - appsettings.json: MQTT.KeepAlivePeriodSeconds - Environment variable: MQTT__KeepAlivePeriodSeconds Default: 60 seconds (standard MQTT keepalive interval)
1 parent b9ea645 commit b1d650e

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/AppConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class MqttConfig
4646
public double RetryBackoffMultiplier { get; set; } = 2.0;
4747
public bool AutoReconnect { get; set; } = true;
4848
public int ReconnectDelaySeconds { get; set; } = 5;
49+
public int KeepAlivePeriodSeconds { get; set; } = 60;
4950
public bool UseTls { get; set; } = false;
5051
public bool AllowUntrustedCertificates { get; set; } = false;
5152
public string? CertificatePath { get; set; }

src/MQTTControl.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,16 @@ public static async Task Initialise(CancellationTokenSource cts, AppConfig? conf
142142
return Task.CompletedTask;
143143
};
144144

145+
// Get keepalive period from config (default: 60 seconds)
146+
// KeepAlive prevents broker from disconnecting idle clients
147+
int keepAlivePeriod = _config?.MQTT.KeepAlivePeriodSeconds ?? 60;
148+
145149
var builder = new MqttClientOptionsBuilder()
146150
.WithClientId(_clientId)
147151
.WithTcpServer(_serverAddress, _port)
148152
.WithCleanSession()
149-
.WithTimeout(TimeSpan.FromSeconds(_timeoutInSeconds));
153+
.WithTimeout(TimeSpan.FromSeconds(_timeoutInSeconds))
154+
.WithKeepAlivePeriod(TimeSpan.FromSeconds(keepAlivePeriod));
150155

151156
// Add TLS/SSL if configured
152157
if (_config?.MQTT.UseTls == true)
@@ -351,11 +356,15 @@ private static async Task AttemptReconnectAsync(CancellationToken cancellationTo
351356
return;
352357
}
353358

359+
// Get keepalive period from config (default: 60 seconds)
360+
int keepAlivePeriod = _config?.MQTT.KeepAlivePeriodSeconds ?? 60;
361+
354362
var builder = new MqttClientOptionsBuilder()
355363
.WithClientId(_clientId)
356364
.WithTcpServer(_serverAddress, _port)
357365
.WithCleanSession()
358-
.WithTimeout(TimeSpan.FromSeconds(_timeoutInSeconds));
366+
.WithTimeout(TimeSpan.FromSeconds(_timeoutInSeconds))
367+
.WithKeepAlivePeriod(TimeSpan.FromSeconds(keepAlivePeriod));
359368

360369
// Add TLS/SSL if configured (same as initial connection)
361370
if (_config?.MQTT.UseTls == true)

src/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"RetryBackoffMultiplier": 2.0,
1414
"AutoReconnect": true,
1515
"ReconnectDelaySeconds": 5,
16+
"KeepAlivePeriodSeconds": 60,
1617
"UseTls": false,
1718
"AllowUntrustedCertificates": false,
1819
"CertificatePath": null,

0 commit comments

Comments
 (0)