0

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?

João Antunes
  • 798
  • 8
  • 27
  • What MQTT version are you connecting to the broker with? Only messages sent via MQTT v5 will have a PayloadFormatIndicator – hardillb Aug 03 '22 at 12:19
  • I am using MQTTnet.Extensions.ManagedClient" Version="4.0.2.221. How do I know which MQTT protocol is used? – João Antunes Aug 03 '22 at 13:29
  • Ok I've added ".WithProtocolVersion(MqttProtocolVersion.V500)" on the WithClientOptions and it now works :) thanks for the help! – João Antunes Aug 03 '22 at 13:48

1 Answers1

1

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

hardillb
  • 54,545
  • 11
  • 67
  • 105