Search code examples
c#correlationmqttnet

MQTTnet: Cannot send/receive CorrelationData


We are using the MQTTnet library in its lastes version to communicate. General communication is working. It's possible to subscribe and publish. The Payload and the Topic are received. But when publishing a message with CorrelationData set, in the received message only the payload and the topic exist. CorrelationData is Null. We tested it with another application using some other C++ lib. This can send and receive Correlation data. So I assume that the MQTT broker is fine.

Is there anything to consider/configure to be able to use the CorrelationData?


Solution

  • Adding to Jeanot's answer the version can be set in the MqttClientOptionsBuilder. Ex:

    using (var mqttClient = mqttFactory.CreateMqttClient())
    {
        var mqttClientOptions = new MqttClientOptionsBuilder()
            .WithTcpServer("localhost")
            .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500)
            .Build();
            
        await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
    }