<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Realtime on Mitja Martini</title><link>https://mitjamartini.com/en/tags/realtime/</link><description>Recent content in Realtime on Mitja Martini</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>© 2026 Mitja Martini</copyright><lastBuildDate>Sat, 25 Jan 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://mitjamartini.com/en/tags/realtime/index.xml" rel="self" type="application/rss+xml"/><item><title>A note on the hidden complexities of WebSockets</title><link>https://mitjamartini.com/en/posts/a-note-about-the-hidden-complexities-of-websockets/</link><pubDate>Sat, 25 Jan 2025 00:00:00 +0000</pubDate><guid>https://mitjamartini.com/en/posts/a-note-about-the-hidden-complexities-of-websockets/</guid><description>&lt;p&gt;AI Apps are often expected to be realtime. On the web, realtime communication can be implemented with WebSockets. I&amp;rsquo;ve started with WebSockets to create chatbots and other live-updated interfaces, but then switched to SSE and now mostly follow these rules of thumbs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use WebSockets for server-to-server communication.&lt;/li&gt;
&lt;li&gt;Use SSE for server-to-client communication.&lt;/li&gt;
&lt;li&gt;If you still want or need to use WebSockets for server-to-client communication, add a fallback to SSE.&lt;/li&gt;
&lt;li&gt;If you need realtime voice/video server-to-client communication, use WebRTC.&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- more --&gt;
&lt;p&gt;If you still want or need to build a websocket service, please read Atul Jalan&amp;rsquo;s blog post about &lt;a
href="https://composehq.com/blog/scaling-websockets-1-23-25"
target="_blank"
&gt;The Hidden Complexity of Scaling WebSockets&lt;/a&gt;. It&amp;rsquo;s a quick read and captures important lessons to keep in mind when working with WebSockets.&lt;/p&gt;
&lt;p&gt;All of his lessons are important, even when working not at scale. Here is a quick summary:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Downtimeless deployments are much more involved than in HTTP services.&lt;/li&gt;
&lt;li&gt;Establish a good message schema, eg. 2 byte prefixes and single character field delimiters.&lt;/li&gt;
&lt;li&gt;Use heartbeats to detect dead connections - both ways.&lt;/li&gt;
&lt;li&gt;Have an http fallback as WebSockets are often blocked. Usually: Server sent events (SSE) for server to client communication and simple requests for client to server.&lt;/li&gt;
&lt;li&gt;More, like standard tooling (rate limiting, validation, error handling), no caching at the edge, per-message authentication.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Standard framworks like Django, FastHTML, and Quart (an async Flask clone) have good basic support for WebSockets, but don&amp;rsquo;t really help dealing with their hidden complexities. I hope, frameworks will level up a bit, as this is mostly &amp;ldquo;undifferentiated heavy lifting&amp;rdquo;.&lt;/p&gt;</description></item></channel></rss>