<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE xep SYSTEM 'xep.dtd' [
  <!ENTITY % ents SYSTEM 'xep.ent'>
%ents;
]>
<?xml-stylesheet type='text/xsl' href='xep.xsl'?>
<xep>
<header>
  <title>Best Practices for Message Threads</title>
  <abstract>This specification defines recommended handling of XMPP message threads.</abstract>
  &LEGALNOTICE;
  <number>0201</number>
  <status>Experimental</status>
  <type>Informational</type>
  <sig>Standards</sig>
  <approver>Council</approver>
  <dependencies>
    <spec>XMPP Core</spec>
  </dependencies>
  <supersedes/>
  <supersededby/>
  <shortname>N/A</shortname>
  &stpeter;
  &ianpaterson;
  &ksmith;
  <revision>
    <version>0.5</version>
    <date>2008-02-06</date>
    <initials>psa</initials>
    <remark><p>Defined parent attribute and provided recommendations regarding creation of new threads and child threads.</p></remark>
  </revision>
  <revision>
    <version>0.4</version>
    <date>2007-08-30</date>
    <initials>psa</initials>
    <remark><p>Specified handling of thread IDs on groupchat messages.</p></remark>
  </revision>
  <revision>
    <version>0.3</version>
    <date>2007-01-29</date>
    <initials>ip</initials>
    <remark><p>Described handling of unavailable presence and chat messages without thread IDs; minor changes.</p></remark>
  </revision>
  <revision>
    <version>0.2</version>
    <date>2007-01-23</date>
    <initials>psa/ks</initials>
    <remark><p>Equalized treatment of different message types (chat and groupchat not preferred over normal); required the use of UUIDs; specified use of In-Reply-To header; added Kevin Smith as co-author.</p></remark>
  </revision>
  <revision>
    <version>0.1</version>
    <date>2006-12-20</date>
    <initials>psa</initials>
    <remark><p>Initial version.</p></remark>
  </revision>
  <revision>
    <version>0.0.2</version>
    <date>2006-12-14</date>
    <initials>psa</initials>
    <remark>Corrected SHIM example; added XMPP Registrar considerations.</remark>
  </revision>
  <revision>
    <version>0.0.1</version>
    <date>2006-12-13</date>
    <initials>psa/ip</initials>
    <remark>First draft.</remark>
  </revision>
</header>
<section1 topic='Introduction' anchor='intro'>
  <p>Although message threads are re-used in XMPP extension protocols such as &xep0085; and &xep0155;, the semantics of message threads have never been well specified (e.g., in &rfc3921;). This document attempts to clearly specify the meaning and handling of message threads for implementation by XMPP clients and for potential inclusion in &rfc3921bis;.</p>
</section1>
<section1 topic='Motivation' anchor='motivation'>
  <p>Threads matter because they enable XMPP clients to:</p>
  <ul>
    <li>Track conversation topics within the context of a one-to-one chat session, a &xep0045; room, or exchange of normal messages.</li>
    <li>Restart a conversation from message history.</li>
    <li>Enable clients to distinguish between different conversation threads when presenting a user's message, chat, and groupchat histories, thus providing a more coherent user interface (e.g. by collapsing threads to a single history entry).</li>
    <li>Separate logical sessions from physical interface objects such as windows.</li>
    <li>Route XMPP stanzas within a client (e.g., dispatching different content types to different windows), thus facilitating the creation of more robust plugin architectures.</li>
  </ul>
</section1>
<section1 topic='Definition' anchor='def'>
  <section2 topic='Syntax' anchor='syntax'>
    <p>Section 2.1.2.3 of <cite>RFC 3920</cite> currently states the following regarding the syntax of the ThreadID:</p>
    <p class='indent'>A message stanza MUST NOT contain more than one &lt;thread/&gt; element.  The &lt;thread/&gt; element MUST NOT possess any attributes....  The &lt;thread/&gt; element MUST NOT contain mixed content (as defined in Section 3.2.2 of XML).</p>
    <p>For the purpose of improved thread handling, we propose defining a 'parent' attribute that enables an application to identify the current thread as an offshoot or child of a previous thread. Therefore we suggest the following syntax definition:</p>
    <p class='indent'>The inclusion of the &lt;thread/&gt; element is OPTIONAL.  Because the &lt;thread/&gt; element uniquely identifies the particular conversation thread to which a message belongs, a message stanza MUST NOT contain more than one &lt;thread/&gt; element.</p>
    <p class='indent'>The &lt;thread/&gt; element MAY possess a 'parent' attribute that identifies another thread of which the current thread is an offshoot or child; the value of the 'parent' MUST conform to the syntax of the &lt;thread/&gt; element itself.  The &lt;thread/&gt; element MUST NOT contain mixed content (as defined in Section 3.2.2 of <xref target="XML"/>).</p>
  </section2>
  <section2 topic='Semantics' anchor='semantics'>
    <p>Section 2.1.2.3 of <cite>RFC 3921</cite> currently states the following regarding the semantics of the ThreadID:</p>
    <p class='indent'>The &lt;thread/&gt; element contains non-human-readable XML character data specifying an identifier that is used for tracking a conversation thread (sometimes referred to as an "instant messaging session") between two entities....  The value of the &lt;thread/&gt; element MUST be treated as opaque by entities; no semantic meaning may be derived from it, and only exact comparisons may be made against it.</p>
    <p>The description in <cite>RFC 3921</cite> is deemed to be too limiting, since it ignores the potential use of the ThreadID when exchanging message stanzas of types other than "chat". Therefore we propose the following description:</p>
    <p class='indent'>The primary use of the XMPP &lt;thread/&gt; element is to uniquely identify a conversation thread or "chat session" between two entities instantiated by &lt;message/&gt; stanzas of type 'chat'.  However, the XMPP &lt;thread/&gt; element may also be used to uniquely identify an analogous thread between two entities instantiated by &lt;message/&gt; stanzas of type 'headline' or 'normal', or among multiple entities in the context of a multi-user chat room instantiated by &lt;message/&gt; stanzas of type 'groupchat'. It may also be used for &lt;message/&gt; stanzas not related to a conversation, such as a game session or between plugins.  The &lt;thread/&gt; element is not used to identify individual messages, only conversations.</p>
  </section2>
  <section2 topic='Uniqueness' anchor='unique'>
    <p>Section 2.1.2.3 of <cite>RFC 3921</cite> currently states the following uniqueness requirement:</p>
    <p class='indent'>The value of the &lt;thread/&gt; element ... MUST be unique to that conversation thread within the stream and MUST be consistent throughout that conversation (a client that receives a message from the same full JID but with a different thread ID MUST assume that the message in question exists outside the context of the existing conversation thread).</p>
    <p>The uniqueness requirement in <cite>RFC 3921</cite> is not deemed strong enough since it is desirable that a ThreadID could be used to (for instance) restart a conversation at a later date. Therefore we propose the following uniqueness requirement:</p>
    <p class='indent'>The value of the &lt;thread/&gt; element is not human-readable and MUST be treated as opaque by entities; no semantic meaning may be derived from it, and only exact comparisons may be made against it.  The value of the &lt;thread/&gt; element MUST be a universally unique identifier (UUID) as described in <xref target='UUID'/>.</p>
  </section2>
</section1>
<section1 topic='Generation' anchor='generation'>
  <section2 topic='Inclusion' anchor='inclusion'>
    <p>Depending on the type of the message (i.e., the value of the 'type' attribute), the &lt;thread/&gt; should be included as follows:</p>
    <table caption='When to Include Threads'>
      <tr>
        <th>Message Type</th>
        <th>Inclusion</th>
      </tr>
      <tr>
        <td>chat</td>
        <td>RECOMMENDED</td>
      </tr>
      <tr>
        <td>groupchat</td>
        <td>RECOMMENDED</td>
      </tr>
      <tr>
        <td>headline</td>
        <td>OPTIONAL</td>
      </tr>
      <tr>
        <td>normal</td>
        <td>OPTIONAL</td>
      </tr>
    </table>
  </section2>
  <section2 topic='New Threads' anchor='new'>
    <p>Unless a &lt;message/&gt; stanza is written in direct reply to another &lt;message/&gt; stanza, if a ThreadID is included then its value SHOULD be newly generated if a a human user initiates a chat conversation with another user (i.e., a &lt;message/&gt; stanza of type 'chat'), starts a new conversation in the context of a multi-user chat environment (i.e., a &lt;message/&gt; stanza of type 'groupchat'), or sends a normal message.</p>
    <p>If the &lt;message/&gt; stanza is written in directly reply to another &lt;message/&gt; stanza, then the ThreadID should be the value from the the original &lt;message/&gt; stanza.</p>
    <p>Determining what constitutes a &lt;message/&gt; stanza written in reply to another is a matter left to individual implementation, but it is envisaged that in most cases it would be the result of, e.g., the user clicking a 'reply' button when reading the contents of the previous stanza; alternatively, the entity that replies can include an "In-Reply-To" header as described in the <link url='#impl'>Implementation Notes</link> section of this document.</p>
  </section2>
  <section2 topic='Child Threads' anchor='child'>
    <p>In some situations, the conversation veers from the original topic. In this situation, it can be sensible to generate a new thread that is an offshoot or child of the original thread. The connection of the child thread to the parent thread SHOULD be indicated by including the original ThreadID as the value of the 'parent' attribute.</p>
    <example caption='Message with ID'><![CDATA[
<message
    to='romeo@example.net/orchard'
    from='juliet@example.com/balcony'
    id='asiwe8289ljfdalk'
    type='chat'
    xml:lang='en'>
  <body>Art thou not Romeo, and a Montague?</body>
  <thread parent='7edac73ab41e45c4aafa7b2d7b749080'>
    e0ffe42b28561960c6b12b944a092794b9683a38
  </thread>
</message>
    ]]></example>
  </section2>
</section1>
<section1 topic='Handling' anchor='handling'>
  <section2 topic='Chat Messages' anchor='chat'>
    <p>In the context of &lt;message/&gt; stanzas of type "chat" exchanged between two entities, the value of the &lt;thread/&gt; element shall be considered equivalent to a unique identifier for the chat session or conversation thread. If an entity receives such a message with a new or unknown ThreadID, it SHOULD treat the message as part of a session with unnegotiated parameters (i.e., as equivalent to the first message in a chat session that has been negotiated via <cite>XEP-0155</cite> with no parameters specified). An entity SHOULD destroy the thread when it sends or receives a <cite>XEP-0155</cite> "terminate" stanza (such a stanza SHOULD be sent even for sessions that were not negotitated with <cite>XEP-0155</cite>) and MAY destroy the thread when it goes offline, but SHOULD NOT destroy the thread if a human user merely disengages from the chat session (e.g., by closing a window in a client interface).</p>
    <p>If an entity receives an XMPP presence stanza of type "unavailable" from the other entity during a chat session, it SHOULD NOT destroy the thread; instead, it SHOULD assume that the other entity will still be able to continue the session (perhaps the other entity simply became "invisible", was temporarily disconnected by a network error, or is persisting the state of the session until it reconnects and receives "offline" messages).</p>
    <p>If an entity receives a message of type "chat" without a thread ID then:</p>
    <ul>
      <li>If the receiver has no sessions open with the sender (full JID), or if all the open sessions have already <em>received</em> a message that included the session's thread ID, then the receiver should create a new session with a new thread ID (and include that thread ID in all the messages it sends within the session).</li>
      <li>If the receiver has a session open with the sender within which it has never received any message that included the session's thread ID, then it should consider the message to be part of that session. If more than one of such sessions exist, then the message should be considered part of the session in which the receiver last sent a message.</li>
    </ul>
  </section2>
  <section2 topic='Groupchat Messages' anchor='groupchat'>
    <p>In the context of &lt;message/&gt; stanzas of type "groupchat" exchanged between multiple entities in a &xep0045; room or similar environment, the value of the &lt;thread/&gt; element shall be considered equivalent to a unique identifier for a conversation thread in the multi-user environment.</p>
    <p>When displaying threaded groupchat conversation within a user interface, a client SHOULD provide a visual indication of the thread to which a message belongs. Methods for such indications include (non-exhaustively) the grouping together of all messages from the same thread, providing an index of threads, or formatting all messages within a thread in a cohesive manner, e.g. with uniform coloring.</p>
  </section2>
  <section2 topic='Headline Messages' anchor='headline'>
    <p>There are no special handling requirements related to threads in the context of &lt;message/&gt; stanzas of type "headline".</p>
  </section2>
  <section2 topic='Normal Messages' anchor='normal'>
    <p>There are no special handling requirements related to threads in the context of &lt;message/&gt; stanzas of type "headline".</p>
  </section2>
  <section2 topic='Messages That Have Been Archived' anchor='archived'>
    <p>When displaying historical conversations within a user interface, a client SHOULD provide a visual indication of the thread to which a message belongs. Methods for such indications include (non-exhaustively) the grouping together of all messages from the same thread, providing an index of threads, or formatting all messages within a thread in a cohesive manner, e.g. with uniform coloring.</p>
  </section2>
</section1>
<section1 topic='SHIM Header' anchor='shim'>
  <p>In some contexts it may be desirable to enforce thread-like semantics when exchanging XMPP &lt;iq/&gt; stanzas. Because <cite>RFC 3920</cite> disallows more than one direct child element of the &lt;iq/&gt; stanza, it is not possible to include the &lt;thread/&gt; element for tracking purposes. Therefore we define a "ThreadID" &xep0131; header with the same semantics as the &lt;thread/&gt; element, but with the syntax of a SHIM header:</p>
  <example caption='ThreadID header'><![CDATA[
<iq from='romeo@montague.net/home'
    to='joogle@botster.shakespeare.lit'
    type='get'
    id='create1'>
  <command xmlns='http://jabber.org/protocol/commands'
           node='create'
           action='execute'>
    <headers xmlns='http://jabber.org/protocol/shim'>
      <header name='ThreadID'>e0ffe42b28561960c6b12b944a092794b9683a38</header>
    </headers>
  </command>
</iq>
  ]]></example>
</section1>
<section1 topic='Implementation Notes' anchor='impl'>
  <p>An entity that needs to track replies to particular messages may do so by including an 'id' attribute with the &MESSAGE; stanza.</p>
  <example caption='Message with ID'><![CDATA[
<message
    to='romeo@example.net/orchard'
    from='juliet@example.com/balcony'
    id='asiwe8289ljfdalk'
    type='chat'
    xml:lang='en'>
  <body>Art thou not Romeo, and a Montague?</body>
  <thread>e0ffe42b28561960c6b12b944a092794b9683a38</thread>
</message>
  ]]></example>
  <p>The entity that replies then MAY include an "In-Reply-To" SHIM header:</p>
  <example caption='Reply'><![CDATA[
<message
    to='juliet@example.com/balcony'
    from='romeo@example.net/orchard'
    type='chat'
    id='pertio4387435ilq'
    xml:lang='en'>
  <body>Neither, fair saint, if either thee dislike.</body>
  <thread>e0ffe42b28561960c6b12b944a092794b9683a38</thread>
  <headers xmlns='http://jabber.org/protocol/shim'>
    <header name='In-Reply-To'>asiwe8289ljfdalk</header>
  </headers>
</message>
  ]]></example>
</section1>
<section1 topic='Security Considerations' anchor='security'>
  <p>An application that generates the UUID used as the ThreadID MUST ensure that the UUID does not reveal identifying information about the entity (e.g., the MAC address of the device on which the XMPP application is running).</p>
</section1>
<section1 topic='IANA Considerations' anchor='iana'>
  <p>This document requires no interaction with &IANA;.</p>
</section1>
<section1 topic='XMPP Registrar Considerations' anchor='registrar'>
  <section2 topic='SHIM Headers Registry' anchor='registrar-shim'>
    <p>The XMPP Registrar shall add "ThreadID" to its registry of SHIM headers (see &SHIMHEADERS;). The submission is as follows:</p>
    <code><![CDATA[
<header>
  <name>ThreadID</name>
  <desc>
    This header has the same semantics as the thread child element
    of the XMPP message stanza but is for use in IQ stanzas.
  </desc>
  <doc>XEP-0201</doc>
</header>
    ]]></code>
  </section2>
</section1>
</xep>
