dotnet / dotnet/MQTTnet

Problems with QoS > 0

Open
#1,089 8 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
C#
Stars
5.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

### Describe your question
Hi, i recently created 2 c# application with the MqttNet nuget. All works perfectly, but I have some problems with the redelivery of the QoS 1 and 2 messages not delivered (client offline).

My client connection code is like this:

`static async void StartClient()
{
//private System.Diagnostics.EventLog Log;

//IsoftMQTTClient settings = new IsoftMQTTClient();
//settings.LoadSettings();
//StartServer();
EventLog Log1 = new EventLog();

if (!EventLog.SourceExists("IsMQTTClient"))
EventLog.CreateEventSource("IsMQTTClient", "IsMQTTClient");

Log1.Source = "IsMQTTClient";
Log1.Log = "IsMQTTClient";

try {
// Create a new MQTT client.
var factory = new MqttFactory();
var mqttClient = factory.CreateMqttClient();

MqttClientOptions options;

//Log1.WriteEntry("aaa " + GlobalVar.MQTT_CLIENT_CLEAN_SESSION.ToUpper());

if (GlobalVar.MQTT_CLIENT_CLEAN_SESSION.ToUpper() == "S")
{
// Create TCP based options using the builder.
options = (MqttClientOptions)new MqttClientOptionsBuilder()
.WithClientId(GlobalVar.MQTT_CLIENT_CLIENT_ID_WINDOWS_SERVICE)
.WithTcpServer(GlobalVar.MQTT_BROKER_HOST, Int32.Parse((GlobalVar.MQTT_BROKER_PORT))) // Port is optional
.WithCredentials(GlobalVar.MQTT_BROKER_USERNAME, GlobalVar.MQTT_BROKER_PASSWORD)
.WithCommunicationTimeout(new TimeSpan(0, 1, 0))
//.WithTls()
.WithCleanSession()
.Build();
}
else
{
options = (MqttClientOptions)new MqttClientOptionsBuilder()
.WithClientId(GlobalVar.MQTT_CLIENT_CLIENT_ID_WINDOWS_SERVICE)
.WithTcpServer(GlobalVar.MQTT_BROKER_HOST, Int32.Parse((GlobalVar.MQTT_BROKER_PORT))) // Port is optional
.WithCredentials(GlobalVar.MQTT_BROKER_USERNAME, GlobalVar.MQTT_BROKER_PASSWORD)
.WithCommunicationTimeout(new TimeSpan(0, 1, 0))
.WithCleanSession(false)
//.WithTls()
.Build();
}

//Log1.WriteEntry(options.CleanSession.ToString());

await mqttClient.ConnectAsync(options, CancellationToken.None); // Since 3.0.5 with CancellationToken
Log1.WriteEntry("### CONNECTED TO BROKER ###");
//await mqttClient.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("#").Build());
//Log1.WriteEntry("### SUBSCRIBED TO ALL TOPICS ###");
SubscribeToTopics(GlobalVar.MQTT_CLIENT_TOPIC_SUBSCRIPTION, mqttClient, options);
//while (true)
//{
await PoolMessagesAsync(mqttClient, options);
//}

//Console.ReadKey();
} catch (Exception ex) { Log1.WriteEntry("Errore: " + ex.ToString()); }
}`

inside SubscribeToTopics() i'm subscribing to all topics available, and while client is connected i recive every message the broker recives
`await mqttClient.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("#").Build());
Log1.WriteEntry("### SUBSCRIBED TO ALL TOPICS ###");`

My Broker is hosted like this:
`static async void StartServer()
{
var factory = new MqttFactory();

// Configure MQTT server.
var optionsBuilder = new MqttServerOptionsBuilder()
.WithConnectionValidator(c =>
{
if (c.ClientId.Length < 1)
{
c.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
return;
}

System.Diagnostics.EventLog Log1;

Log1 = new EventLog();

if (!EventLog.SourceExists("IsMQTTBroker"))
EventLog.CreateEventSource("IsMQTTBroker", "IsMQTTBroker");

Log1.Source = "IsMQTTBroker";
Log1.Log = "IsMQTTBroker";

//Log1.WriteEntry(GlobalVar.MQTT_BROKER_HOST);
//Log1.WriteEntry(GlobalVar.MQTT_BROKER_PORT);
//Log1.WriteEntry(GlobalVar.MQTT_BROKER_USERNAME);
//Log1.WriteEntry(GlobalVar.MQTT_BROKER_PASSWORD);
//Log1.WriteEntry(GlobalVar.MQTT_CLIENT_CLIENT_ID_WINDOWS_SERVICE);
//
//Log1.WriteEntry("c Username " +c.Username);
//Log1.WriteEntry("c Password " + c.Password);
//Log1.WriteEntry("Int32.Parse((GlobalVar.MQTT_BROKER_PORT)) " + Int32.Parse((GlobalVar.MQTT_BROKER_PORT)).ToString());

if (c.Username != GlobalVar.MQTT_BROKER_USERNAME)
{
c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
return;
}

if (c.Password != GlobalVar.MQTT_BROKER_PASSWORD)
{
c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
return;
}

c.ReasonCode = MqttConnectReasonCode.Success;
})
.WithConnectionBacklog(100)
.WithDefaultEndpointPort(Int32.Parse((GlobalVar.MQTT_BROKER_PORT)))
.WithPersistentSessions();

var mqttServer = new MqttFactory().CreateMqttServer();
await mqttServer.StartAsync(optionsBuilder.Build());

Console.WriteLine("Press any key to exit.");
Console.ReadLine();
//await mqttServer.StopAsync();
}`

when i stop my client, send some messages with QoS 1 or 2 and start my client again those messages are vanished.
I have a static and unique ClientID, and the client is always started with ".WithCleanSession(false)". Any idea on what i'm missing to manage those messages recived while offline?

Thanks

Best regards,
Andrea

### Which project is your question related to?
- Client
- Server

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.