Home > other >  How to set RETAIN publish message for flutter Mqtt_client
How to set RETAIN publish message for flutter Mqtt_client

Time:02-27

Now I can publish message using this

MqttServerClient client = MqttServerClient.withPort(broker, "", 1883); //connect broker

//create message
final builder = MqttClientPayloadBuilder();
builder.addString(message);

client.publishMessage(topic, MqttQos.atLeastOnce, builder.payload); //publish message

but how can I set published message to Retain.

CodePudding user response:

As per the docs the syntax is:

int publishMessage(

    String topic,
    MqttQos qualityOfService,
    Uint8Buffer data,
    {bool retain = false}

) 

So your publish would need to be:

client.publishMessage(topic, MqttQos.atLeastOnce, builder.payload, true);
  • Related