Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MQTT message timestamp

Tags:

timestamp

mqtt

I want to recover an MQTT message publish timestamp but I couldn't find Support in the subscriber library. In the other hand I see MQTT.fx client is able to recover this Information. Anyone knows how to handle that?

MQTT. fx client -> message timestamp

like image 465
Guido Avatar asked Dec 08 '22 21:12

Guido


2 Answers

There is no timestamp in the message, there is no where to store such information in the MQTT v3 header.

MQTT.fx must be using time of arrival at the client.

If you need a published time you will have to add it to the message payload yourself.

like image 188
hardillb Avatar answered Dec 11 '22 11:12

hardillb


In Eclipse Mosquitto, they have added a plugin to support having the broker timestamp the message in the user properties (MQQTv5 only): plugins/message-timestamp

Plugin code from: https://github.com/eclipse/mosquitto/blob/master/plugins/message-timestamp/mosquitto_message_timestamp.c

static int callback_message(int event, void *event_data, void *userdata)
{
    struct mosquitto_evt_message *ed = event_data;
    struct timespec ts;
    struct tm *ti;
    char time_buf[25];

    clock_gettime(CLOCK_REALTIME, &ts);
    ti = gmtime(&ts.tv_sec);
    strftime(time_buf, sizeof(time_buf), "%Y-%m-%dT%H:%M:%SZ", ti);

    return mosquitto_property_add_string_pair(&ed->properties, MQTT_PROP_USER_PROPERTY, "timestamp", time_buf);
}

User properties are not backwards compatible to MQTTv3. If you're stuck on MQTTv3, then you can:

  • Add timestamps to your message payload (ugly because publisher timestamp might not be valid and adds complexity to payload)
  • Avoid using retain (creates the need to timestamp and cache data at subscriber).

While Paho supports MQTTv5, some language variants do not. And some common debug tools like MQTT Explorer which do support MQTTv5 don't yet handle/display user properties.

like image 27
VoteCoffee Avatar answered Dec 11 '22 12:12

VoteCoffee