Home > Mobile >  Subscriber on Mosquitto MQTT ContentType is Null
Subscriber on Mosquitto MQTT ContentType is Null

Time:08-04

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?

CodePudding user response:

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

  • Related