What is the purpose of Stanza IDs in XMPP?

Viewed 984

Can someone explain what purpose do ID's serve in some of XMPP XML requests (talking about core XMPP)?

For example, I can fetch roster by sending this request:

<iq type="get">
    <query xmlns="jabber:iq:roster"></query>
</iq>

But I can also fetch it by adding a random ID:

<iq type="get" id="2346543">
    <query xmlns="jabber:iq:roster"></query>
</iq>

Is there a reason as to why should or shouldn't I use ID's? Is it security related? Are there any drawbacks? What is the convention about ID's and how should they be constructed?

2 Answers

When you send an IQ with type="get" or type="set" then you will receive a response with type="result" and the same id

So this is just to match responses to their requests, by id.

For example,

get roster:

<iq type="get" id="2346543">
    <query xmlns="jabber:iq:roster"></query>
</iq>

response:

<iq to='icq.exampel.com' from='juliet@example.com' type='result' id='2346543'>
  <query xmlns='jabber:iq:roster'>
    <item jid='123456789@icq.example.com'
          name='Romeo'
          subscription='both'>
      <group>Friends</group>
    </item>
    <item jid='554323654@icq.example.com'
          name='Mercutio'
          subscription='from'>
      <group>Friends</group>
    </item>
    <item jid='997665667@icq.example.com'
          name='Benvolio'
          subscription='both'>
      <group>Friends</group>
    </item>
  </query>
</iq>

they contain the same id so you know for what request this response is

I would recommend simply looking into the protocol specification. RFC 6120 § 8.1.3. id explains it pretty nicely IMHO:

The 'id' attribute is used by the originating entity to track any
response or error stanza that it might receive in relation to the
generated stanza from another entity (such as an intermediate server
or the intended recipient).

It is up to the originating entity whether the value of the 'id'
attribute is unique only within its current stream or unique
globally.

For <message/> and <presence/> stanzas, it is RECOMMENDED for the
originating entity to include an 'id' attribute; for <iq/> stanzas,
it is REQUIRED.

If the generated stanza includes an 'id' attribute then it is
REQUIRED for the response or error stanza to also include an 'id'
attribute, where the value of the 'id' attribute MUST match that of
the generated stanza.

Not that your example showing an IQ stanza without an 'id' attribute set is actually invalid.

Related