I am using aSmack library to communicate with remote xmpp server. I am able to send/receive messages, but I want to get timestamp of incoming message.
Could you tell me
Timestamp is not part of the regular messages. Therefore you cannot extract it.
There is a spec (as mentioned by @Dmitry), but this only applies to specific types of messages. Typically those that are NOT realtime, such as offline and publishing the last pubsub message of a node when a new subscription is created. These are messages with an inherent delay built into them.
It also works for 4.1.9
DelayInformation inf = null; inf = (DelayInformation)message.getExtension(DelayInformation.ELEMENT,DelayInformation.NAMESPACE); if (inf != null){ Date date = inf.getStamp(); System.out.println("date: "+date); }
For offline message: You will receive message as below:
<message xml:lang='en' to='<TO>' from='<FROM>' type='chat' id='3SE7n-39'><delay xmlns='urn:xmpp:delay' from='lakshitnagar.com' stamp='2020-09-05T18:01:01.854690Z'>Offline Storage</delay><offline xmlns='http://jabber.org/protocol/offline'><item node='335'/></offline><body>Hello</body></message>
You can parse this XML to get stamp attribute under delay tag. This will give you the time on which it is sent from the sender.
For MAM archived message: You will receive something as below:
<message xmlns='jabber:client' to='<TO>' from='<FROM>' id='NzVV5-84' xml:lang='en' type='chat'><archived xmlns='urn:xmpp:mam:tmp' xmlns:stream='http://etherx.jabber.org/streams' by='lakshitnagar@lakshitnagar.com' id='1599328757912003'></archived><body>hello</body></message>
You can parse this XML to get id attribute under archived tag. This will give you the epoch time in microseconds when this message was sent from the sender.
Due to specs time is required attribute for XMPP message:
http://xmpp.org/extensions/xep-0203.html#protocol
Check the <delay
item of <message
:
<delay xmlns='urn:xmpp:delay'
from='capulet.com'
stamp='2002-09-10T23:08:25Z'>
Offline Storage
</delay>
But getting it looks a bit tricky. As soon as aSmack is recompiled Smack with some replaced stuff, so try to get it the way like here:
http://edwin.baculsoft.com/2011/06/how-to-get-offline-messages-timestamp-on-openfire/
DelayInformation inf = null;
try {
inf = (DelayInformation)packet.getExtension("x","jabber:x:delay");
} catch (Exception e) {
log.error(e);
}
// get offline message timestamp
if(inf!=null)
Date date = inf.getStamp();
Problably, you will need to check what server sends with message as extension value and replace "jabber:x:delay"
with 'urn:xmpp:delay'
as it is shown in XMPP specs example.
But not sure if it works.
With smack 4.2.0-rc2-SNAPSHOT version it's easy to access,
DelayInformation delayInformation = forwarded.getDelayInformation();
delayInformation.getStamp().getTime();
Although in part a blend of the other answers already here, I would present the answer as follows...
XMPP is "realtime"... although that term can mean a lot of different things to different people, in general you can safely assume that you receive messages (almost) as soon as they're sent - all XMPP software is designed around fulfilling this goal.
However there is no actual guarantee about the end-to-end latency. In general practice you'll typically always receive a message far less than a second after it was transmitted by the sender, though network conditions can affect this.
In some cases the message will be temporarily delayed, such as when a new server-to-server connection needs to be established. This can take up to a few seconds generally, depending again on network conditions and the method of authentication used between the servers.
A message may also be held up if the recipient is offline - the recipient's server may hold the message in an "offline message" store, until it can deliver it when the the recipient comes online.
Because of the general instant delivery of XMPP messages, timestamps are generally not included in the message itself, as that information is useless (and assumes clocks are all correct).
In cases where the message is knowingly delayed though, such as in the examples above, the entity that delays the message may insert a tag into the message indicating the original time of the message. This is detailed in XEP-0203: Delayed Delivery.