Building a single-page application traditionally requires a JavaScript framework, a JSON API, and separate view and logic codebases.
Building a single-page application traditionally requires a JavaScript framework, a JSON API, and separate view and logic codebases. An alternative approach sends pre‑rendered HTML from the server over a persistent WebSocket connection, allowing the client to place the HTML without writing much JavaScript. The approach can operate over HTTP, Server‑Sent Events, or WebSockets. HTTP delivers HTML on each request, as seen with htmx or Unicorn. Server‑Sent Events maintain a one‑way channel, exemplified by Datastar. WebSockets provide a bidirectional, continuous channel, as used by Phoenix LiveView or Django LiveView. With WebSockets, the client opens a connection, authenticates once, then sends a request such as “I want /article/2/”. The server queries the database, renders HTML with a template engine, and returns the assembled markup. The client inserts the HTML into the page and listens for further updates. Because the connection remains open, the server can push changes without a new request. Advantages include a single rendering engine, elimination of a separate API, server‑side state that persists across requests, direct database access, real‑time updates without polling, and the ability to broadcast changes to all clients. The architecture reduces traffic and latency by avoiding repeated HTTP handshakes and header overhead. HTML rendered on the server is indexable on initial load, and because the server escapes output, it mitigates injection risks. Drawbacks involve higher server resource usage, as each client maintains a stateful connection, and scaling requires sharing state across instances. Latency can increase with physical network distance, and loss of connection disables functionality until reconnection is handled. The initial learning curve is steeper than adding a simple script, and operating a WebSocket server adds complexity. Frameworks supporting HTML over WebSockets exist in multiple languages. Phoenix LiveView in Elixir, Hotwire Turbo in Ruby, Django LiveView in Python, Blazor Server in C#, and Laravel Reverb in PHP provide implementations. Other libraries such as htmx with SSE extensions or Datastar rely on Server‑Sent Events for one‑way push. The choice of transport depends on whether bidirectional communication is required. HTML over WebSockets is not a universal solution; it is best suited for applications that need low‑latency, interactive communication such as chat, collaborative editing, or live dashboards. For push‑only scenarios, Server‑Sent Events may be simpler and more scalable. The pattern emphasizes sending HTML instead of JSON, staying within a single language stack, and reducing client‑side complexity.
- Publisher
- Hacker News
- Reliability
- high
- Published
- 8/13/2026, 10:00:33 AM
- Retrieved
- 8/13/2026, 10:00:33 AM
- Relevance
- 80%
- Confidence
- 85%

