Search code examples
c#mqttmosquittomqttnet

Subscriber on Mosquitto MQTT ContentType is Null


I'm trying to publish a serialized message and sending the ContentType of that object type, so when I receive the message on the subscriber I know which type I can deserialize back to.

Here is the publish code:

public async Task PublishByTopicAsync<T>(string topic, T payload)
    {
        var payloadInBytes = _serializer.SerializeObject(payload);
        var message = new MqttApplicationMessage()
        {
            Topic = topic,
            PayloadFormatIndicator = MqttPayloadFormatIndicator.CharacterData,
            ContentType = "json",//payload?.GetType().FullName,
            Payload = payloadInBytes,
            QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
        };
        await _mqttClient.Value.Result.EnqueueAsync(message);
    }

On the receiving side I get this:

enter image description here

Any idea how can I send the type of object on publishing, and receive it on the subscriber?


Solution

  • As described in the comments, PayloadFormatIndicator is only available when both clients are connected to the broker with MQTT v5