Time stamp of publish


Is there any way in MQTT protocol for a subscriber to get the time stamp of when a message was published ? I am using a NodeJS MQTT client (GitHub - mqttjs/MQTT.js: The MQTT client for Node.js and the browser) with HiveMQ CE broker. Is there any mechanism to ensure message delivery order ?


Hi @Harsha,

there should be no need for you to concern yourself with ordering of publishes as the MQTT broker will deliver the messages he receives in the same order to an subscriber.

Is there any way in MQTT protocol for a subscriber to get the time stamp of when a message was published

Yes that is possible (but only with a subscriber that uses MQTT 5), you can use the extension system to intercept publishes and get their timestamp when they were received by the broker and modify the publish so to add the timestamp as user property. Alternatively you can let the publisher add the timestamp as user property then you don’t need an extension (would require that the publisher client also uses MQTT 5).

Here a snippet how to add the timestamp via extension sdk to give you a starting point should you go with the extension solution:

    public void extensionStart(final @NotNull ExtensionStartInput extensionStartInput,
                               final @NotNull ExtensionStartOutput extensionStartOutput) {

        Services.initializerRegistry().setClientInitializer((initializerInput, clientContext) ->
                        (publishInboundInput, publishInboundOutput) -> {
                            // timestamp when the broker received the publish from the publisher
                            final long timestamp = publishInboundInput.getPublishPacket().getTimestamp();
                            final ModifiablePublishPacket modifiablePublishPacket = publishInboundOutput.getPublishPacket();
                            final ModifiableUserProperties modifiableUserProperties = modifiablePublishPacket.getUserProperties();

                            modifiableUserProperties.addUserProperty("timestamp", String.valueOf(timestamp));

Here is the javadoc for timestamp:

My opinion:
If you know that publishers are always MQTT 5 clients go with the second option as this solution works with any MQTT 5 broker. If you have also MQTT 3 publisher use the extension solution, though this solution only works with HiveMQ.

Hope this helps!

Kind regards,
Michael from the HiveMQ team


Thank you @michael_w.

Is there any advantage in adding the time stamp as user property instead of directly adding it in the payload ? I am using NodeJS MQTT client and still trying to find the syntax to add the time stamp as user property using MQTT 5.


Hi @Harsha,

one advantage I would see is that you keep the metadata (i.e. timestamp) separate from the payload. So if the timestamp was already part of payload then this point is moot.
Another advantage which only exists when you want to get the timestamp on the broker is that you wouldn’t have to parse the payload into a readable format to get the timestamp, this is especially good when you have big payloads, here again if the broker only forwards the message and doesn’t do anything else → this advantage is irrelevant.

Adding the timestamp to the payload has the advantage that you can do this with any MQTT version.

As for how to add user properties in mqtt.js, it would be better to ask on the mqtt.js github repo for advice.
From their code it looks like the publish docu example you have the “options” parameter where you can add the user properties.



Thank you @michael_w