PabloGS
PabloGS

Reputation: 91

MQTT with AWS IoT Core not sending messages when QoS 2

I am trying to send messages from one client to another using MQTT, both are publishers and subscribers. When QoS = 1, the messages are sent but sometimes duplicated. However, when I use QoS = 2, the messages are apparently not sent. Each client sends 4 messages each round, the payload is a chunk of a biggest bytearray.

Code for the publisher:

def publish_model(model):
SliceLength, FinalSlice, ByteArray, ArrayLength,SlicesNumber = get_lengths(model)
mqttc.publish("home/AWS1", ByteArray[ArrayLength - FinalSlice:ArrayLength], qos=2)
sleep(0.2)
for i in range(SlicesNumber-1,0,-1):
        if i != 1:
            print(SliceLength*(i-1)-SliceLength*i)
            mqttc.publish("home/AWS1", ByteArray[SliceLength*(i-1):SliceLength*i], qos=2)
            mqttc.loop()

            print("model sent: home/AWS1")

        else:   ###The final chunk
            mqttc.publish("home/AWS2", ByteArray[SliceLength*(i-1):SliceLength*i], qos=2)  
            mqttc.loop()
            print("model sent: home/AWS2")

        sleep(0.2)

Code for the subscriber:

mqttc.subscribe("home/AWS1", 2)
mqttc.subscribe("home/AWS2", 2)

while 1 == 1:
    if connflag == True:
        p = 1  ##line that does nothing only waits

    else:
        print("waiting for connection...")

I would like to emphasize that both are subscribers and publishers

The publisher just keeps : MQTT sending when sending the messages

Upvotes: 2

Views: 1039

Answers (2)

Abhay Nayak
Abhay Nayak

Reputation: 1109

AWS IoT doesn't support QoS 2. When QoS level 2 is requested, the message broker doesn't send a PUBACK or SUBACK.

As a workaround, you can either

  • setup your own MQTT server and have the MQTT server configured as a publisher to IoT Core over QoS 1. That way your physical devices will still function over QoS 2 with your local server but you will be able to work with IoT Core within its limits

Or

  • publish the messages with a sequence number over QoS 1 and have a logic which removes duplicate sequence numbers ( using SQS + lambda or something similar). This way you will be able to make sure theres at most one message (no duplicate messages, something QoS 2 provides)

Upvotes: 3

PabloGS
PabloGS

Reputation: 91

The problem here is that AWS IoT Core only allows QoS 0 and 1. Therefore the messages won't be sent if they have QoS=2.

Upvotes: 2

Related Questions