How to set RETAIN publish message for flutter Mqtt_client

1.2k views Asked by At

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.

1

There are 1 answers

1
Brits On BEST ANSWER

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, retain:true);