Search code examples
amazon-web-servicesmqttmosquitto

Repeated messages mosquitto MQTT broker to amazon IoT Service using persistence


I have a Raspberry with a mosquitto broker on it and bridged with amazon IoT service. https://aws.amazon.com/es/blogs/iot/how-to-bridge-mosquitto-mqtt-broker-to-aws-iot/

This is my mosquitto.conf file:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

This is the bridge.conf that is inside the /etc/mosquitto/conf.d

# =================================================================
# Bridges to AWS IOT
# =================================================================

# AWS IoT endpoint, use AWS CLI 'aws iot describe-endpoint'
connection awsiot
address xxxxxxxxx.iot.eu-central-1.amazonaws.com:8883

# Specifying which topics are bridged
topic awsiot_to_localgateway in 1
topic localgateway_to_awsiot/iot out 1
topic both_directions both 1

# Setting protocol version explicitly
bridge_protocol_version mqttv311
bridge_insecure false

# Bridge connection name and MQTT client Id,
# enabling the connection automatically when the broker starts.
cleansession true
clientid bridgeawsiot
start_type automatic
notifications false
log_type all

# =================================================================
# Certificate based SSL/TLS support
# -----------------------------------------------------------------
#Path to the rootCA
bridge_cafile /etc/mosquitto/certs/rootCA.pem

# Path to the PEM encoded client certificate
bridge_certfile /etc/mosquitto/certs/cert.crt

# Path to the PEM encoded client private key
bridge_keyfile /etc/mosquitto/certs/private.key

All works fine. But, If I remove the ethernet cable to test the pesistence. When the comunications are reestablished. The broker send repeated messages to the amazon IoT service.

This is the message that I'm sending

char dataToSend[] = "Message Id: ";
counter++;

snprintf(dataToSend, sizeof(dataToSend) + 10, "Message Id: %d", counter);
app_mqtt_publish(&dataToSend);

Is it a normal behaviour?


Solution

  • The short version of (part of) the MQTT spec:

    • QOS 0 - > Messages may be delivered
    • QOS 1 -> Messages will be delivered at least once
    • QOS 2 -> Messages will be delivered once and only once.

    So if messages have not been acknowledged then there is a chance that QOS 1 messages will be delivered again. They should have the DUP flag set in the header so the receiving broker should know they may have been delivered already.

    IIRC AWS-IoT doesn't support QOS 2 so you may have just put up with this.