<?xml version="1.0" encoding="utf-8"?>
<!-- name="GENERATOR" content="github.com/mmarkdown/mmark Mmark Markdown Processor - mmark.nl" -->
<rfc version="3" ipr="trust200902" submissionType="IETF" category="info" xml:lang="en" consensus="true" xmlns:xi="http://www.w3.org/2001/XInclude" docName="draft-dunglas-mercure-00">

<front>
<title>The Mercure Protocol</title><seriesInfo value="draft-dunglas-mercure-00" stream="IETF" status="informational" name="Internet-Draft"></seriesInfo>
<author initials="K." surname="Dunglas" fullname="Kévin Dunglas"><organization abbrev="Les-Tilleuls.coop">Les-Tilleuls.coop</organization><address><postal><street>5 rue Hegel</street>
<city>Lille</city>
<code>59000</code>
<country>France</country>
<region></region>
</postal><phone></phone>
<email>kevin@les-tilleuls.coop</email>
<uri></uri>
</address></author>
<date/>
<area>Internet</area>
<workgroup>Network Working Group</workgroup>

<abstract>
<t>Mercure is a protocol allowing to push data updates to web browsers and other HTTP clients in a fast, reliable and battery-efficient way. It is especially useful to publish real-time updates of resources served through web APIs, to reactive web and mobile apps.</t>
</abstract>

</front>

<middle>

<section anchor="terminology"><name>Terminology</name>
<t>The keywords <bcp14>MUST</bcp14>, <bcp14>MUST NOT</bcp14>, <bcp14>REQUIRED</bcp14>, <bcp14>SHALL</bcp14>, <bcp14>SHALL NOT</bcp14>, <bcp14>SHOULD</bcp14>, <bcp14>SHOULD NOT</bcp14>, <bcp14>RECOMMENDED</bcp14>, <bcp14>MAY</bcp14>, and <bcp14>OPTIONAL</bcp14>, when they appear in this document, are to be interpreted as described in <xref target="RFC2119"></xref>.</t>

<ul>
<li><t>Topic: An HTTP <xref target="RFC7230"></xref> or HTTPS <xref target="RFC2818"></xref> topic URL. The unit to which one can subscribe to changes.</t>
</li>
<li><t>Publisher: An owner of a topic. Notifies the hub when the topic feed has been updated. As in almost all pubsub systems, the publisher is unaware of the subscribers, if any. Other pubsub systems might call the publisher the &quot;source&quot;. Typically a website or a web API.</t>
</li>
<li><t>Subscriber: A client application that subscribes to real-time updates of topics (typically a Progressive Web App or a Mobile App).</t>
</li>
<li><t>Hub: A server that handles subscription requests and distributes the content to subscribers when the corresponding topics have been updated (a Hub implementation is provided in this repository). Any hub <bcp14>MAY</bcp14> implement its own policies on who can use it.</t>
</li>
</ul>
</section>

<section anchor="discovery"><name>Discovery</name>
<t>The publisher <bcp14>SHOULD</bcp14> advertises the URL of one or more hubs to the subscriber, allowing it to receive live updates when topics are updated.
If more than one hub URL is specified, it is expected that the publisher notifies each hub, so the subscriber <bcp14>MAY</bcp14> subscribe to one or more of them.</t>
<t>The publisher <bcp14>SHOULD</bcp14> include at least one Link Header <xref target="RFC5988"></xref> with <tt>rel=mercure</tt> (a hub link header).
The target URL of these links <bcp14>MUST</bcp14> be a hub implementing the Mercure protocol.</t>
<t>Note: this relation type has not been registered yet <xref target="RFC5988"></xref>.
During the meantime, the relation type <tt>https://git.io/mercure</tt> can be used instead.</t>
<t>The publisher <bcp14>MAY</bcp14> provide the following target attributes in the Link headers:</t>

<ul>
<li><t><tt>last-event-id</tt>: the globally unique identifier of the last event dispatched by the publisher at the time of the generation of this resource. If provided, it <bcp14>MUST</bcp14> be passed to the hub through a query parameter called <tt>Last-Event-ID</tt> and will be used to ensure that possible updates having been made during between the resource generation time and the connection to the hub are not lost. See section #Re-Connection-and-State-Reconciliation). If this attribute is provided, the publisher <bcp14>MUST</bcp14> always set the <tt>id</tt> parameter when sending updates to the hub.</t>
</li>
<li><t><tt>content-type</tt>: the content type of the updates that will pushed by the hub. If omited, the subscriber <bcp14>MUST</bcp14> assume that the content type will be the same than the one of the original resource. Setting the <tt>content-type</tt> attribute is especially useful to hint that partial updates will be pushed, using formats such as JSON Patch <xref target="RFC6902"></xref> or JSON Merge Patch <xref target="RFC7386"></xref>.</t>
</li>
<li><t><tt>key-set=&lt;JWKS&gt;</tt>: the key(s) to decrypt updates encoded in the JWKS (JSON Web Key Set) format (see the Encryption section).</t>
</li>
</ul>
<t>All these attributes are optional.</t>
<t>The publisher <bcp14>MAY</bcp14> also include one Link Header <xref target="RFC5988"></xref> with <tt>rel=self</tt> (the self link header). It <bcp14>SHOULD</bcp14> contain the canonical URL for the topic to which subscribers are expected to use for subscriptions. If the Link with <tt>rel=self</tt> is ommitted, the current URL of the resource <bcp14>MUST</bcp14> be used as fallback.</t>
<t>Minimal example:</t>

<sourcecode type="http">GET /books/foo.jsonld HTTP/1.1
Host: example.com

HTTP/1.1 200 Ok
Content-type: application/ld+json
Link: &lt;https://hub.example.com/subscribe&gt;; rel=&quot;mercure&quot;

{&quot;@id&quot;: &quot;/books/foo.jsonld&quot;, &quot;foo&quot;: &quot;bar&quot;}
</sourcecode>
<t>Links embedded in HTML or XML documents (as defined in the WebSub recommendation) <bcp14>MAY</bcp14> also be supported by subscribers.</t>
<t>Note: the discovery mechanism described in this section <eref target="https://www.w3.org/TR/websub/#discovery">is strongly inspired from the one specified in the WebSub recommendation</eref>.</t>
</section>

<section anchor="subscriptions"><name>Subscriptions</name>
<t>The subscriber subscribes to an URL exposed by a hub to receive updates of one or many topics.
To subscribe to updates, the client opens an HTTPS connection following the <eref target="https://html.spec.whatwg.org/multipage/server-sent-events.html">Server-Sent Events specification</eref> to the hub's subscription URL advertised
by the Publisher.
The connection <bcp14>SHOULD</bcp14> use HTTP/2 to leverage mutliplexing and other advanced features of this protocol.</t>
<t>The subscriber specifies the list of topics to get updates for by using one or several query parameters named <tt>topic</tt>.
The value of these query parameters <bcp14>MUST</bcp14> be URI templates <xref target="RFC6570"></xref>.</t>
<t>Note: an URL is also a valid URI template.</t>
<t>The protocol doesn't specify the maximum number of <tt>topic</tt> parameters that can be sent, but the hub <bcp14>MAY</bcp14> apply an arbitrary limit.</t>
<t>The <eref target="https://html.spec.whatwg.org/multipage/server-sent-events.html#the-eventsource-interface">EventSource JavaScript interface</eref> <bcp14>MAY</bcp14> be used to establish the connection.
Any other appropriate mechanism including but not limited to <eref target="https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams">readable streams</eref> and <eref target="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest">XMLHttpRequest</eref> (used by popular polyfills) <bcp14>MAY</bcp14> also be used.</t>
<t>The hub sends updates concerning all subscribed resources matching the provided URI templates.
The hub <bcp14>MUST</bcp14> send these updates as <eref target="https://html.spec.whatwg.org/multipage/server-sent-events.html#sse-processing-model">text/event-stream compliant events</eref>.</t>
<t>The <tt>data</tt> property <bcp14>MUST</bcp14> contain the new version of the topic. It can be the full resource, or a partial update by using formats such as JSON Patch <tt>@RFC6902</tt> or JSON Merge Patch <tt>@RFC7386</tt>.</t>
<t>All other properties defined in the Server-Sent Events specification <bcp14>MAY</bcp14> be used and <bcp14>SHOULD</bcp14> be supported by hubs.</t>
<t>The resource <bcp14>SHOULD</bcp14> be represented in a format with hypermedia capabilities such as JSON-LD <xref target="W3C.REC-json-ld-20140116"></xref>, Atom <xref target="RFC4287"></xref>, XML <xref target="W3C.REC-xml-20081126"></xref> or HTML <xref target="W3C.REC-html52-20171214"></xref>.</t>
<t>Web Linking <xref target="RFC5988"></xref> <bcp14>SHOULD</bcp14> be used to indicate the IRI of the resource sent in the event.
When using Atom, XML or HTML as serialization format for the resource, the document <bcp14>SHOULD</bcp14> contain a <tt>link</tt> element with a <tt>self</tt> relation containing the IRI of the resource.
When using JSON-LD, the document <bcp14>SHOULD</bcp14> contain an <tt>@id</tt> property containing the IRI of the resource.</t>
<t>Example:</t>

<sourcecode type="javascript">// The subscriber subscribes to updates for the https://example.com/foo topic
// and to any topic matching https://example.com/books/{name}
const url = new URL('https://hub.example.com/subscribe');
url.searchParams.append('topic', 'https://example.com/foo');
url.searchParams.append('topic', 'https://example.com/bar/{id}');

const eventSource = new EventSource(url);

// The callback will be called every time an update is published
eventSource.onmessage = function ({data}) {
    console.log(data);
};
</sourcecode>
</section>

<section anchor="hub"><name>Hub</name>
<t>The hub receives updates from the publisher on a dedicated HTTPS endpoint.
The connection <bcp14>MUST</bcp14> use an encryption layer, such as TLS. HTTPS certificate can be obtained for free using <eref target="https://letsencrypt.org/">Let's Encrypt</eref>.</t>
<t>When it receives an update, the hub dispatches it to subsribers using the established server-sent events connections.</t>
<t>An application CAN send events directly to the subscribers, without using an external hub server, if it is able to do so.
In this case, it <strong>MAY NOT</strong> implement the endpoint to publish updates.</t>
<t>The endpoint to publish updates is an HTTPS URL accessed using the <tt>POST</tt> method.
The request <bcp14>MUST</bcp14> be encoded using the <tt>application/x-www-form-urlencoded</tt> format and contains the following data:</t>

<ul>
<li><t><tt>topic</tt>: IRIs of the updated topic. If this key is present several times, the first occurence is considered to be the canonical URL of the topic, and other ones are considered to be alternate URLs. The hub <bcp14>MUST</bcp14> dispatch this update to subscribers subscribed to both canonical or alternate URLs.</t>
</li>
<li><t><tt>data</tt>: the content of the new version of this topic</t>
</li>
<li><t><tt>target</tt> (optional): target audience of this event, see the Authorization section for further information.</t>
</li>
<li><t><tt>id</tt> (optional): the topic's revision identifier, it will be used as the SSE's <tt>id</tt> property, if omited the hub <bcp14>MUST</bcp14> generate a valid UUID.</t>
</li>
<li><t><tt>type</tt> (optional): the SSE's <tt>event</tt> property (a specific event type)</t>
</li>
<li><t><tt>retry</tt> (optional): the SSE's <tt>retry</tt> property (the reconnection time)</t>
</li>
</ul>
<t>The request <bcp14>MUST</bcp14> also contain an <tt>Authorization</tt> HTTP header containing the string <tt>Bearer</tt> followed by a valid JWS <xref target="RFC7515"></xref> in compact serialization that the hub will check to ensure that the publisher is authorized to publish the update.</t>
</section>

<section anchor="authorization"><name>Authorization</name>
<t>If a topic is not public, the update request sent by the publisher to the hub <bcp14>MUST</bcp14> also contain a list of keys named <tt>target</tt>.
Theirs values are <tt>string</tt>. They can be, for instance a user ID, or a list of group IDs.</t>
<t>To receive updates for private topics, the subscriber <bcp14>MUST</bcp14> send a cookie called <tt>mercureAuthorization</tt> when connecting
to the hub.</t>
<t>The cookie <bcp14>SHOULD</bcp14> be set by the publisher during the discovery. The cookie <bcp14>SHOULD</bcp14> have the <tt>Secure</tt>, <tt>HttpOnly</tt>.
It <bcp14>MAY</bcp14> have the <tt>SameSite</tt> flag if appropriate. Setting the cookie's <tt>Path</tt> to the path of the subscribe endpoint is also <bcp14>RECOMMENDED</bcp14>.
When skipping the discovery mechanism, the client <bcp14>MAY</bcp14> set the cookie itself (for security reasons, this is not recommended in the context of a web browser).</t>
<t>Consequently if the subscriber is a web browser, both the publisher and the hub have to share the same second level domain to use the autorization feature. The <tt>Domain</tt> flag <bcp14>MAY</bcp14> be used to allow the publisher and the host to use different subdomains.</t>
<t>By the <tt>EventSource</tt> specification, connections can only be estabilished using the <tt>GET</tt> HTTP method, and it is not possible to set custom HTTP headers (such as the <tt>Authorization</tt> one).</t>
<t>However, cookies are supported, and can be included even in crossdomain requests if <eref target="https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsourceinit-withcredentials">the CORS credentials are set</eref>:</t>
<t>The value of this cookie <bcp14>MUST</bcp14> be a JWS in compact serialization. It <bcp14>MUST</bcp14> have a claim named <tt>mercureTargets</tt> that contains an array of strings: the list of targets the user is authorized to receive updates for.
For instance, valid targets can be a username or a list of group identifiers.
The JWS <bcp14>SHOULD</bcp14> be short lived, especially if the subscriber is a web browser.</t>
<t>If one or more targets are specified, the update <bcp14>MUST NOT</bcp14> be sent to the subscriber by the hub, unless the <tt>mercureTargets</tt> claim of the subscriber contains at least one target specified for the topic by the publisher.</t>
<t>When using the authorization mechanism, the connection between the subscriber and the hub <bcp14>MUST</bcp14> use an encryption layer (HTTPS is required).</t>
</section>

<section anchor="re-connection-and-state-reconciliation"><name>Re-Connection and State Reconciliation</name>
<t>To allow re-establisment in case of connection lost, events dispatched by the hub <bcp14>SHOULD</bcp14> include an <tt>id</tt> property.
The value contained in this <tt>id</tt> property <bcp14>SHOULD</bcp14> be a globally unique identifier.
To do so, UUID <xref target="RFC4122"></xref> <bcp14>MAY</bcp14> be used.</t>
<t>According to the server-sent events specification, in case of connection lost the subscriber will try to automatically reconnect. During the reconnection the subscriber <bcp14>MUST</bcp14> send the last received event id in a <eref target="https://html.spec.whatwg.org/multipage/iana.html#last-event-id">Last-Event-ID</eref> HTTP header.</t>
<t>The server-sent events specification doesn't allow to set this HTTP header during the first connection (before a re-connection occurs).
In order to fetch any update dispatched between the initial resource generation by the publisher and the connection to he hub, the subscriber <bcp14>MUST</bcp14> send the event id provided during the discovery in the <tt>last-event-id</tt> link's attribute in a query parameter named <tt>Last-Event-ID</tt> when connecting to the hub.</t>
<t>If both the <tt>Last-Event-ID</tt> HTTP header and the query parameter are present, the HTTP header <bcp14>MUST</bcp14> take precedence.</t>
<t>If the <tt>Last-Event-ID</tt> header or query parameter exists, the hub <bcp14>SHOULD</bcp14> send to the subscriber all events published since the one having this identifier.</t>
<t>The hub <bcp14>MAY</bcp14> discard some messages for operational reasons. The subscriber <bcp14>MUST NOT</bcp14> assume that no update will be lost, and <bcp14>MUST</bcp14> re-fetch the original topic to ensure this (for instance, after a long deconnection time).</t>
<t>The hub <bcp14>MAY</bcp14> also specify the reconnection time using the <tt>retry</tt> key, as specified in the server-sent events format.</t>
</section>

<section anchor="encryption"><name>Encryption</name>
<t>Using HTTPS doesn't prevent the hub to access to the update's content.
Depending of the intended privacy of informations contained in the updates, it <bcp14>MAY</bcp14> be necessary to prevent eavesdropping by the hub.</t>
<t>To make sure that the message content can not be read by the hub, the publisher <bcp14>MAY</bcp14> encode the message before sending it to the hub.
The publisher <bcp14>SHOULD</bcp14> use JSON Web Encryption <xref target="RFC7516"></xref> to encrypt the update content.
The publisher <bcp14>MAY</bcp14> provide the relevant encryption key(s) in the <tt>key-set</tt> attribute of the Link HTTP header during the discovery.
The <tt>key-set</tt> attribute <bcp14>SHOULD</bcp14> contain a key encoded using the JSON Web Key Set <xref target="RFC7517"></xref> format.
Any other out-of-band mechanism <bcp14>MAY</bcp14> be used instead to share the key between the publisher and the subscriber.</t>
<t>Updates encyption is considered a best practice to prevent mass surveillance.
This is especially relevant if the hub is managed by an external provider.</t>
</section>

</middle>

<back>
<references><name>Normative References</name>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.2119.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5988.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.7230.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.4122.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.2818.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.7515.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.7517.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.6570.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.7516.xml"/>
</references>
<references><name>Informative References</name>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml-w3c/reference.W3C.REC-xml-20081126.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.4287.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.7386.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml-w3c/reference.W3C.REC-json-ld-20140116.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.6902.xml"/>
<xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml-w3c/reference.W3C.REC-html52-20171214.xml"/>
</references>

</back>

</rfc>
