Guido
Guido

Reputation: 688

MQTT message timestamp

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

Upvotes: 7

Views: 26949

Answers (3)

VoteCoffee
VoteCoffee

Reputation: 5107

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.

Upvotes: 5

hardillb
hardillb

Reputation: 59751

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.

Upvotes: 14

Amine Harbaoui
Amine Harbaoui

Reputation: 1295

You can use custom format string

example:

mosquitto_sub -v -t "your topic" -F "%I %t %p"

For more details: Click here

Upvotes: 4

Related Questions