archive / 2026

One WebSocket to make a static blog feel alive

Other readers as dots on the progress bar, a cat everyone can feed, a shared pixel wall and a live htop page, all on a static Hugo site. One small Go service, one WebSocket, and no cookies.

A static site is wonderful. It’s fast, it’s cheap, and nothing can break at three in the morning. It’s also a bit like a museum: you walk through it alone, and nobody notices you were there.

So, a few hours after moving this blog to its own VPS, I gave it a pulse. If you’re reading this in a browser with JavaScript on, look around:

  • Other readers show up as coloured dots on the progress bar at the top of every post, at the spot where they are in the text. When someone else is reading, the meta line under the title also says “2 reading now”.
  • The footer counts everyone online ($ who | wc -l) and has a button to feed Segfault. When anyone feeds her, she walks across the bottom of the screen for everybody who has the site open.
  • The about page has a 32×32 pixel wall. Everyone shares it, and you get one pixel every 20 seconds.
  • /status/ is htop in a browser: live CPU, memory, disk, requests per second and network, plus the pages being read right now, listed as processes.

The site itself is still plain static HTML from Hugo. All of the above is progressive enhancement, served by one small Go program over one WebSocket.

How a WebSocket starts: a plain HTTP request#

What I like about WebSockets is that they start life as a perfectly normal HTTP/1.1 request that asks to change protocols:

GET /ws?room=/about/ HTTP/1.1
Host: domienator.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Origin: https://domienator.com

If the server agrees, it answers with a status code you rarely see in the wild:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

After that, the same TCP connection (inside TLS) is no longer HTTP. It carries small frames in both directions, for as long as both sides want.

The Sec-WebSocket-Accept header isn’t magic. It proves the server understood the handshake, and you can compute it yourself: append a fixed GUID from RFC 6455 to the key, take the SHA-1, and base64 it.

$ printf '%s' 'dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11' \
    | openssl sha1 -binary | base64
s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

One thing surprised me. The pages of this site load over HTTP/2, but when I watched Chromium’s DevTools protocol, the WebSocket still opened with that classic HTTP/1.1 101. There are specs for WebSockets over HTTP/2 (RFC 8441) and HTTP/3 (RFC 9220), but in practice you still get the old handshake. The bottom of the status page shows it too.

The architecture#

It’s the smallest setup I could get away with:

browser ──HTTPS──▶ Caddy ──┬── /*   → static files from Hugo
                           └── /ws  → blog-live on 127.0.0.1:8090 (Go)

On the Caddy side it’s one line. Caddy notices the Upgrade header and proxies the WebSocket on its own:

reverse_proxy /ws 127.0.0.1:8090

On the other side is blog-live, about 1,250 lines of Go using coder/websocket. The browser side is around 430 lines of plain JavaScript, no framework. The binary is 8 MB, and right now the whole service uses about 10 MB of memory.

Every browser tab opens exactly one connection, with the page path as its “room”: /ws?room=/2026/10/.../. The server only accepts a room if that page actually exists on disk. Without that check, anyone could open a connection to /ws?room=/something-rude/, and it would show up as a process on the status page.

A tiny protocol#

All messages are small JSON objects with a t field for the type. That’s the whole protocol:

DirectiontWhat it does
server → clienthiyour anonymous id and the cat’s state
client → serverposyour reading position, 0 to 1
server → clientpresencereaders in this room, total online, everyone’s positions
client → serverfeedfeed Segfault
server → everyonecatnew counter, and whether she walks
client → serversubsubscribe to canvas or status
server → clientcanvas / pxthe full wall, or a single pixel
server → clientcooldown“not yet, wait 17 seconds”
server → clientstatusone second of server stats
both waysping / pongmeasure your latency for the status page

Presence without flooding everyone#

The naive version broadcasts every scroll event to every reader. That’s a lovely way to set your server on fire, so there are two brakes.

The browser sends its position at most every 350 ms, and only if it moved. On the server, a pos message only updates a number and marks the room as dirty. A separate loop runs every 300 ms and sends one presence message to each dirty room:

for room := range h.dirty {
	members := h.rooms[room]
	peers := make([]peer, 0, min(len(members), maxPeersInMsg))
	for c := range members {
		if c.pos >= 0 && len(peers) < maxPeersInMsg {
			peers = append(peers, peer{I: c.id, P: c.pos})
		}
	}
	msg := mustJSON(map[string]any{"t": "presence", "n": len(members), "total": total, "peers": peers})
	for c := range members {
		h.sendLocked(c, msg)
	}
}
clear(h.dirty)

Each connection has a buffered channel and its own writer goroutine. Sending never blocks the hub. If a client is so slow that its buffer of 64 messages is full, it gets disconnected; its browser will reconnect with backoff:

func (h *hub) sendLocked(c *client, msg []byte) {
	select {
	case c.send <- msg:
	default:
		c.cancel() // can't keep up: drop it
	}
}

In the browser, the dots are <span>s inside the fixed progress bar, positioned with left: 59.3% and a CSS transition, so they glide instead of jump. The colour comes from a hash of the anonymous id. You keep the same colour for the life of your connection, and nobody learns anything about you.

Segfault, the shared cat#

The real Segfault has been walking over my keyboard since 2014, so it seemed only fair that she gets to walk over yours.

The button sends {"t":"feed"}. The server counts it, saves the counter to disk, and broadcasts a cat message to every connection. Every tab that gets one appends an inline SVG cat to the page. Its legs, tail and body bob are animated with CSS; the walk itself is one translateX from off-screen left to off-screen right. With prefers-reduced-motion on, you only get the notification.

There are two limits. Each IP address can feed her once every 30 seconds, and she walks at most once every 4 seconds globally. Without those, one person with a for-loop would turn everyone’s screen into a cat parade. Admittedly that’s funny, but only once.

After a few hours without food, the footer reports that she “is getting hungry”, which is also true of the real one.

A pixel wall in 1,024 bytes#

The wall is 32×32 pixels in the PICO-8 palette: 16 colours that always look good together. That makes the whole canvas a 1,024-byte array of palette indices. It goes to the browser as base64 inside JSON and gets drawn on a <canvas> with image-rendering: pixelated, so it scales up without blurring.

Placing a pixel sends {"t":"px","x":5,"y":6,"c":8}. The server checks the bounds and the 20-second cooldown, updates its array, and broadcasts the single pixel to everyone looking at the wall.

Anything people can draw on, people will eventually draw something on. So the server keeps an hourly snapshot (the last 72 are kept). There’s also a small admin API that only listens on localhost, so I can clear or restore the wall from an SSH session:

curl localhost:8091/admin/canvas/snapshots
curl -X POST "localhost:8091/admin/canvas/restore?name=canvas-20261013-030000.json"

htop in a browser#

The status page subscribes to status, and from then on the server pushes a sample every second. Nothing in the browser polls. All the numbers come straight from Linux and Caddy:

  • CPU: deltas between two reads of /proc/stat;
  • memory: MemTotal minus MemAvailable from /proc/meminfo;
  • network: byte counters from /proc/net/dev, without the loopback interface;
  • disk: statfs("/");
  • requests per second: Caddy’s Prometheus endpoint. Turning it on takes one global option in the Caddyfile, metrics. I sum caddy_http_requests_total over the top-level subroutes, so every request counts once.

The server keeps the last 120 samples, so a new visitor sees two minutes of graph right away instead of an empty chart.

The process list was the most fun to make. Every room with readers becomes a “process” with a fake PID derived from its path, the user reader, and the command less /2026/10/.... Same data as the reader counts, in a much more satisfying shape.

Privacy#

This site says “no trackers, no cookies” in the footer, and I want that to stay true:

  • No cookies. Your id is random and only lives as long as your connection.
  • IP addresses stay in memory, and only briefly. They’re kept just long enough to enforce the cooldowns and a limit of 12 connections per address. A sweeper forgets them every minute. Nothing about visitors is written to disk.
  • The client IP comes from X-Forwarded-For, but only when the request arrives from localhost, which means Caddy. Otherwise anyone could spoof it.
  • Other websites can’t connect. The server checks the Origin header, so another website can’t open a WebSocket to mine from your browser. That gets a 403.
  • The status page leaves out the details. It shows the kernel’s major.minor version, not the exact patch level, and never the server’s hostname or IP address.

Running it with systemd#

blog-live runs as a systemd service. That’s as close as I get to a container without running a container:

[Service]
ExecStart=/usr/local/bin/blog-live -listen 127.0.0.1:8090 -state /var/lib/blog-live ...
DynamicUser=yes
StateDirectory=blog-live
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
NoNewPrivileges=yes
MemoryDenyWriteExecute=yes
SystemCallFilter=@system-service
CapabilityBoundingSet=
MemoryMax=128M

DynamicUser=yes means the process runs as a throwaway user that doesn’t exist outside the service. The only place it can write is its own StateDirectory, and the rest of the filesystem is read-only to it. systemd-analyze security blog-live gives it a 1.7 out of 10 (lower is better), which systemd labels “OK :-)”. I’ll take the smiley.

Deploying is a small script: go vet, a static build with CGO_ENABLED=0, copy the binary over SSH, systemctl restart. A restart drops all WebSockets for a second; the browsers reconnect without anyone noticing.

Testing, and three bugs that weren’t where I expected#

I tested everything with two headless Chromium instances side by side. One scrolls; the other should see a dot move. One feeds the cat; both should see her walk. One places a pixel; the other should see it appear. That caught three things:

  1. A dot three pixels tall. The other readers’ dots were squashed into thin slivers. The cause was my own CSS from the redesign: .progress span { height: 100% } was meant for the progress bar, and also matched the dots, which are spans too. One > fixed it: .progress > span. CSS selectors are global variables with better marketing.
  2. A headless browser that kept crashing. Chromium crashed on some pages, but only in my minimal test environment. That environment had no system fonts at all. As soon as a page used a glyph that’s missing from my web fonts (an arrow, a check mark), Chromium went looking for a fallback font, found nothing, and fell over. Real visitors never notice this, but it cost me an hour.
  3. A test that clicked thin air. My pixel test passed locally and failed in production. On the live site, the wall was below the fold, so the test clicked a coordinate outside the viewport. The bug was in the test, not the wall. Still, finding that out took longer than I like to admit.

What’s next#

Probably nothing for a while. That’s the nice thing about boring technology with a little bit of fun on top. The blog is still a folder of HTML files. If blog-live ever dies, you simply don’t see the dots, the cat or the wall, and everything else keeps working.

Now go feed Segfault. She’s watching.

    Tip: / opens search, ↑↓ to select, ↵ to open.