MQTT ordering guarantees

Hi everyone,

we have an agent that sends multiple MQTT messages (SmartRest and JSON) for Measurements, Events and Inventory updates to Cumulocity. In some cases the order of messages is important (most noticeably we want to create a child before sending an event on it).
The agent is a simple single-threaded Python script, but we were wondering if Cumulocity guarantees that MQTT messages are handled in time-order and whether this happens for Agent and Child devices?
I.e. if I sent a message creating a child device creation followed by an JSON MQTT Event for the child, is it possible that the Event won’t be created because the child device has not been created yet?

Thanks,
Mario

1 Like

You can ensure that a message is fully processed and the contained data is created by sending it with higher QOS and waiting for PUBACK or PUBCOMP respectively before sending the next message that relies on the data existing.

1 Like

Yep, it is most probably a client issue sending multiple messages in short sequence. For (child) device creation I always suggest to use at least QoS 1 and as mentioned to wait for the PUBACK or PUBCOMP.

In python and paho-mqtt client this could look like this:

# set Device Name
msg = SmartRESTMessage('s/us', '100', [self.device_name, self.device_type])
self.publishMessage(msg, 1, wait_for_publish=True)

def publishMessage(self, message, qos=0, wait_for_publish=False):
        self.logger.debug(f'Send: topic={message.topic} msg={message.getMessage()}')
        if self.__client is not None and self.__client.is_connected:
            if wait_for_publish:
                self.__client.publish(message.topic, message.getMessage(), qos).wait_for_publish()
            else:
                self.__client.publish(message.topic, message.getMessage(), qos)
1 Like

Thanks, I get that Cumulocity should keep the processing order, as long as the client makes sure the message have been published in correct order.
I’ll give it a try.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.