MQTT message timestamp
Solution 1
There is no timestamp in the message, there is no where to store such information in the MQTT 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.
Solution 2
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 (you may need to pull the develop branch to get this plugin)
Plugin code from: https://github.com/eclipse/mosquitto/blob/develop/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.
Solution 3
You can use custom format string
example:
mosquitto_sub -v -t "your topic" -F "%I %t %p"
For more details: Click here
Related videos on Youtube
Guido
Updated on September 16, 2022Comments
-
Guido about 1 year
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?
-
hardillb almost 3 yearsYou've not said which MQTT broker has this plugin. Also the Paho Python client does support MQTTv5 (the Paho website just needs updating)
-
VoteCoffee almost 3 yearsThanks, I improved my answer based on your feedback
-
hardillb about 2 yearsTopics should NOT start with a leading
/