publishMessage method

int publishMessage(
  1. String topic,
  2. MqttQos qualityOfService,
  3. Uint8Buffer data, {
  4. bool retain = false,
  5. List<MqttUserProperty>? userProperties,
})

Publishes a message to the message broker. Returns the message identifer assigned to the message. Raises InvalidTopicException if the topic supplied violates the MQTT topic format rules.

Implementation

int publishMessage(
    String topic, MqttQos qualityOfService, typed.Uint8Buffer data,
    {bool retain = false, List<MqttUserProperty>? userProperties}) {
  if (connectionHandler?.connectionStatus?.state !=
      MqttConnectionState.connected) {
    throw MqttConnectionException(connectionHandler?.connectionStatus?.state);
  }
  try {
    final pubTopic = MqttPublicationTopic(topic);
    return publishingManager!.publish(pubTopic, qualityOfService, data,
        retain: retain, userProperties: userProperties);
  } on Exception catch (e) {
    throw MqttInvalidTopicException(e.toString(), topic);
  }
}