A place to cache linked articles (think custom and personal wayback machine)
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.

2 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. title: Server-Sent Events: the alternative to WebSockets you should be using
  2. url: https://germano.dev/sse-websockets/#comments
  3. hash_url: b17f8ac80615c86cade89dd81c8aa50b
  4. <p>When developing real-time web applications, WebSockets might be the first thing
  5. that come to your mind. However, Server Sent Events (SSE) are a simpler
  6. alternative that is often superior.</p>
  7. <h2 id="prologue">Prologue<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#prologue"><span class="icon icon-link"></span></a></h2>
  8. <p>Recently I have been curious about the best way to implement a
  9. <em>real-time web application</em>. That is, an application containing one ore more components
  10. which automatically update, in real-time, reacting to some external event.
  11. The most common example of such an application, would be a messaging service, where we
  12. want every message to be immediately broadcasted to everyone that is connected,
  13. without requiring any user interaction.</p>
  14. <p>After some research I stumbled upon an <a href="https://www.youtube.com/watch?v=n9mRjkQg3VE" target="_blank" rel="nofollow noopener noreferrer">amazing talk by Martin Chaov</a>, which compares Server Sent
  15. Events, WebSockets and Long Polling. The talk, which is also <a href="https://www.smashingmagazine.com/2018/02/sse-websockets-data-flow-http2/#comments-sse-websockets-data-flow-http2" target="_blank" rel="nofollow noopener noreferrer">available as a blog post</a>,
  16. is entertaining and very informative. I really recommend it.
  17. However, it is from 2018 and some small things have changed, so I decided to write this article.</p>
  18. <h2 id="websockets">WebSockets?<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#websockets"><span class="icon icon-link"></span></a></h2>
  19. <p><a href="https://tools.ietf.org/html/rfc6455" target="_blank" rel="nofollow noopener noreferrer">WebSockets</a> enable the creation of <strong>two-way</strong> <strong>low-latency</strong> communication
  20. channels between the browser and a server.</p>
  21. <p>This makes them ideal in certain scenarios, like multiplayer games, where the
  22. communication is <strong>two-way</strong>, in the sense that both the browser and server
  23. send messages on the channel <strong>all the time</strong>, and it is required that these messages
  24. be delivered with <strong>low latency</strong>.</p>
  25. <p>In a First-Person Shooter,
  26. the browser could be continuously streaming
  27. the player’s position, while simoultaneously receiving updates on the location of all
  28. the other players from the server. Moreover, we definitely want
  29. these messages to be delivered with as little overhead as possible, to avoid
  30. the game feeling sluggish.</p>
  31. <p>This is the opposite of the traditional <a href="https://en.wikipedia.org/wiki/Request%E2%80%93response" target="_blank" rel="nofollow noopener noreferrer">request-response model</a> of <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP" target="_blank" rel="nofollow noopener noreferrer">HTTP</a>, where
  32. the browser is always the one initiating the communication, and each message
  33. has a significant overhead, due to establishing <a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol" target="_blank" rel="nofollow noopener noreferrer">TCP connections</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers" target="_blank" rel="nofollow noopener noreferrer">HTTP headers</a>.</p>
  34. <p>However, many applications do not have requirements this strict.
  35. Even among real-time applications, <strong>the data flow is usually asymmetric</strong>:
  36. the server sends the majority of the messages while the client mostly just listens and
  37. only once in a while sends some updates. For example, in a chat application
  38. an user may be connected to many rooms each with tens or hundreds of participants.
  39. Thus, the volume of messages received far exceeds the one of messages sent.</p>
  40. <h2 id="what-is-wrong-with-websockets">What is wrong with WebSockets<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#what-is-wrong-with-websockets"><span class="icon icon-link"></span></a></h2>
  41. <p>Two-way channels and low latency are extremely good features. Why bother looking
  42. further? </p>
  43. <p>WebSockets have one major drawback: <strong>they do not work on top of HTTP</strong>, at least
  44. not fully. They require their own TCP connection. They use HTTP only to establish
  45. the connection, but then upgrade it to a standalone TCP connection on top of
  46. which the WebSocket protocol can be used.</p>
  47. <p>This may not seem a big deal, however it means that <strong>WebSockets cannot benefit
  48. from any HTTP feature</strong>. That is:</p>
  49. <ul>
  50. <li>No support for compression</li>
  51. <li>No support for HTTP/2 multiplexing</li>
  52. <li>Potential issues with proxies</li>
  53. <li>No protection from Cross-Site Hijacking</li>
  54. </ul>
  55. <p>At least, this was the situation when the WebSocket protocol was first released.
  56. Nowadays, there are some complementary standards that try to improve upon this
  57. situation. Let’s take a closer look to the current situation.</p>
  58. <p><strong>Note</strong>: If you do not care about the details, feel free to skip the rest of
  59. this section and jump directly to <a href="#sse">Server-Sent Events</a> or the <a href="#code">demo</a>.</p>
  60. <h3 id="compression">Compression<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#compression"><span class="icon icon-link"></span></a></h3>
  61. <p>On standard connections,
  62. <a href="https://en.wikipedia.org/wiki/HTTP_compression" target="_blank" rel="nofollow noopener noreferrer">HTTP compression</a> is supported by every browser, and is super easy to enable
  63. server-side. Just flip a switch in your reverse-proxy of choice. With WebSockets
  64. the question is more complex, because there are no requests and responses, but
  65. one needs to compress the individual WebSocket frames.</p>
  66. <p><a href="https://tools.ietf.org/html/rfc7692" target="_blank" rel="nofollow noopener noreferrer">RFC 7692</a>, released on December 2015, tries to improve the situation by
  67. definining <em>“Compression Extensions for WebSocket”</em>. However, to the best of my knowledge,
  68. no popular reverse-proxy (e.g. nginx, caddy) implements this, making it impossible
  69. to have compression enabled transparently.</p>
  70. <p>This means that if you want compression, it has to be implemented directly in your backend.
  71. Luckily, I was able to find some libraries supporting RFC 7692. For example,
  72. the <a href="https://websockets.readthedocs.io/en/stable/extensions.html" target="_blank" rel="nofollow noopener noreferrer">websockets</a> and <a href="https://github.com/python-hyper/wsproto/" target="_blank" rel="nofollow noopener noreferrer">wsproto</a> Python libraries, and
  73. the <a href="https://github.com/websockets/ws" target="_blank" rel="nofollow noopener noreferrer">ws</a> library for nodejs.</p>
  74. <p>However, the latter suggests not to use the feature:</p>
  75. <blockquote>
  76. <p>The extension is disabled by default on the server and enabled by default on
  77. the client. It adds a significant overhead in terms of performance and memory
  78. consumption so we suggest to enable it only if it is really needed.</p>
  79. <p>Note that Node.js has a variety of issues with high-performance compression,
  80. where increased concurrency, especially on Linux, can lead to catastrophic
  81. memory fragmentation and slow performance.</p>
  82. </blockquote>
  83. <p>On the browsers side, <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/37#networking" target="_blank" rel="nofollow noopener noreferrer">Firefox supports WebSocket compression since version 37</a>.
  84. <a href="https://chromestatus.com/feature/6555138000945152" target="_blank" rel="nofollow noopener noreferrer">Chrome supports it as well</a>. However, apparently Safari and Edge do not.</p>
  85. <p>I did not take the time to verify what is the situation on the mobile landscape.</p>
  86. <h3 id="multiplexing">Multiplexing<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#multiplexing"><span class="icon icon-link"></span></a></h3>
  87. <p><a href="https://tools.ietf.org/html/rfc7540" target="_blank" rel="nofollow noopener noreferrer">HTTP/2</a> introduced support for multiplexing, meaning that multiple request/response
  88. pairs to the same host no longer require separate TCP connections. Instead, they all share
  89. the same TCP connection, each operating on its own independent <a href="https://tools.ietf.org/html/rfc7540#section-5" target="_blank" rel="nofollow noopener noreferrer">HTTP/2 stream</a>.</p>
  90. <p>This is, again, <a href="https://caniuse.com/http2" target="_blank" rel="nofollow noopener noreferrer">supported by every browser</a> and is very easy to
  91. transparently enable on most reverse-proxies.</p>
  92. <p>On the contrary, the WebSocket protocol has no support, by default, for multiplexing.
  93. Multiple WebSockets to the same host will each open their own separate TCP
  94. connection. If you want to have two separate WebSocket endpoints share their
  95. underlying connection you must add multiplexing in your application’s code.</p>
  96. <p><a href="https://tools.ietf.org/html/rfc8441" target="_blank" rel="nofollow noopener noreferrer">RFC 8441</a>, released on September 2018, tries to fix this limitation by
  97. adding support for <em>“Bootstrapping WebSockets with HTTP/2”</em>. It has been
  98. <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1434137" target="_blank" rel="nofollow noopener noreferrer">implemented in Firefox</a> <a href="https://chromestatus.com/feature/6251293127475200" target="_blank" rel="nofollow noopener noreferrer">and Chrome</a>. However, as far as I know, no major
  99. reverse-proxy implements it. Unfortunately, I could not find any implementation in
  100. Python or Javascript either.</p>
  101. <h3 id="proxies">Issues with proxies<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#proxies"><span class="icon icon-link"></span></a></h3>
  102. <p>HTTP proxies without explicit support for WebSockets can prevent unencrypted
  103. WebSocket connections to work. This is because the proxy will not be able to
  104. parse the WebSocket frames and close the connection.</p>
  105. <p>However, WebSocket connections happening over HTTPS should be unaffected by
  106. this problem, since the frames will be encrypted and the proxy should just
  107. forward everything without closing the connection.</p>
  108. <p>To learn more, see <a href="https://www.infoq.com/articles/Web-Sockets-Proxy-Servers/" target="_blank" rel="nofollow noopener noreferrer">“How HTML5 Web Sockets Interact With Proxy Servers”</a>
  109. by Peter Lubbers.</p>
  110. <h3 id="hijacking">Cross-Site WebSocket Hijacking<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#hijacking"><span class="icon icon-link"></span></a></h3>
  111. <p>WebSocket connections are not protected by the same-origin policy. This makes
  112. them vulnerable to Cross-Site WebSocket Hijacking.</p>
  113. <p>Therefore, <strong>WebSocket backends must check the correctness of the <code>Origin</code> header</strong>,
  114. if they use any kind of client-cached authentication, such as <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies" target="_blank" rel="nofollow noopener noreferrer">cookies</a> or
  115. <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication" target="_blank" rel="nofollow noopener noreferrer">HTTP authentication</a>.</p>
  116. <p>I will not go into the details here, but consider this short example. Assume
  117. a Bitcoin Exchange uses WebSockets to provide its trading service. When you
  118. log in, the Exchange might set a cookie to keep your session active
  119. for a given period of time. Now, all an attacker has to do to steal your precious
  120. Bitcoins is make you visit a site under her control, and simply open a WebSocket
  121. connection to the Exchange. The malicious connection is going to be automatically
  122. authenticated. That is, unless the Exchange checks the <code>Origin</code> header and blocks
  123. the connections coming from unauthorized domains.</p>
  124. <p>I encourage you to check out the great article about <a href="https://christian-schneider.net/CrossSiteWebSocketHijacking.html#main" target="_blank" rel="nofollow noopener noreferrer">Cross-Site WebSocket Hijacking</a> by
  125. Christian Schneider, to learn more.</p>
  126. <h2 id="sse">Server-Sent Events<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#sse"><span class="icon icon-link"></span></a></h2>
  127. <p>Now that we know a bit more about WebSockets, including their advantages and shortcomings,
  128. let us learn about Server-Sent Events and find out if they are a valid alternative.</p>
  129. <p><a href="https://html.spec.whatwg.org/#server-sent-events" target="_blank" rel="nofollow noopener noreferrer">Server-Sent Events</a> enable the server to send low-latency push
  130. events to the client, at any time.
  131. They use a very simple protocol that is <a href="https://html.spec.whatwg.org/#server-sent-events" target="_blank" rel="nofollow noopener noreferrer">part of the HTML Standard</a> and <a href="https://caniuse.com/eventsource" target="_blank" rel="nofollow noopener noreferrer">supported by every
  132. browser</a>.</p>
  133. <p>Unlike WebSockets, <strong>Server-sent Events flow only one way</strong>: from the
  134. server to the client. This makes them unsuitable for a very specific set of
  135. applications, that is, those that require a communication channel that is <strong>both
  136. two-way and low latency</strong>, like real-time games.
  137. However, this trade-off is also their major advantage
  138. over WebSockets, because being <em>one-way</em>, <strong>Server-Sent Events work seamlessly on top of HTTP,
  139. without requiring a custom protocol</strong>. This gives them automatic access to
  140. all of HTTP’s features, such as compression or HTTP/2 multiplexing, making them a very convenient choice
  141. for the majority of real-time applications, where the bulk of the data is sent from the server, and
  142. where a little overhead in requests, due to HTTP headers, is acceptable.</p>
  143. <p>The protocol is very simple. It uses the <code>text/event-stream</code> Content-Type and
  144. messages of the form:</p>
  145. <pre class="language-text"><code class="language-text">data: First message
  146. event: join
  147. data: Second message. It has two
  148. data: lines, a custom event type and an id.
  149. id: 5
  150. : comment. Can be used as keep-alive
  151. data: Third message. I do not have more data.
  152. data: Please retry later.
  153. retry: 10</code></pre>
  154. <p>Each event is separated by two empty lines (<code>\n</code>) and consists of various optional
  155. fields.</p>
  156. <p>The <code>data</code> field, which can be repeted to denote multiple lines in the message,
  157. is unsurprisingly used for the content of the event.</p>
  158. <p>The <code>event</code> field allows to specify custom event types, which as we will show
  159. in the next section, can be used to fire different event handlers on the client.</p>
  160. <p>The other two fields, <code>id</code> and <code>retry</code>, are used to configure the behaviour of the <em>automatic
  161. reconnection mechanism</em>. This is one of the most interesting features of Server-Sent
  162. Events. It ensures that
  163. <strong>when the connection is dropped or closed by the server, the client will
  164. automatically try to reconnect</strong>, without any user intervention.</p>
  165. <p>The <code>retry</code> field is used to specify the minimum amount of time, in seconds,
  166. to wait before trying to reconnect.
  167. It can also be sent by a server, immediately before closing the client’s connection,
  168. to reduce its load when too many clients are connected.</p>
  169. <p>The <code>id</code> field associates an identifier with the current event. When reconnecting
  170. the client will transmit to the server the last seen id, using the <code>Last-Event-ID</code> HTTP header.
  171. This allows the stream to be resumed from the correct point.</p>
  172. <p>Finally, the server can stop the automatic reconnection mechanism altogether
  173. by returning an <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204" target="_blank" rel="nofollow noopener noreferrer">HTTP 204 No Content</a> response.</p>
  174. <h2 id="code">Let’s write some code!<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#code"><span class="icon icon-link"></span></a></h2>
  175. <p>Let us now put into practice what we learned.
  176. In this section we will implement a simple service
  177. both with Server-Sent Events and WebSockets.
  178. This should enable us to compare the two technologies. We will find out how easy
  179. it is to get started with each one, and verify by hand the features discussed
  180. in the previous sections.</p>
  181. <p>We are going to use Python
  182. for the backend, Caddy as a reverse-proxy and of course a couple of lines of
  183. JavaScript for the frontend.</p>
  184. <p>To make our example as simple as possible, our backend is just going to consist of
  185. two endpoints, each streaming a unique sequence of random numbers. They are going to be reachable from
  186. <code>/sse1</code> and <code>/sse2</code> for Server-Sent Events, and from <code>/ws1</code> and <code>/ws2</code> for
  187. WebSockets. While our frontend is going to consist of a single <code>index.html</code>
  188. file, with some JavaScript which will let us start and stop WebSockets and
  189. Server-Sent Events connections.</p>
  190. <p><a href="https://github.com/tyrion/sse-websockets-demo" target="_blank" rel="nofollow noopener noreferrer">The code of this example is available on GitHub</a>.</p>
  191. <h3 id="reverse-proxy">The Reverse-Proxy<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#reverse-proxy"><span class="icon icon-link"></span></a></h3>
  192. <p>Using a reverse-proxy, such as Caddy or nginx, is very useful, even in a small
  193. example such as this one.
  194. It gives us very easy access to many features that our backend
  195. of choice may lack.</p>
  196. <p>More specifically, it allows us to easily serve static files and automatically
  197. compress HTTP responses; to provide support for HTTP/2, letting us benefit
  198. from multiplexing, even if our
  199. backend only supports HTTP/1; and finally to do load balancing.</p>
  200. <p>I chose Caddy because it automatically manages for us HTTPS certificates,
  201. letting us skip a very boring task, especially for a quick experiment.</p>
  202. <p>The basic configuration, which resides in a <code>Caddyfile</code> at the root of our
  203. project, looks something like this:</p>
  204. <pre class="language-text"><code class="language-text">localhost
  205. bind 127.0.0.1 ::1
  206. root ./static
  207. file_server browse
  208. encode zstd gzip</code></pre>
  209. <p>This instructs Caddy to listen on the local interface on ports 80 and 443,
  210. enabling support for HTTPS and generating a self-signed certificate. It also
  211. enables compression and serving static files from the <code>static</code> directory.</p>
  212. <p>As the last step we need to ask Caddy to proxy our backend services. Server-Sent
  213. Events is just regular HTTP, so nothing special here:</p>
  214. <pre class="language-text"><code class="language-text">reverse_proxy /sse1 127.0.1.1:6001
  215. reverse_proxy /sse2 127.0.1.1:6002</code></pre>
  216. <p>To proxy WebSockets our reverse-proxy needs to have explicit support for it.
  217. Luckily, Caddy can handle this without problems, even though the configuration
  218. is slighly more verbose:</p>
  219. <pre class="language-text"><code class="language-text">@websockets {
  220. header Connection *Upgrade*
  221. header Upgrade websocket
  222. }
  223. handle /ws1 {
  224. reverse_proxy @websockets 127.0.1.1:6001
  225. }
  226. handle /ws2 {
  227. reverse_proxy @websockets 127.0.1.1:6002
  228. }</code></pre>
  229. <p>Finally you should start Caddy with</p>
  230. <pre class="language-bash"><code class="language-bash">$ <span class="token function">sudo</span> caddy start</code></pre>
  231. <h3 id="frontend">The Frontend<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#frontend"><span class="icon icon-link"></span></a></h3>
  232. <p>Let us start with the frontend, by comparing the JavaScript APIs of WebSockets
  233. and Server-Sent Events.</p>
  234. <p>The <a href="https://developer.mozilla.org/en-US/docs/Web/API/Websockets_API" target="_blank" rel="nofollow noopener noreferrer">WebSocket JavaScript API</a> is very simple to use.
  235. First, we need to create a new
  236. <code>WebSocket</code> object passing the URL of the server. Here <code>wss</code> indicates that
  237. the connection is to happen over HTTPS. As mentioned above it is really recommended
  238. to use HTTPS to avoid issues with proxies.</p>
  239. <p>Then, we should listen to some of the possible events (i.e. <code>open</code>,
  240. <code>message</code>, <code>close</code>, <code>error</code>), by either setting the <code>on$event</code> property or
  241. by using <code>addEventListener()</code>.</p>
  242. <pre class="language-javascript"><code class="language-javascript"><span class="token keyword">const</span> ws <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">WebSocket</span><span class="token punctuation">(</span><span class="token string">"wss://localhost/ws"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  243. ws<span class="token punctuation">.</span><span class="token method-variable function-variable method function property-access">onopen</span> <span class="token operator">=</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">"WebSocket open"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  244. ws<span class="token punctuation">.</span><span class="token method function property-access">addEventListener</span><span class="token punctuation">(</span>
  245. <span class="token string">"message"</span><span class="token punctuation">,</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token property-access">data</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  246. <p>The JavaScript API for Server-Sent Events is very similar. It requires us to
  247. create a new <code>EventSource</code> object passing the URL of the server, and then
  248. allows us to subscribe to the events in the same way as before.</p>
  249. <p>The main difference is that we can also subscribe to custom events.</p>
  250. <pre class="language-javascript"><code class="language-javascript"><span class="token keyword">const</span> es <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">EventSource</span><span class="token punctuation">(</span><span class="token string">"https://localhost/sse"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  251. es<span class="token punctuation">.</span><span class="token method-variable function-variable method function property-access">onopen</span> <span class="token operator">=</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">"EventSource open"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  252. es<span class="token punctuation">.</span><span class="token method function property-access">addEventListener</span><span class="token punctuation">(</span>
  253. <span class="token string">"message"</span><span class="token punctuation">,</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token property-access">data</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  254. es<span class="token punctuation">.</span><span class="token method function property-access">addEventListener</span><span class="token punctuation">(</span>
  255. <span class="token string">"join"</span><span class="token punctuation">,</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>e<span class="token punctuation">.</span><span class="token property-access">data</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> joined</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span></code></pre>
  256. <p>We can now use all this freshly aquired knowledge about JS APIs to build our
  257. actual frontend.</p>
  258. <p>To keep things as simple as possible, it is going to consist of only one <code>index.html</code>
  259. file, with a bunch of buttons that will let us start and stop our WebSockets
  260. and EventSources. Like so</p>
  261. <pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">onclick</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>startWS(1)<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Start WS1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  262. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">onclick</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>closeWS(1)<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Close WS1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  263. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span>
  264. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">onclick</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>startWS(2)<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Start WS2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  265. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">onclick</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>closeWS(2)<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Close WS2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span></code></pre>
  266. <p>We want more than one WebSocket/EventSource so we can test if HTTP/2 multiplexing
  267. works and how many connections are open.</p>
  268. <p>Now let us implement the two functions needed by those buttons to work:</p>
  269. <pre class="language-javascript"><code class="language-javascript"><span class="token keyword">const</span> wss <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  270. <span class="token keyword">function</span> <span class="token function">startWS</span><span class="token punctuation">(</span><span class="token parameter">i</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  271. <span class="token keyword">if</span> <span class="token punctuation">(</span>wss<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!==</span> <span class="token keyword nil">undefined</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
  272. <span class="token keyword">const</span> ws <span class="token operator">=</span> wss<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">WebSocket</span><span class="token punctuation">(</span><span class="token string">"wss://localhost/ws"</span><span class="token operator">+</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
  273. ws<span class="token punctuation">.</span><span class="token method-variable function-variable method function property-access">onopen</span> <span class="token operator">=</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">"WS open"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  274. ws<span class="token punctuation">.</span><span class="token method-variable function-variable method function property-access">onmessage</span> <span class="token operator">=</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token property-access">data</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  275. ws<span class="token punctuation">.</span><span class="token method-variable function-variable method function property-access">onclose</span> <span class="token operator">=</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token function">closeWS</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
  276. <span class="token punctuation">}</span>
  277. <span class="token keyword">function</span> <span class="token function">closeWS</span><span class="token punctuation">(</span><span class="token parameter">i</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  278. <span class="token keyword">if</span> <span class="token punctuation">(</span>wss<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!==</span> <span class="token keyword nil">undefined</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  279. <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">"Closing websocket"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  280. websockets<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token method function property-access">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  281. <span class="token keyword">delete</span> websockets<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
  282. <span class="token punctuation">}</span>
  283. <span class="token punctuation">}</span></code></pre>
  284. <p>The frontend code for Server-Sent Events is almost identical. The only difference
  285. is the <code>onerror</code> event handler, which is there because in case of error a message
  286. is logged and the browser will attempt to reconnect.</p>
  287. <pre class="language-javascript"><code class="language-javascript"><span class="token keyword">const</span> ess <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  288. <span class="token keyword">function</span> <span class="token function">startES</span><span class="token punctuation">(</span><span class="token parameter">i</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  289. <span class="token keyword">if</span> <span class="token punctuation">(</span>ess<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!==</span> <span class="token keyword nil">undefined</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
  290. <span class="token keyword">const</span> es <span class="token operator">=</span> ess<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">EventSource</span><span class="token punctuation">(</span><span class="token string">"https://localhost/sse"</span><span class="token operator">+</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
  291. es<span class="token punctuation">.</span><span class="token method-variable function-variable method function property-access">onopen</span> <span class="token operator">=</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">"ES open"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  292. es<span class="token punctuation">.</span><span class="token method-variable function-variable method function property-access">onerror</span> <span class="token operator">=</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">"ES error"</span><span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">;</span>
  293. es<span class="token punctuation">.</span><span class="token method-variable function-variable method function property-access">onmessage</span> <span class="token operator">=</span> <span class="token parameter">e</span> <span class="token arrow operator">=&gt;</span> <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token property-access">data</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  294. <span class="token punctuation">}</span>
  295. <span class="token keyword">function</span> <span class="token function">closeES</span><span class="token punctuation">(</span><span class="token parameter">i</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  296. <span class="token keyword">if</span> <span class="token punctuation">(</span>ess<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!==</span> <span class="token keyword nil">undefined</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  297. <span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">"Closing EventSource"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  298. ess<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token method function property-access">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  299. <span class="token keyword">delete</span> ess<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
  300. <span class="token punctuation">}</span>
  301. <span class="token punctuation">}</span></code></pre>
  302. <h3 id="backend">The Backend<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#backend"><span class="icon icon-link"></span></a></h3>
  303. <p>To write our backend, we are going to use <a href="https://www.starlette.io/" target="_blank" rel="nofollow noopener noreferrer">Starlette</a>, a simple async web framework
  304. for Python, and <a href="https://www.uvicorn.org/" target="_blank" rel="nofollow noopener noreferrer">Uvicorn</a> as the server.
  305. Moreover, to make things modular, we are going to separate the <em>data-generating process</em>,
  306. from the implementation of the endpoints.</p>
  307. <p>We want each of the two endpoints to generate an <em>unique</em> random sequence
  308. of numbers. To accomplish this we will use the stream id (i.e. <code>1</code> or <code>2</code>) as
  309. part of the <a href="https://en.wikipedia.org/wiki/Random_seed" target="_blank" rel="nofollow noopener noreferrer">random seed</a>.</p>
  310. <p>Ideally, we would also like our streams to be <em>resumable</em>. That is, a client
  311. should be able to resume the stream from the last message it received, in case
  312. the connection is dropped, instead or re-reading the whole sequence.
  313. To make this possible we will assign an ID to each message/event, and use it
  314. to initialize the random seed, together with the stream id, before each message
  315. is generated.
  316. In our case, the ID is just going to be a counter starting from <code>0</code>.</p>
  317. <p>With all that said, we are ready to write the <code>get_data</code> function which is
  318. responsible to generate our random numbers:</p>
  319. <pre class="language-python"><code class="language-python"><span class="token keyword">import</span> random
  320. <span class="token keyword">def</span> <span class="token function">get_data</span><span class="token punctuation">(</span>stream_id<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span> event_id<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token builtin">int</span><span class="token punctuation">:</span>
  321. rnd <span class="token operator">=</span> random<span class="token punctuation">.</span>Random<span class="token punctuation">(</span><span class="token punctuation">)</span>
  322. rnd<span class="token punctuation">.</span>seed<span class="token punctuation">(</span>stream_id <span class="token operator">*</span> event_id<span class="token punctuation">)</span>
  323. <span class="token keyword">return</span> rnd<span class="token punctuation">.</span>randrange<span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span></code></pre>
  324. <p>Let’s now write the actual endpoints.</p>
  325. <p>Getting started with Starlette is very simple. We just need to initialize
  326. an <code>app</code> and then register some routes:</p>
  327. <pre class="language-python"><code class="language-python"><span class="token keyword">from</span> starlette<span class="token punctuation">.</span>applications <span class="token keyword">import</span> Starlette
  328. app <span class="token operator">=</span> Starlette<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
  329. <p>To write a WebSocket service both our web server and framework of choice must
  330. have explicit support. Luckily Uvicorn and Starlette are up to the task,
  331. and writing a WebSocket endpoint is as convenient as writing a normal route.</p>
  332. <p>This all the code that we need:</p>
  333. <pre class="language-python"><code class="language-python"><span class="token keyword">from</span> websockets<span class="token punctuation">.</span>exceptions <span class="token keyword">import</span> WebSocketException
  334. <span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>websocket_route</span><span class="token punctuation">(</span><span class="token string">"/ws{id:int}"</span><span class="token punctuation">)</span>
  335. <span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">websocket_endpoint</span><span class="token punctuation">(</span>ws<span class="token punctuation">)</span><span class="token punctuation">:</span>
  336. <span class="token builtin">id</span> <span class="token operator">=</span> ws<span class="token punctuation">.</span>path_params<span class="token punctuation">[</span><span class="token string">"id"</span><span class="token punctuation">]</span>
  337. <span class="token keyword">try</span><span class="token punctuation">:</span>
  338. <span class="token keyword">await</span> ws<span class="token punctuation">.</span>accept<span class="token punctuation">(</span><span class="token punctuation">)</span>
  339. <span class="token keyword">for</span> i <span class="token keyword">in</span> itertools<span class="token punctuation">.</span>count<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
  340. data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"id"</span><span class="token punctuation">:</span> i<span class="token punctuation">,</span> <span class="token string">"msg"</span><span class="token punctuation">:</span> get_data<span class="token punctuation">(</span><span class="token builtin">id</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">}</span>
  341. <span class="token keyword">await</span> ws<span class="token punctuation">.</span>send_json<span class="token punctuation">(</span>data<span class="token punctuation">)</span>
  342. <span class="token keyword">await</span> asyncio<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
  343. <span class="token keyword">except</span> WebSocketException<span class="token punctuation">:</span>
  344. <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"client disconnected"</span><span class="token punctuation">)</span></code></pre>
  345. <p>The code above will make sure our <code>websocket_endpoint</code> function is called every time
  346. a browser requests a path starting with <code>/ws</code> and followed by a number (e.g. <code>/ws1</code>, <code>/ws2</code>).</p>
  347. <p>Then, for every matching request, it will wait for a WebSocket connection to be
  348. established and subsequently start an infinite loop sending random numbers,
  349. encoded as a JSON payload, every second.</p>
  350. <p>For Server-Sent Events the code is very similar, except that no special
  351. framework support is needed.
  352. In this case, we register a route matching URLs starting with <code>/sse</code> and ending
  353. with a number (e.g. <code>/sse1</code>, <code>/sse2</code>).
  354. However, this time our endpoint just sets the appropriate headers and returns a <code>StreamingResponse</code>:</p>
  355. <pre class="language-python"><code class="language-python"><span class="token keyword">from</span> starlette<span class="token punctuation">.</span>responses <span class="token keyword">import</span> StreamingResponse
  356. <span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">"/sse{id:int}"</span><span class="token punctuation">)</span>
  357. <span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">sse_endpoint</span><span class="token punctuation">(</span>req<span class="token punctuation">)</span><span class="token punctuation">:</span>
  358. <span class="token keyword">return</span> StreamingResponse<span class="token punctuation">(</span>
  359. sse_generator<span class="token punctuation">(</span>req<span class="token punctuation">)</span><span class="token punctuation">,</span>
  360. headers<span class="token operator">=</span><span class="token punctuation">{</span>
  361. <span class="token string">"Content-type"</span><span class="token punctuation">:</span> <span class="token string">"text/event-stream"</span><span class="token punctuation">,</span>
  362. <span class="token string">"Cache-Control"</span><span class="token punctuation">:</span> <span class="token string">"no-cache"</span><span class="token punctuation">,</span>
  363. <span class="token string">"Connection"</span><span class="token punctuation">:</span> <span class="token string">"keep-alive"</span><span class="token punctuation">,</span>
  364. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  365. <span class="token punctuation">)</span></code></pre>
  366. <p><code>StreamingResponse</code> is an utility class, provided by Starlette, which takes a generator and streams
  367. its output to the client, keeping the connection open.</p>
  368. <p>The code of <code>sse_generator</code> is shown below, and is almost identical to the WebSocket
  369. endpoint, except that messages are encoded according to the Server-Sent Events
  370. protocol:</p>
  371. <pre class="language-python"><code class="language-python"><span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">sse_generator</span><span class="token punctuation">(</span>req<span class="token punctuation">)</span><span class="token punctuation">:</span>
  372. <span class="token builtin">id</span> <span class="token operator">=</span> req<span class="token punctuation">.</span>path_params<span class="token punctuation">[</span><span class="token string">"id"</span><span class="token punctuation">]</span>
  373. <span class="token keyword">for</span> i <span class="token keyword">in</span> itertools<span class="token punctuation">.</span>count<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
  374. data <span class="token operator">=</span> get_data<span class="token punctuation">(</span><span class="token builtin">id</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span>
  375. data <span class="token operator">=</span> <span class="token string">b"id: %d\ndata: %d\n\n"</span> <span class="token operator">%</span> <span class="token punctuation">(</span>i<span class="token punctuation">,</span> data<span class="token punctuation">)</span>
  376. <span class="token keyword">yield</span> data
  377. <span class="token keyword">await</span> asyncio<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span></code></pre>
  378. <p>We are done!</p>
  379. <p>Finally, assuming we put all our code in a file named <code>server.py</code>, we can start
  380. our backend endpoints using Uvicorn, like so:</p>
  381. <pre class="language-text"><code class="language-text">$ uvicorn --host 127.0.1.1 --port 6001 server:app &amp;
  382. $ uvicorn --host 127.0.1.1 --port 6002 server:app &amp;</code></pre>
  383. <h2 id="bonus">Bonus: Cool SSE features<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#bonus"><span class="icon icon-link"></span></a></h2>
  384. <p>Ok, let us now conclude by showing how easy it is to implement all those nice features we bragged about earlier.</p>
  385. <p><strong>Compression</strong> can be enabled by changing just a few lines in our endpoint:</p>
  386. <pre class="language-diff"><code class="language-diff">@@ -32,10 +33,12 @@ async def websocket_endpoint(ws):
  387. <span class="token unchanged">
  388. async def sse_generator(req):
  389. id = req.path_params["id"]
  390. </span><span class="token inserted-sign inserted">+ stream = zlib.compressobj()
  391. </span><span class="token unchanged"> for i in itertools.count():
  392. data = get_data(id, i)
  393. data = b"id: %d\ndata: %d\n\n" % (i, data)
  394. </span><span class="token deleted-sign deleted">- yield data
  395. </span><span class="token inserted-sign inserted">+ yield stream.compress(data)
  396. + yield stream.flush(zlib.Z_SYNC_FLUSH)
  397. </span><span class="token unchanged"> await asyncio.sleep(1)
  398. </span>@@ -47,5 +50,6 @@ async def sse_endpoint(req):
  399. <span class="token unchanged"> "Content-type": "text/event-stream",
  400. "Cache-Control": "no-cache",
  401. "Connection": "keep-alive",
  402. </span><span class="token inserted-sign inserted">+ "Content-Encoding": "deflate",
  403. </span><span class="token unchanged"> },
  404. )</span></code></pre>
  405. <p>We can then verify that everything is working as expected by checking the DevTools:</p>
  406. <p><img class="g-image g-image--lazy g-image--loading" src="data:image/svg+xml,%3csvg%20fill='none'%20viewBox='0%200%20646%20288'%20xmlns='http://www.w3.org/2000/svg'%20xmlns:xlink='http://www.w3.org/1999/xlink'%3e%3cdefs%3e%3cfilter%20id='__svg-blur-aa886b223fb216a8551fa2b5df639959'%3e%3cfeGaussianBlur%20in='SourceGraphic'%20stdDeviation='40'/%3e%3c/filter%3e%3c/defs%3e%3cimage%20x='0'%20y='0'%20filter='url(%23__svg-blur-aa886b223fb216a8551fa2b5df639959)'%20width='646'%20height='288'%20xlink:href='data:image/png%3bbase64%2ciVBORw0KGgoAAAANSUhEUgAAAEAAAAAdCAYAAAAaeWr3AAAACXBIWXMAAAsSAAALEgHS3X78AAALF0lEQVRYw7VYaVMiWxLl/3%2bazxMxETOv%2b6ntDooCKrYrrig2IIugIiCyyCKrgDnnZFUh0rTR2jNEXKrqLpl582aezLy2p0ZTavWGVGpPUn2qa9N3tEqt/qZ/uFn9uUIR65vSaLaUDvve0jOepXJFCqVHaXWe5anxOm48n/Q5jhf7SC9fLMljpSrNducN/dF3PrmnOuSpmHuw6LCPY9UhPrbM/YMUIRwHytWadtabzTfCv6eAQqksqXRGkqm00qlhTa5QMhgNzaMCio9luYzG5A48Gy1DwIYKWn%2bXT61eVwVUIF88ca30rQ2y39p4qVxWJWVzOblOZfRguNZQXFtu7zKSfcgP5nO97d8TMzLrcIrT5ZHZxWWxr7hlctYukcStCll%2bxwLYSPzs/Ey%2bTC%2bKd3tX5hbtsuT2yt7RidiXVyUBQbhJnj7neze/y8bOnrjXNmR1fVO%2bTM1J9B1elgIfq08SjUZl9%2bBY9o9OQXtFlt3rMudYlXn7srhAb2phWb7v%2bsTudMk6%2bDg967L2fVc2t3dUpr%2bnZmXObqxbda/JWSgmtnDsSrb3fLKxtSOB4KWc/wjifU/SufyrqY5plukWcKo3Nzfi3fGJD5te/74jJ%2bcXKsgmNnp1c6ebo6UUHyuSvc/J0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vzGE5brfanLjSMm6f/z4XIfwERJGHCHQ2p7wAAAABJRU5ErkJggg=='%20/%3e%3c/svg%3e" alt="SSE Compression" data-srcset="https://d33wubrfki0l68.cloudfront.net/d9aa3cb7e8ec5b4cc500b1e2b4c9275c8f73bb7b/3df27/assets/static/sse-compression.0a1763c.a6a3649bb9d1351f6e26788e472622a9.png 646w" data-sizes="(max-width: 646px) 100vw, 646px" data-src="https://d33wubrfki0l68.cloudfront.net/d9aa3cb7e8ec5b4cc500b1e2b4c9275c8f73bb7b/3df27/assets/static/sse-compression.0a1763c.a6a3649bb9d1351f6e26788e472622a9.png"><noscript><img class="g-image g-image--lazy g-image--loaded" src="https://d33wubrfki0l68.cloudfront.net/d9aa3cb7e8ec5b4cc500b1e2b4c9275c8f73bb7b/3df27/assets/static/sse-compression.0a1763c.a6a3649bb9d1351f6e26788e472622a9.png" alt="SSE Compression"></noscript></p>
  407. <p><strong>Multiplexing</strong> is enabled by default since Caddy supports HTTP/2. We can confirm
  408. that the same connection is being used for all our SSE requests using the
  409. DevTools again:</p>
  410. <p><img class="g-image g-image--lazy g-image--loading" src="data:image/svg+xml,%3csvg%20fill='none'%20viewBox='0%200%20620%20270'%20xmlns='http://www.w3.org/2000/svg'%20xmlns:xlink='http://www.w3.org/1999/xlink'%3e%3cdefs%3e%3cfilter%20id='__svg-blur-0a52f18c0d2930b06db6e22b1898fd5c'%3e%3cfeGaussianBlur%20in='SourceGraphic'%20stdDeviation='40'/%3e%3c/filter%3e%3c/defs%3e%3cimage%20x='0'%20y='0'%20filter='url(%23__svg-blur-0a52f18c0d2930b06db6e22b1898fd5c)'%20width='620'%20height='270'%20xlink:href='data:image/png%3bbase64%2ciVBORw0KGgoAAAANSUhEUgAAAEAAAAAcCAYAAADRJblSAAAACXBIWXMAAAsSAAALEgHS3X78AAAMIklEQVRYw61Y%2bW9kxRGevyBkgQ2wC4mU5MdIIVESCIQIBSQkEoGEkKJIiQIhSgSbXfaAECmHsoBAJBG5CBKEcOwuu2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2ZtqKuuQ09ff0i4ens6qcYMIweSoDMZjNaOnvR0tomflIbGrVicnISF%2boa4fGHBT/mwe4xNzuD1o4uSr6GMDkzL377O8hS97UANktmykkIWwA/T%2byj7NM5zT9109vvj5Me9idew9bA39zWulCOlGRZBdoAf/OzJ%2bSJJCorEiqm4XnVXdQ1rEe1/NpEiHX8xImQLMeEEuxHhYIilOKkRk2ISlsKlkpFNWBtBTlFfDM9J1OKoiuhJTVs8lX03Gc6pucAyLKqExLeILf6DW8nWuoY0%2bv82BqFjtpakchpQbVav2uBgd9UiRKPZfsqPWOrIglJUQLCkTZBSUmC4gH7qIdSY/Ec1SRDWQIrFA4FMUf%2buGhbxvzCIvmtawc9t5wcMW9OZFL0zX7OcsL0DLs9PuGGPMatTideJxrjIJ3V%2bMRiCSGPn273ugdX5%2bbhWFsXG6rV79oSIQo%2bMgnhzXNCtLQwj8sNRtRRInKhrgkd5h4sO9wig6tNhhj8vnvW3WiiJKi%2b0YgPzp2HeWBU/Pae1ehVxbIYG7Xgvx9cQluHGW%2b9dx51HxnR3NKCBlMnWlpaUU/xob6hGV19A8TPjMGhIbxNdEt2FyVWBfFScU4wPDhABVU96ptaRbxwUfAsFvbW78BESFgAnTRHWoY4iHAI84s2LC7ZsEop8gL1I5zGam9yLeJkKV6vl6zIIZIptqQ1UihXYwFxcoENCpYc%2bdfolbHTK8Ctz%2bcTspeXl7G0bCeLs2NuYUkUSU6nk6xqRfwUrmeUbEmSFKHXYkNksR5KuvbT7WMtgJmxf2e0E9HjAAdC9v2yiAFlMbaXD/F4XIsBejHF9GUtrlTHAKbTfb68VXiVdozxWparxx19nPXSeXEsEfGhau31%2br6O/wODFRuSjruYwgAAAABJRU5ErkJggg=='%20/%3e%3c/svg%3e" alt="SSE Multiplexing" data-srcset="https://d33wubrfki0l68.cloudfront.net/a214dba7e635b9c1705b811e1f9e4f1ec063ff1d/35a94/assets/static/sse.41b9fa4.1efd0be9ccdca2dac72468578f5a26a7.png 620w" data-sizes="(max-width: 620px) 100vw, 620px" data-src="https://d33wubrfki0l68.cloudfront.net/a214dba7e635b9c1705b811e1f9e4f1ec063ff1d/35a94/assets/static/sse.41b9fa4.1efd0be9ccdca2dac72468578f5a26a7.png"><noscript><img class="g-image g-image--lazy g-image--loaded" src="https://d33wubrfki0l68.cloudfront.net/a214dba7e635b9c1705b811e1f9e4f1ec063ff1d/35a94/assets/static/sse.41b9fa4.1efd0be9ccdca2dac72468578f5a26a7.png" alt="SSE Multiplexing"></noscript></p>
  411. <p><strong>Automatic reconnection</strong> on unexpected connection errors is as simple as
  412. reading the <a href="https://html.spec.whatwg.org/multipage/server-sent-events.html#last-event-id" target="_blank" rel="nofollow noopener noreferrer"><code>Last-Event-ID</code></a> header in our backend code:</p>
  413. <pre class="language-diff"><code class="language-diff"><span class="token deleted-arrow deleted">&lt; for i in itertools.count():
  414. </span><span class="token coord">---</span>
  415. <span class="token inserted-arrow inserted">&gt; start = int(req.headers.get("last-event-id", 0))
  416. &gt; for i in itertools.count(start):</span></code></pre>
  417. <p><em>Nothing has to be changed in the front-end code.</em></p>
  418. <p>We can test that it is working by starting the connection to one of the SSE
  419. endpoints and then killing uvicorn. The connection will drop, but the browser
  420. will automatically try to reconnect. Thus, if we re-start
  421. the server, we will see the stream resume from where it left off!</p>
  422. <p>Notice how the stream resumes from the message <code>243</code>. Feels like magic 🔥</p>
  423. <p><img class="g-image g-image--lazy g-image--loading" src="data:image/svg+xml,%3csvg%20fill='none'%20viewBox='0%200%20836%20728'%20xmlns='http://www.w3.org/2000/svg'%20xmlns:xlink='http://www.w3.org/1999/xlink'%3e%3cdefs%3e%3cfilter%20id='__svg-blur-56b8ef60bc5a52f2598b1e8ef1b1a2dc'%3e%3cfeGaussianBlur%20in='SourceGraphic'%20stdDeviation='40'/%3e%3c/filter%3e%3c/defs%3e%3cimage%20x='0'%20y='0'%20filter='url(%23__svg-blur-56b8ef60bc5a52f2598b1e8ef1b1a2dc)'%20width='836'%20height='728'%20xlink:href='data:image/gif%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SjIdi8muvoYhm78%2bQOXLc8xg36jmF9kNbRuHd997DS6%2b8gldefRXTvpyB50aOVFS2qmaQYnljmDDJ/xOPHjOWaw9AckY23p0yBfl5OaisG4xhQxupyERMeOF5rlWPF156BVOnvoepH/0NH055pxsvnAowm9DXRrOwOHmAp5EA5KOhCfkSfDxU8uAEJr26WQllAmKiRWFaEt/9aA3CC3wtzpsVAPXqiiAWWoEgs5FZnYQp5ZusC1%2bPT4hXFiIh0I83Jf3ltmWMiI3KEfeRWwwmpQ2PiICdViD9xQpsAU4wlZxArEuotHAG4RPSJlYiVuhHCeA4a1cf12VK3S0oPAo1vCUP%2bkZYZAwGEZjqeasxJCiSPGTSh4XFhYZxk/YglJaVUsqZLo9CFfMA2ZT4fjHbNLQYBzVfUlZGkpOo6G%2bcfDpjUhNHf5bwVlBUpMzWYHJSXpXHU5EuiuzKAkUxLlrrzOic1Nq76xuA%2bLUrW3T5veudV1e26MQAQ3e7q49rTVGkW3B4JEaNHqnSzBiCTEFhPpOHbKQyKxxEVK6qqlSoLiBktgUSrQvpo3FMdipV9peVk82kJkXFbT2xIrRfFOoHD1Ymq6NPikm%2b%2bPKLyMorYGKUqLI2ySqtdvsDxet7qfg/b39QEQWIVbiF5tciRQiE5O1d4UHlztReXw9PZf6Srnp1aVtcwOu%2bhMO963uBy7RcMVzdjtxsF3dwJVUm5hOaf8LY7ufw/4ja/ssKEAtw1L2IGMZeQXZp7Knd%2b2Pn/TfQs1/Pjd0/pmdMdkWTR3WQ/7MCEhtfRk11JWwEje64/5C38uD9jY/0Fh%2bFC/wvaziYeObNTrkAAAAASUVORK5CYII='%20/%3e%3c/svg%3e" alt="Prova" data-srcset="https://d33wubrfki0l68.cloudfront.net/5c6f9d7829ced2d6df1a2953744acf0d28b764f6/73e0c/assets/static/sse-auto-reconnect.82a2fbd.b190f19887f331ddb680b6ba6bc4921e.gif 480w, https://d33wubrfki0l68.cloudfront.net/5c6f9d7829ced2d6df1a2953744acf0d28b764f6/24679/assets/static/sse-auto-reconnect.7900e78.b190f19887f331ddb680b6ba6bc4921e.gif 836w" data-sizes="(max-width: 836px) 100vw, 836px" data-src="https://d33wubrfki0l68.cloudfront.net/5c6f9d7829ced2d6df1a2953744acf0d28b764f6/24679/assets/static/sse-auto-reconnect.7900e78.b190f19887f331ddb680b6ba6bc4921e.gif"><noscript><img class="g-image g-image--lazy g-image--loaded" src="https://d33wubrfki0l68.cloudfront.net/5c6f9d7829ced2d6df1a2953744acf0d28b764f6/24679/assets/static/sse-auto-reconnect.7900e78.b190f19887f331ddb680b6ba6bc4921e.gif" alt="Prova"></noscript></p>
  424. <h2 id="conclusion">Conclusion<a aria-hidden="true" tabindex="-1" class="h-anchor" href="#conclusion"><span class="icon icon-link"></span></a></h2>
  425. <p>WebSockets are a big machinery built on top of HTTP and TCP to provide a set
  426. of extremely specific features, that is <strong>two-way</strong> and <strong>low latency</strong> communication.</p>
  427. <p>In order to do that they introduce a number of complications, which end up
  428. making both client and server implementations more complicated than solutions
  429. based entirely on HTTP.</p>
  430. <p>These complications and limitations have been addressed by new specs (<a href="https://tools.ietf.org/html/rfc7692" target="_blank" rel="nofollow noopener noreferrer">RFC 7692</a>, <a href="https://tools.ietf.org/html/rfc8441" target="_blank" rel="nofollow noopener noreferrer">RFC 8441</a>), and
  431. will slowly end up implemented in client and server libraries.</p>
  432. <p>However, even in a world where WebSockets have no technical downsides, they will
  433. still be a fairly complex technology, involving a large amount of additional code both on clients and servers.
  434. Therefore, you should carefully consider if the addeded complexity is worth it,
  435. or if you can solve your problem with a much simpler solution, such as Server-Sent Events.</p>
  436. <hr>
  437. <p>That’s all, folks! I hope you found this post interesting and maybe learned
  438. something new.</p>
  439. <p><a href="https://github.com/tyrion/sse-websockets-demo" target="_blank" rel="nofollow noopener noreferrer">Feel free to check out the code of the demo on GitHub</a>, if you want to experiment
  440. a bit with Server Sent Events and Websockets.</p>
  441. <p><a href="https://html.spec.whatwg.org/#server-sent-events" target="_blank" rel="nofollow noopener noreferrer">I also encourage you to read the spec</a>, because it surprisingly clear and contains
  442. many examples.</p>