No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xep-0198.xml 41KB

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!DOCTYPE xep SYSTEM 'xep.dtd' [
  3. <!ENTITY % ents SYSTEM 'xep.ent'>
  4. %ents;
  5. ]>
  6. <?xml-stylesheet type='text/xsl' href='xep.xsl'?>
  7. <xep>
  8. <header>
  9. <title>Stream Management</title>
  10. <abstract>This specification defines an XMPP protocol extension for active management of an XML stream between two XMPP entities, including features for stanza acknowledgements and stream resumption.</abstract>
  12. <number>0198</number>
  13. <status>Draft</status>
  14. <type>Standards Track</type>
  15. <sig>Standards</sig>
  16. <dependencies>
  17. <spec>XMPP Core</spec>
  18. </dependencies>
  19. <supersedes/>
  20. <supersededby/>
  21. <shortname>sm</shortname>
  22. <schemaloc>
  23. <url></url>
  24. </schemaloc>
  25. &infiniti;
  26. &stpeter;
  27. &hildjj;
  28. &fabio;
  29. &dcridland;
  30. &mwild;
  31. <revision>
  32. <version>1.5.2</version>
  33. <date>2016-12-08</date>
  34. <initials>cs (XEP Editor: ssw)</initials>
  35. <remark><p>Send 'a' element before stream closure.</p></remark>
  36. </revision>
  37. <revision>
  38. <version>1.5.1</version>
  39. <date>2016-09-30</date>
  40. <initials>ssw</initials>
  41. <remark><p>Fix example syntax highlighting and formatting.</p></remark>
  42. </revision>
  43. <revision>
  44. <version>1.5</version>
  45. <date>2015-09-13</date>
  46. <initials>dg</initials>
  47. <remark>
  48. <ul>
  49. <li>Noted that the server MAY include a 'h' attribute when failing to resume a session because it has been timed out</li>
  50. </ul>
  51. </remark>
  52. </revision>
  53. <revision>
  54. <version>1.4rc1</version>
  55. <date>2015-07-27</date>
  56. <initials>dc</initials>
  57. <remark>
  58. <ul>
  59. <li>Expressed how to handle duplicate enable requests.</li>
  60. <li>Noted the use of delay stamping in redelivery/offline messaging by servers.</li>
  61. </ul>
  62. </remark>
  63. </revision>
  64. <revision>
  65. <version>1.3</version>
  66. <date>2011-06-29</date>
  67. <initials>psa/mw</initials>
  68. <remark>
  69. <ul>
  70. <li>Specified that 'h' is REQUIRED when sending &lt;resume/&gt; or &lt;resumed/&gt;, and clarified how to set its value</li>
  71. <li>Mentioned the possibility of duplicate stanzas</li>
  72. <li>Clarified terminology to use 'client' and 'server' instead of 'initiating entity' and 'receiving entity'</li>
  73. <li>Added note about the order of events in the stream negotiation process</li>
  74. <li>Corrected the value of 'h' in several examples</li>
  75. <li>Removed an extraneous 'stanzas' attribute from one example</li>
  76. <li>Corrected the schema</li>
  77. </ul>
  78. </remark>
  79. </revision>
  80. <revision>
  81. <version>1.2</version>
  82. <date>2011-03-02</date>
  83. <initials>psa/dc/mw</initials>
  84. <remark><p>Simplification based on implementation experience: removed acking per number of stanzas exchanged because either entity can request an ack at any time; moved throttling feature to a separate specification; removed 'stanzas' attribute from &lt;enable/&gt; element; added 'location' attribute to &lt;enabled/&gt; element; clarified several implementation issues in the text; fixed several examples; versioned the XML namespace from urn:xmpp:sm:2 to urn:xmpp:sm:3.</p></remark>
  85. </revision>
  86. <revision>
  87. <version>1.1</version>
  88. <date>2010-03-05</date>
  89. <initials>psa/jk</initials>
  90. <remark><p>Corrected value of 'h' so that zero means no stanzas have yet been handled; clarified distinction between a cleanly closed stream and an unfinished stream.</p></remark>
  91. </revision>
  92. <revision>
  93. <version>1.0</version>
  94. <date>2009-06-17</date>
  95. <initials>psa</initials>
  96. <remark><p>Per a vote of the XMPP Council, advanced specification from Experimental to Draft.</p></remark>
  97. </revision>
  98. <revision>
  99. <version>0.10</version>
  100. <date>2009-06-11</date>
  101. <initials>psa</initials>
  102. <remark><p>Editorial review.</p></remark>
  103. </revision>
  104. <revision>
  105. <version>0.9</version>
  106. <date>2009-06-03</date>
  107. <initials>psa</initials>
  108. <remark>
  109. <ul>
  110. <li>Specified that the value of the 'h' attribute starts at zero, not one.</li>
  111. <li>Specified that the 'h' attribute is an unsignedInt and that it loops back to zero when reaching 2^32.</li>
  112. <li>Added security consideration regarding session resumption and removed security consideration regarding proxies.</li>
  113. <li>Clarified the meaning of handled as defining which entity has responsibility for a stanza.</li>
  114. <li>Corrected schema and examples.</li>
  115. </ul>
  116. </remark>
  117. </revision>
  118. <revision>
  119. <version>0.8</version>
  120. <date>2009-04-09</date>
  121. <initials>ff/jk/jjh/psa</initials>
  122. <remark>
  123. <ul>
  124. <li>Added &lt;t/&gt; element for throttling notifications, including 'stanzas' attribute for dynamic adjustment of the stanzas window.</li>
  125. <li>Simplified protocol by making the sequence number increment per stanza and removing the u attribute (which is now unnecessary).</li>
  126. <li>Removed 'h' attribute from &lt;r/&gt; element.</li>
  127. <li>Added 'max' and 'stanzas' attributes to both &lt;enable/&gt; and &lt;enabled/&gt; elements, and removed those attributes from the &lt;sm/&gt; element.</li>
  128. <li>Incremented the protocol version from 1 to 2.</li>
  129. <li>Added in-depth usage scenarios.</li>
  130. </ul>
  131. </remark>
  132. </revision>
  133. <revision>
  134. <version>0.7</version>
  135. <date>2009-03-30</date>
  136. <initials>jjh/psa</initials>
  137. <remark><p>Removed pings (use XEP-0199, whitespace pings, or TCP keepalives instead); removed section on throttling, since it is unworkable.</p></remark>
  138. </revision>
  139. <revision>
  140. <version>0.6</version>
  141. <date>2009-03-19</date>
  142. <initials>psa</initials>
  143. <remark>
  144. <ul>
  145. <li>Incremented protocol version from 0 to 1.</li>
  146. <li>Changed attribute names from c (?) and b (?) to u (unacknowledged) and h (handled).</li>
  147. <li>Added stanzas attribute to specify maximum number of stanzas between acking requests.</li>
  148. <li>More clearly defined error handling using &lt;failed/&gt; element plus stanza error conditions.</li>
  149. <li>Defined error handling for &lt;ping/&gt; element by allowing stanza error conditions in &lt;pong/&gt; element.</li>
  150. <li>More clearly specified maximum reconnect time.</li>
  151. <li>Added detailed scenarios for basic acking and for outbound and inbound throttling.</li>
  152. </ul>
  153. </remark>
  154. </revision>
  155. <revision>
  156. <version>0.5</version>
  157. <date>2008-09-29</date>
  158. <initials>psa</initials>
  159. <remark><p>Removed recommendation to use namespace prefixes; modified namespace to incorporate namespace versioning.</p></remark>
  160. </revision>
  161. <revision>
  162. <version>0.4</version>
  163. <date>2008-09-08</date>
  164. <initials>jjh/jk/psa</initials>
  165. <remark><p>Added support for session resumption; re-organized the document; changed name to stream management; changed provisional namespace.</p></remark>
  166. </revision>
  167. <revision>
  168. <version>0.3</version>
  169. <date>2007-10-03</date>
  170. <initials>jk</initials>
  171. <remark><p>Updates per devcon discussion.</p></remark>
  172. </revision>
  173. <revision>
  174. <version>0.2</version>
  175. <date>2007-04-05</date>
  176. <initials>jk</initials>
  177. <remark><p>Require c attribute on &lt;r/&gt; element. Describe minimal implementation. Switch to standard temporary namespace.</p></remark>
  178. </revision>
  179. <revision>
  180. <version>0.1</version>
  181. <date>2006-11-21</date>
  182. <initials>psa</initials>
  183. <remark><p>Initial published version.</p></remark>
  184. </revision>
  185. <revision>
  186. <version>0.0.3</version>
  187. <date>2006-11-08</date>
  188. <initials>jk</initials>
  189. <remark>New version, using sequence numbers.</remark>
  190. </revision>
  191. <revision>
  192. <version>0.0.2</version>
  193. <date>2004-12-11</date>
  194. <initials>jk</initials>
  195. <remark>Further clarification, allow acking many stanzas at once.</remark>
  196. </revision>
  197. <revision>
  198. <version>0.0.1</version>
  199. <date>2004-08-09</date>
  200. <initials>jk</initials>
  201. <remark>First draft.</remark>
  202. </revision>
  203. </header>
  204. <section1 topic='Introduction' anchor='intro'>
  205. <p>&xmppcore; defines the fundamental streaming XML technology used by XMPP (i.e., stream establishment and termination including authentication and encryption). However, the core XMPP specification does not provide tools for actively managing a live XML stream.</p>
  206. <p>The basic concept behind stream management is that the initiating entity (either a client or a server) and the receiving entity (a server) can exchange "commands" for active management of the stream. The following stream management features are of particular interest because they are expected to improve network reliability and the end-user experience:</p>
  207. <ul>
  208. <li>Stanza Acknowledgements -- the ability to know if a stanza or series of stanzas has been received by one's peer.</li>
  209. <li>Stream Resumption -- the ability to quickly resume a stream that has been terminated.</li>
  210. </ul>
  211. <p>Stream management implements these features using short XML elements at the root stream level. These elements are not "stanzas" in the XMPP sense (i.e., not &IQ;, &MESSAGE;, or &PRESENCE; stanzas as defined in <cite>RFC 6120</cite>) and are not counted or acked in stream management, since they exist for the purpose of managing stanzas themselves.</p>
  212. <p>Stream management is used at the level of an XML stream. To check TCP connectivity underneath a given stream, it is RECOMMENDED to use whitespace keepalives (see <cite>RFC 6120</cite>), &xep0199;, or TCP keepalives. By contrast with stream management, &xep0079; and &xep0184; define acks that are sent end-to-end over multiple streams; these facilities are useful in special scenarios but are unnecessary for checking of a direct stream between two XMPP entities.</p>
  213. <p>Note: Stream Management can be used for server-to-server streams as well as for client-to-server streams. However, for convenience this specification discusses client-to-server streams only. The same principles apply to server-to-server streams.</p>
  214. </section1>
  215. <section1 topic='Stream Feature' anchor='feature'>
  216. <p>The server returns a stream header to the client along with stream features, where the features include an &lt;sm/&gt; element qualified by the 'urn:xmpp:sm:3' namespace &VNOTE;.</p>
  217. <p>Note: The client cannot negotiate stream management until it has authenticated with the server and has bound a resource; see below for specific restrictions.</p>
  218. <example caption='Server sends new stream header along with stream features'><![CDATA[
  219. <stream:stream
  220. from=''
  221. xmlns='jabber:client'
  222. xmlns:stream=''
  223. version='1.0'>
  224. <stream:features>
  225. <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
  226. <sm xmlns='urn:xmpp:sm:3'/>
  227. </stream:features>
  228. ]]></example>
  229. </section1>
  230. <section1 topic='Enabling Stream Management' anchor='enable'>
  231. <p>To enable use of stream management, the client sends an &lt;enable/&gt; command to the server.</p>
  232. <example caption='Client enables stream management'><![CDATA[
  233. <enable xmlns='urn:xmpp:sm:3'/>
  234. ]]></example>
  235. <p>If the client wants to be allowed to resume the stream, it includes a boolean 'resume' attribute, which defaults to false &BOOLEANNOTE;. For information about resuming a previous session, see the <link url='#resumption'>Resumption</link> section of this document.</p>
  236. <p>The &lt;enable/&gt; element MAY include a 'max' attribute to specify the client's preferred maximum resumption time in seconds.</p>
  237. <p>Upon receiving the enable request, the server MUST reply with an &lt;enabled/&gt; element or a &lt;failed/&gt; element qualified by the 'urn:xmpp:sm:3' namespace. The &lt;failed/&gt; element indicates that there was a problem establishing the stream management "session". The &lt;enabled/&gt; element indicates successful establishment of the stream management session.</p>
  238. <example caption='Server enables stream management'><![CDATA[
  239. <enabled xmlns='urn:xmpp:sm:3'/>
  240. ]]></example>
  241. <p>The parties can then the use stream management features defined below.</p>
  242. <p>If the server allows session resumption, it MUST include a 'resume' attribute set to a value of "true" or "1" &BOOLEANNOTE;.</p>
  243. <example caption='Server enables stream management with session resumption'><![CDATA[
  244. <enabled xmlns='urn:xmpp:sm:3' id='some-long-sm-id' resume='true'/>
  245. ]]></example>
  246. <p>The &lt;enabled/&gt; element MAY include a 'max' attribute to specify the server's preferred maximum resumption time.</p>
  247. <p>The &lt;enabled/&gt; element MAY include a 'location' attribute to specify the server's preferred IP address or hostname (optionally with a port) for reconnection, in the form specified in Section of <cite>RFC 6120</cite> (i.e., "domainpart:port", where IPv6 addresses are enclosed in square brackets "[...]" as described in &rfc5952;); if reconnection to that location fails, the standard XMPP connection algorithm specified in <cite>RFC 6120</cite> applies.</p>
  248. <p>The client MUST NOT attempt to negotiate stream management until it is authenticated; i.e., it MUST NOT send an &lt;enable/&gt; element until after authentication (such as SASL, &xep0078; or &xep0220;) has been completed successfully.</p>
  249. <p>For client-to-server connections, the client MUST NOT attempt to enable stream management until after it has completed Resource Binding <em>unless it is resuming a previous session</em> (see <link url='#resumption'>Resumption</link>).</p>
  250. <p>The server SHALL enforce this order and return a &lt;failed/&gt; element in response if the order is violated (see <link url='#errors'>Error Handling</link>).</p>
  251. <example caption='Server returns error if client attempts to enable stream management before resource binding'><![CDATA[
  252. <failed xmlns='urn:xmpp:sm:3'>
  253. <unexpected-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  254. </failed>
  255. ]]></example>
  256. <p>Note that a client SHALL only make at most one attempt to enable stream management. If a server receives a second &lt;enable/> element it SHOULD respond with a stream error, thus terminating the client connection.</p>
  257. </section1>
  258. <section1 topic='Acks' anchor='acking'>
  259. <p>After enabling stream management, the client or server can send ack elements at any time over the stream. An ack element is one of the following:</p>
  260. <ul>
  261. <li>The &lt;a/&gt; element is used to <strong>answer</strong> a request for acknowledgement or to send an unrequested ack.</li>
  262. <li>The &lt;r/&gt; element is used to <strong>request</strong> acknowledgement of received stanzas.</li>
  263. </ul>
  264. <p>The following attribute is defined:</p>
  265. <ul>
  266. <li>The 'h' attribute identifies the last <strong>handled</strong> stanza (i.e., the last stanza that the server will acknowledge as having received).</li>
  267. </ul>
  268. <p>An &lt;a/&gt; element MUST possess an 'h' attribute.</p>
  269. <p>The &lt;r/&gt; element has no defined attributes.</p>
  270. <p class='def'><strong>Definition:</strong> Acknowledging a previously-received ack element indicates that the stanza(s) sent since then have been "handled" by the server. By "handled" we mean that the server has accepted responsibility for a stanza or stanzas (e.g., to process the stanza(s) directly, deliver the stanza(s) to a local entity such as another connected client on the same server, or route the stanza(s) to a remote entity at a different server); until a stanza has been affirmed as handled by the server, that stanza is the responsibility of the sender (e.g., to resend it or generate an error if it is never affirmed as handled by the server).</p>
  271. <p>Receipt of an &lt;r/&gt; element does not imply that new stanzas have been transmitted by the peer; receipt of an &lt;a/&gt; element only indicates that new stanzas have been processed if the 'h' attribute has been incremented.</p>
  272. <p>The value of 'h' starts at zero at the point stream management is enabled or requested to be enabled (see note below). The value of 'h' is then incremented to one for the first stanza handled and incremented by one again with each subsequent stanza handled. In the unlikely case that the number of stanzas handled during a stream management session exceeds the number of digits that can be represented by the unsignedInt datatype as specified in &w3xmlschema2; (i.e., 2<span class='super'>32</span>), the value of 'h' SHALL be reset from 2<span class='super'>32</span>-1 back to zero (rather than being incremented to 2<span class='super'>32</span>).</p>
  273. <p class='box'>Note: There are two values of 'h' for any given stream: one maintained by the client to keep track of stanzas it has handled from the server, and one maintained by the server to keep track of stanzas it has handled from the client. The client initializes its value to zero when it sends &lt;enable/&gt; to the server, and the server initializes its value to zero when it sends &lt;enabled/&gt; to the client (it is expected that the server will respond immediately to &lt;enable/&gt; and set its counter to zero at that time). After this initialization, the client increments its value of 'h' for each stanza it handles from server, and the server increments its value of 'h' for each stanza it handles from the client.</p>
  274. <p>The following annotated example shows a message sent by the client, a request for acknowledgement, and an ack of the stanza.</p>
  275. <example caption='Simple stanza acking'><![CDATA[
  276. <!-- Client -->
  277. <enable xmlns='urn:xmpp:sm:3'/>
  278. <!-- Client sets outbound count to zero. -->
  279. <message from=''
  280. to=''
  281. xml:lang='en'>
  282. <body>
  283. I'll send a friar with speed, to Mantua,
  284. with my letters to thy lord.
  285. </body>
  286. </message>
  287. <!-- Note that client need not wait for a response. -->
  288. <!-- Server -->
  289. <enabled xmlns='urn:xmpp:sm:3'/>
  290. <!--
  291. Server receives enable, and responds,
  292. setting both inbound and outbound counts
  293. to zero.
  294. In addition, client sets inbound count to zero.
  295. -->
  296. <!-- Client -->
  297. <r xmlns='urn:xmpp:sm:3'/>
  298. <!-- Server -->
  299. <a xmlns='urn:xmpp:sm:3' h='1'/>
  300. ]]></example>
  301. <p>When an &lt;r/&gt; element ("request") is received, the recipient MUST acknowledge it by sending an &lt;a/&gt; element to the sender containing a value of 'h' that is equal to the number of stanzas handled by the recipient of the &lt;r/&gt; element. The response SHOULD be sent as soon as possible after receiving the &lt;r/&gt; element, and MUST NOT be withheld for any condition other than a timeout. For example, a client with a slow connection might want to collect many stanzas over a period of time before acking, and a server might want to throttle incoming stanzas. The sender does not need to wait for an ack to continue sending stanzas.</p>
  302. <p>Either party MAY send an &lt;a/&gt; element at any time (e.g., after it has received a certain number of stanzas, or after a certain period of time), even if it has not received an &lt;r/&gt; element from the other party.
  303. It is RECOMMENDED that initiating entities (usually clients) send an &lt;a/&gt; element right before they gracefully close the stream, in order to inform the peer about received stanzas. Otherwise it can happen that stanzas are re-sent (usually by the server) although they were actually received.
  304. </p>
  305. <p>When a party receives an &lt;a/&gt; element, it SHOULD keep a record of the 'h' value returned as the sequence number of the last handled outbound stanza for the current stream (and discard the previous value).</p>
  306. <p>If a stream ends and it is not resumed within the time specified in the original &lt;enabled/&gt; element, the sequence number and any associated state MAY be discarded by both parties. Before the session state is discarded, implementations SHOULD take alternative action regarding any unhandled stanzas (i.e., stanzas sent after the most recent 'h' value received):</p>
  307. <ul>
  308. <li>A server SHOULD treat unacknowledged stanzas in the same way that it would treat a stanza sent to an unavailable resource, by either returning an error to the sender, delivery to an alternate resource, or committing the stanza to offline storage. (Note that servers SHOULD add a delay element with the original (failed) delivery timestamp, as per &xep0203;).</li>
  309. <li>A user-oriented client SHOULD try to silently resend the stanzas upon reconnection or inform the user of the failure via appropriate user-interface elements. Clients SHOULD also add a delay element with the original (failed) send timestamp, so that the original send date is preserved. Otherwise receiving clients could only display the reconnection timestamp of the sending client, which may confuse users.</li>
  310. </ul>
  311. <p>Because unacknowledged stanzas might have been received by the other party, resending them might result in duplicates; there is no way to prevent such a result in this protocol, although use of the XMPP 'id' attribute on all stanzas can at least assist the intended recipients in weeding out duplicate stanzas.</p>
  312. </section1>
  313. <section1 topic='Resumption' anchor='resumption'>
  314. <p>It can happen that an XML stream is terminated unexpectedly (e.g., because of network outages). In this case, it is desirable to quickly resume the former stream rather than complete the tedious process of stream establishment, roster retrieval, and presence broadcast.</p>
  315. <p>In addition, this protocol exchanges the sequence numbers of the last received stanzas on the previous connection, allowing entities to establish definitively which stanzas require retransmission and which do not, eliminating duplication through replay.</p>
  316. <p>To request that the stream will be resumable, when enabling stream management the client MUST add a 'resume' attribute to the &lt;enable/&gt; element with a value of "true" or "1" &BOOLEANNOTE;.</p>
  317. <example caption='Client enables stream management'><![CDATA[
  318. <enable xmlns='urn:xmpp:sm:3' resume='true'/>
  319. ]]></example>
  320. <p>If the server will allow the stream to be resumed, it MUST include a 'resume' attribute set to "true" or "1" on the &lt;enabled/&gt; element and MUST include an 'id' attribute that specifies an identifier for the stream.</p>
  321. <example caption='Server allows stream resumption'><![CDATA[
  322. <enabled xmlns='urn:xmpp:sm:3' id='some-long-sm-id' resume='true'/>
  323. ]]></example>
  324. <p class='def'><strong>Definition:</strong> The 'id' attribute defines a unique identifier for purposes of stream management (an "SM-ID"). The SM-ID MUST be generated by the server. The client MUST consider the SM-ID to be opaque and therefore MUST NOT assign any semantic meaning to the SM-ID. The server MAY encode any information it deems useful into the SM-ID, such as the full JID &LOCALFULL; of a connected client (e.g., the full JID plus a nonce value). Any characters allowed in an XML attribute are allowed. The SM-ID MUST NOT be reused for simultaneous or subsequent sessions (but the server need not ensure that SM-IDs are unique for all time, only for as long as the server is continuously running). The SM-ID SHOULD NOT be longer than 4000 bytes.</p>
  325. <p>As noted, the &lt;enabled/&gt; element MAY include a 'location' attribute that specifies the server's preferred location for reconnecting (e.g., a particular connection manager that hold session state for the connected client).</p>
  326. <example caption='Server prefers reconnection at a particular location'><![CDATA[
  327. <enabled xmlns='urn:xmpp:sm:3'
  328. id='some-long-sm-id'
  329. location='[2001:41D0:1:A49b::1]:9222'
  330. resume='true'/>
  331. ]]></example>
  332. <p>If the stream is terminated unexpectedly, the client would then open a TCP connection to the server. The order of events is as follows:</p>
  333. <ol start='1'>
  334. <li>After disconnection, the client opens a new TCP connection to the server, preferring the address specified in the 'location' attribute (if any).</li>
  335. <li>Client sends initial stream header.</li>
  336. <li>Server sends response stream header.</li>
  337. <li>Server sends stream features.</li>
  338. <li>Client sends STARTTLS request.</li>
  339. <li>Server informs client to proceed with the TLS negotiation.</li>
  340. <li>The parties complete a TLS handshake. (Note: When performing session resumption and also utilizing TLS, it is RECOMMENDED to take advantage of TLS session resumption &rfc5077; to further optimize the resumption of the XML stream.)</li>
  341. <li>Client sends new initial stream header.</li>
  342. <li>Server sends response stream header.</li>
  343. <li>Server sends stream features, requiring SASL negotiation and offering appropriate SASL mechanisms. (Note: If the server considers the information provided during TLS session resumption to be sufficient authentication, it MAY offer the SASL EXTERNAL mechanism; for details, refer to &sasltls;.)</li>
  344. <li>The parties complete SASL negotiation.</li>
  345. <li>Client sends new initial stream header.</li>
  346. <li>Server sends response stream header.</li>
  347. <li>Server sends stream features, offering the SM feature.</li>
  348. <li>Client requests resumption of the former stream.</li>
  349. </ol>
  350. <p>Note: The order of events might differ from those shown above, depending on when the server offers the SM feature, whether the client chooses STARTTLS, etc. Furthermore, in practice server-to-server streams often do not complete SASL negotiation or even TLS negotiation. The foregoing text does not modify any rules about the stream negotiation process specified in <cite>RFC 6120</cite>. However, since stream management applies to the exchange of stanzas (not any other XML elements), it makes sense for the server to offer the SM feature when it will be possible for the other party to start sending stanzas, not before. See also &xep0170;.</p>
  351. <p>To request resumption of the former stream, the client sends a &lt;resume/&gt; element qualified by the 'urn:xmpp:sm:3' namespace. The &lt;resume/&gt; element MUST include a 'previd' attribute whose value is the SM-ID of the former stream and MUST include an 'h' attribute that identifies the sequence number of the last handled stanza sent over the former stream from the server to the client (in the unlikely case that the client never received any stanzas, it would set 'h' to zero).</p>
  352. <example caption='Stream resumption request'><![CDATA[
  353. <resume xmlns='urn:xmpp:sm:3'
  354. h='some-sequence-number'
  355. previd='some-long-sm-id'/>
  356. ]]></example>
  357. <p>If the server can resume the former stream, it MUST return a &lt;resumed/&gt; element, which MUST include a 'previd' attribute set to the SM-ID of the former stream and MUST also include an 'h' attribute set to the sequence number of the last handled stanza sent over the former stream from the client to the server (in the unlikely case that the server never received any stanzas, it would set 'h' to zero).</p>
  358. <example caption='Stream resumed'><![CDATA[
  359. <resumed xmlns='urn:xmpp:sm:3'
  360. h='another-sequence-number'
  361. previd='some-long-sm-id'/>
  362. ]]></example>
  363. <p>If the server does not support session resumption, it MUST return a &lt;failed/&gt; element, which SHOULD include an error condition of &feature;. If the server does not recognize the 'previd' as an earlier session (e.g., because the former session has timed out), it MUST return a &lt;failed/&gt; element, which SHOULD include an error condition of &notfound;. If the server recogizes the 'previd' as an earlier session that has timed out the server MAY also include a 'h' attribute indicating the number of stanzas received before the timeout. (Note: For this to work the server has to store the SM-ID/sequence number tuple past the time out of the actual session.)</p>
  364. <example caption='Stream timed out'><![CDATA[
  365. <failed xmlns='urn:xmpp:sm:3'
  366. h='another-sequence-number'>
  367. <item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  368. </failed>
  369. ]]></example>
  370. <p>In both of these failure cases, the server SHOULD allow the client to bind a resource at this point rather than forcing the client to restart the stream negotiation process and re-authenticate.</p>
  371. <p>If the former stream is resumed and the server still has the stream for the previously-identified session open at this time, the old stream SHOULD be terminated.</p>
  372. <p>When a session is resumed, the parties proceed as follows:</p>
  373. <ul>
  374. <li>The sequence values are carried over from the previous session and are not reset for the new stream.</li>
  375. <li>Upon receiving a &lt;resume/&gt; or &lt;resumed/&gt; element the client and server use the 'h' attribute to retransmit any stanzas lost by the disconnection. In effect, it should handle the element's 'h' attribute as it would handle it on an &lt;a/&gt; element (i.e., marking stanzas in its outgoing queue as handled), except that after processing it MUST re-send to the peer any stanzas that are still marked as unhandled.</li>
  376. <li>Both parties SHOULD retransmit any stanzas that were not handled during the previous session, based on the sequence number reported by the peer.</li>
  377. <li>A reconnecting client SHOULD NOT request the roster, because any roster changes that occurred while the client was disconnected will be sent to the client after the stream management session resumes.</li>
  378. <li>The client SHOULD NOT resend presence stanzas in an attempt to restore its former presence state, since this state will have been retained by the server.</li>
  379. <li>Both parties SHOULD NOT try to re-establish state information (e.g., &xep0030; information).</li>
  380. </ul>
  381. </section1>
  382. <section1 topic='Error Handling' anchor='errors'>
  383. <p>If an error occurs with regard to an &lt;enable/&gt; or &lt;resume/&gt; element, the server MUST return a &lt;failed/&gt; element. This element SHOULD contain an error condition, which MUST be one of the stanza error conditions defined in <cite>RFC 6120</cite>.</p>
  384. <p>An example follows.</p>
  385. <example caption='Server returns error'><![CDATA[
  386. <failed xmlns='urn:xmpp:sm:3'>
  387. <unexpected-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  388. </failed>
  389. ]]></example>
  390. <p>Stream management errors SHOULD be considered recoverable; however, misuse of stream management MAY result in termination of the stream.</p>
  391. </section1>
  392. <section1 topic='Stream Closure' anchor='closure'>
  393. <p>A cleanly closed stream differs from an unfinished stream. If a client wishes to cleanly close its stream and end its session, it MUST send a &lt;/stream:stream&gt; so that the server can send unavailable presence on the client's behalf.</p>
  394. <p>If the stream is not cleanly closed then the server SHOULD consider the stream to be unfinished (even if the client closes its TCP connection to the server) and SHOULD maintain the session on behalf of the client for a limited amount of time. The client can send whatever presence it wishes before leaving the stream in an unfinished state.</p>
  395. </section1>
  396. <section1 topic='Scenarios' anchor='scenarios'>
  397. <p>The following scenarios illustrate several different uses of stream management. The examples are that of a client and a server, but stream management can also be used for server-to-server streams.</p>
  398. <section2 topic='Basic Acking Scenario' anchor='scenarios-basic'>
  399. <p>The Stream Management protocol can be used to improve reliability using acks without the ability to resume a session. A basic implementation would do the following:</p>
  400. <ul>
  401. <li>As a client, send &lt;enable/&gt; with no attributes, and ignore the attributes on the &lt;enabled/&gt; response.</li>
  402. <li>As a server, ignore the attributes on the &lt;enable/&gt; element received, and respond via &lt;enabled/&gt; with no attributes.</li>
  403. <li>When receiving an &lt;r/&gt; element, immediately respond via an &lt;a/&gt; element where the value of 'h' returned is the sequence number of the last handled stanza.</li>
  404. <li>Keep an integer X for this stream session, initially set to zero. When about to send a stanza, first put the stanza (paired with the current value of X) in an "unacknowleged" queue. Then send the stanza over the wire with &lt;r/&gt; to request acknowledgement of that outbound stanza, and increment X by 1. When receiving an &lt;a/&gt; element with an 'h' attribute, all stanzas whose paired value (X at the time of queueing) is less than or equal to the value of 'h' can be removed from the unacknowledged queue.</li>
  405. </ul>
  406. <p>This is enough of an implementation to minimally satisfy the peer, and allows basic tracking of each outbound stanza. If the stream connection is broken, the application has a queue of unacknowledged stanzas that it can choose to handle appropriately (e.g., warn a human user or silently send after reconnecting).</p>
  407. <p>The following examples illustrate basic acking (here the client automatically acks each stanza it has received from the server, without first being prompted via an &lt;r/&gt; element).</p>
  408. <p>First, after authentication and resource binding, the client enables stream management.</p>
  409. <example caption='Client enables stream management'><![CDATA[
  410. <enable xmlns='urn:xmpp:sm:3'/>
  411. ]]></example>
  412. <p>The server then enables stream management.</p>
  413. <example caption='Server enables stream management'><![CDATA[
  414. <enabled xmlns='urn:xmpp:sm:3'/>
  415. ]]></example>
  416. <p>The client then retrieves its roster and immediately sends an &lt;r/&gt; element to request acknowledgement.</p>
  417. <example caption='Client sends a stanza and requests acknowledgement'><![CDATA[
  418. <iq id='ls72g593' type='get'>
  419. <query xmlns='jabber:iq:roster'/>
  420. </iq>
  421. <r xmlns='urn:xmpp:sm:3'/>
  422. ]]></example>
  423. <p>The server handles the client stanza (here returning the roster) and sends an &lt;a/&gt; element to acknowledge handling of the stanza.</p>
  424. <example caption='Server handles client stanza and acknowledges handling of client stanza'><![CDATA[
  425. <iq id='ls72g593' type='result'>
  426. <query xmlns='jabber:iq:roster'>
  427. <item jid='juliet@capulet.lit'/>
  428. <item jid='benvolio@montague.lit'/>
  429. </query>
  430. </iq>
  431. <a xmlns='urn:xmpp:sm:3' h='1'/>
  432. ]]></example>
  433. <p>The client then chooses to acknowledge receipt of the server's stanza (although here it is under no obligation to do so, since the server has not requested an ack), sends initial presence, and immediately sends an &lt;r/&gt; element to request acknowledgement, incrementing by one its internal representation of how many stanzas have been handled by the server.</p>
  434. <example caption='Client acks handling of first server stanza, sends a stanza, and requests acknowledgement'><![CDATA[
  435. <a xmlns='urn:xmpp:sm:3' h='1'/>
  436. <presence/>
  437. <r xmlns='urn:xmpp:sm:3'/>
  438. ]]></example>
  439. <p>The server immediately sends an &lt;a/&gt; element to acknowledge handling of the stanza and then broadcasts the user's presence (including to the client itself as shown below).</p>
  440. <example caption='Server acks handling of second client stanza and sends a stanza'><![CDATA[
  441. <a xmlns='urn:xmpp:sm:3' h='2'/>
  442. <presence from='romeo@montague.lit/orchard'
  443. to='romeo@montague.lit/orchard'/>
  444. ]]></example>
  445. <p>The client then acks the server's second stanza and sends an outbound message followed by an &lt;r/&gt; element.</p>
  446. <example caption='Client acks receipt of second server stanza, sends a stanza, and requests acknowledgement'><![CDATA[
  447. <a xmlns='urn:xmpp:sm:3' h='2'/>
  448. <message to='juliet@capulet.lit'>
  449. <body>ciao!</body>
  450. </message>
  451. <r xmlns='urn:xmpp:sm:3'/>
  452. ]]></example>
  453. <p>The server immediately sends an &lt;a/&gt; element to acknowledge handling of the third client stanza and then routes the stanza to the remote contact (not shown here because the server does not send a stanza to the client).</p>
  454. <example caption='Server acknowledges handling of third client stanza'><![CDATA[
  455. <a xmlns='urn:xmpp:sm:3' h='3'/>
  456. ]]></example>
  457. <p>And so on.</p>
  458. </section2>
  459. <section2 topic='Efficient Acking Scenario' anchor='scenarios-efficient'>
  460. <p>The basic acking scenario is wasteful because the client requested an ack for each stanza. A more efficient approach is to periodically request acks (e.g., every 5 stanzas). This is shown schematically in the following pseudo-XML.</p>
  461. <example caption='An efficient session'><![CDATA[
  462. <!-- Client -->
  463. <enable/>
  464. <!-- Server -->
  465. <enabled/>
  466. <!-- Client -->
  467. <message/>
  468. <message/>
  469. <message/>
  470. <message/>
  471. <message/>
  472. <r/>
  473. <!-- Server -->
  474. <a h='5'/>
  475. <!-- Client-->
  476. <message/>
  477. <message/>
  478. <message/>
  479. <message/>
  480. <message/>
  481. <r/>
  482. <!-- Server -->
  483. <a h='10'/>
  484. ]]></example>
  485. <p>In particular, on mobile networks, it is advisable to only request and/or send acknowledgements when an entity has other data to send, or in lieu of a whitespace keepalive or XMPP ping (XEP-0199).</p>
  486. </section2>
  487. </section1>
  488. <section1 topic='Security Considerations' anchor='security'>
  489. <p>As noted, a server MUST NOT allow a client to resume a stream management session until after the client has authenticated (for some value of "authentication"); this helps to prevent session hijacking.</p>
  490. </section1>
  491. <section1 topic='IANA Considerations' anchor='iana'>
  492. <p>This XEP requires no interaction with &IANA;. </p>
  493. </section1>
  494. <section1 topic='XMPP Registrar Considerations' anchor='registrar'>
  495. <section2 topic='Protocol Namespaces' anchor='registrar-ns'>
  496. <p>This specification defines the following XML namespace:</p>
  497. <ul>
  498. <li>urn:xmpp:sm:3</li>
  499. </ul>
  500. <p>The &REGISTRAR; includes the foregoing namespace in its registry at &NAMESPACES;, as described in Section 4 of &xep0053;.</p>
  501. </section2>
  502. <section2 topic='Protocol Versioning' anchor='registrar-versioning'>
  503. &NSVER;
  504. </section2>
  505. <section2 topic='Stream Features' anchor='registrar-stream'>
  506. <p>The XMPP Registrar includes 'urn:xmpp:sm:3' in its registry of stream features at &STREAMFEATURES;.</p>
  507. </section2>
  508. </section1>
  509. <section1 topic='XML Schemas' anchor='schemas'>
  510. <code><![CDATA[
  511. <?xml version='1.0' encoding='UTF-8'?>
  512. <xs:schema
  513. xmlns:xs=''
  514. targetNamespace='urn:xmpp:sm:3'
  515. xmlns='urn:xmpp:sm:3'
  516. elementFormDefault='qualified'>
  517. <xs:annotation>
  518. <xs:documentation>
  519. The protocol documented by this schema is defined in
  520. XEP-0198:
  521. </xs:documentation>
  522. </xs:annotation>
  523. <xs:import namespace='urn:ietf:params:xml:ns:xmpp-stanzas'
  524. schemaLocation=''/>
  525. <xs:element name='a'>
  526. <xs:complexType>
  527. <xs:simpleContent>
  528. <xs:extension base='empty'>
  529. <xs:attribute name='h'
  530. type='xs:integer'
  531. use='required'/>
  532. </xs:extension>
  533. </xs:simpleContent>
  534. </xs:complexType>
  535. </xs:element>
  536. <xs:element name='enable'>
  537. <xs:complexType>
  538. <xs:simpleContent>
  539. <xs:extension base='empty'>
  540. <xs:attribute name='max'
  541. type='xs:positiveInteger'
  542. use='optional'/>
  543. <xs:attribute name='resume'
  544. type='xs:boolean'
  545. use='optional'
  546. default='false'/>
  547. </xs:extension>
  548. </xs:simpleContent>
  549. </xs:complexType>
  550. </xs:element>
  551. <xs:element name='enabled'>
  552. <xs:complexType>
  553. <xs:simpleContent>
  554. <xs:extension base='empty'>
  555. <xs:attribute name='id'
  556. type='xs:string'
  557. use='optional'/>
  558. <xs:attribute name='location'
  559. type='xs:string'
  560. use='optional'/>
  561. <xs:attribute name='max'
  562. type='xs:positiveInteger'
  563. use='optional'/>
  564. <xs:attribute name='resume'
  565. type='xs:boolean'
  566. use='optional'
  567. default='false'/>
  568. </xs:extension>
  569. </xs:simpleContent>
  570. </xs:complexType>
  571. </xs:element>
  572. <xs:element name='failed'>
  573. <xs:complexType>
  574. <xs:sequence xmlns:err='urn:ietf:params:xml:ns:xmpp-stanzas'
  575. minOccurs='0'
  576. maxOccurs='1'>
  577. <xs:group ref='err:stanzaErrorGroup'/>
  578. </xs:sequence>
  579. <xs:attribute name='h'
  580. type='xs:unsignedInt'
  581. use='optional'/>
  582. </xs:complexType>
  583. </xs:element>
  584. <xs:element name='r' type='empty'/>
  585. <xs:element name='resume' type='resumptionElementType'/>
  586. <xs:element name='resumed' type='resumptionElementType'/>
  587. <xs:element name='sm'>
  588. <xs:complexType>
  589. <xs:choice>
  590. <xs:element name='optional' type='empty'/>
  591. <xs:element name='required' type='empty'/>
  592. </xs:choice>
  593. </xs:complexType>
  594. </xs:element>
  595. <xs:complexType name='resumptionElementType'>
  596. <xs:simpleContent>
  597. <xs:extension base='empty'>
  598. <xs:attribute name='h'
  599. type='xs:unsignedInt'
  600. use='required'/>
  601. <xs:attribute name='previd'
  602. type='xs:string'
  603. use='required'/>
  604. </xs:extension>
  605. </xs:simpleContent>
  606. </xs:complexType>
  607. <xs:simpleType name='empty'>
  608. <xs:restriction base='xs:string'>
  609. <xs:enumeration value=''/>
  610. </xs:restriction>
  611. </xs:simpleType>
  612. </xs:schema>
  613. ]]></code>
  614. </section1>
  615. <section1 topic='Acknowledgements' anchor='ack'>
  616. <p>Thanks to Bruce Campbell, Jack Erwin, Philipp Hancke, Curtis King, Tobias Markmann, Alexey Melnikov, Pedro Melo, Robin Redeker, Mickaël Rémond, Florian Schmaus, and Tomasz Sterna for their feedback.</p>
  617. </section1>
  618. </xep>