writeup / the room
One room for the whole site
Most personal sites are a stack of pages that nobody shares. I wanted the opposite: every page of nhaajt.com is a corner of one room, everybody reading it is in that room at the same time, and there is exactly one Bit, the small creature who lives here. This is how the room works, what it refuses to do, and what it costs.
- 1Durable Object holds the whole room, every page.
- 2.8 msmedian frame with 20 lamps lit.
- 51-74 msarrival long task, down from 340-451 ms.
- 2.8 KBto come back to a page, down from 35.9 KB.
01The idea
One room, one Bit. Bit is in exactly one place on the site. If you are on another page, a small sign tells you where Bit is and how many people are with it, with a go and a call button. Bit wanders on its own every twenty minutes, toward wherever the most people are, and sleeps at night in San Jose unless someone wakes it.
Visitors are lamps. You never see a name, a cursor or a message, only a pool of warm light where someone is reading. A page with ten readers is brighter than a page with one.
The floor wears. Every visitor's position is a step on a 32 by 32 grid per page, and steps polish the floor. Paths that many people walk show up as a sheen under the light. Old paths fade with a half-life of two weeks.
Bit keeps a diary. Once a night the room's day is closed and written up in two or three lines, in Bit's voice, from counts only: how many came, how many fed it, how many came in through curl. Which is the last piece: curl nhaajt.com is a door too. A curl visit sends a little $ curl nhaajt.com comet across the front room.
02One Durable Object
The whole room is one Durable Object, addressed by a fixed name. The Worker in front of it does the cheap refusals (method, origin, a per-sender rate limit) and forwards the upgrade with a 12-character slice of a salted hash, so the object can cap tabs per visitor without ever seeing an address.
// worker/world.js export const worldStub = (env) => env.WORLD.get(env.WORLD.idFromName('main')); if (!originAllowed(request.headers.get('Origin'), env)) return json({ error: 'forbidden' }, 403); if ((await hit(env, `world:${sender}`, 600000)) > 60) return json({ error: 'slow down' }, 429, { 'Retry-After': '600' }); fwd.headers.set('x-world-sender', String(sender).slice(0, 12));
Sockets use the hibernation API: this.ctx.acceptWebSocket(server) instead of server.accept(). Between messages the object can be evicted from memory while the sockets stay open, and per-socket state rides on the socket itself through serializeAttachment. Keep-alive pings are answered by an auto-response without waking the object at all.
this.ctx.acceptWebSocket(server); server.serializeAttachment({ id, s: sender, page: null, x: 0, y: 0, w: now, c: 0, a: 0, k: 0 });
State from timestamps
Because the object sleeps, nothing in it ticks. Bit's state is computed when someone asks: hunger is how long ago it was fed, sleep is whether it is night and nobody woke it in the last twenty minutes, wandering is checked on the next connection. No alarms run just to keep a creature alive.
const asleep = night && now - b.woke > AWAKE_MS; const hunger = Math.min(1, Math.max(0, (now - b.fed) / HUNGRY_MS));
Wear works the same way. Each page's grid keeps the time it was last decayed; on the next step it is scaled by the time passed, and writes are batched to storage every few seconds instead of on every step.
const WEAR_HALF = 14 * 86400000; #decay(w, now) { const k = Math.pow(0.5, (now - w.t) / WEAR_HALF); if (k < 0.98) { for (...) w.c[i] = Math.round(w.c[i] * k * 100) / 100; w.t = now; } }
A protocol with no free text
Visitors cannot say anything to each other. Every field on the wire is a number on the grid, a page from the site's own page list, or a verb from a fixed set. Anything else closes the socket. That rules out the whole moderation problem: there is nothing to moderate, because there is no channel for a sentence.
// client -> world: {t:'here',page,x,y} {t:'act',v} ping // v in feed | pat | boo | wake | call, or {v:'open',slug} for a project link const ACTS = new Set(['feed', 'pat', 'boo', 'wake', 'call', 'open']); if (x === null || y === null || !PAGES.includes(m.page)) return ws.close(1008, 'bad frame');
The diary is the only place words appear, and it is written from counts by the server, never from anything a visitor typed.
Limits
- 60 socket opens per sender per 10 minutes at the Worker, 30 state reads per minute.
- 120 sockets in the room, 6 per sender. A stale tab gives way only after two quiet minutes.
- 256 bytes per frame, text only, 12 messages a second per socket. Past three times that, the socket is closed.
- One page per socket, one footstep per second, 2.5 s between acts.
03The light
The lamps, the dust and the worn floor are one fragment shader in one WebGL2 pass: a full-screen triangle over the viewport, up to 32 lamps as uniforms, and the 32 by 32 wear grid as a small texture sampled with a bicubic filter so it reads as polish rather than pixels. If there is no WebGL2, the page keeps plain DOM lamps.
Light is capped. Without it, a crowded page burns out to white. Each pixel's light goes through a soft ceiling, so a crowd makes a page warmer and fuller, never blinding. The cap itself rises a little with the head count.
float cap = uRoom.x; float lum = max(light.r, max(light.g, light.b)); float lv = cap * (1.0 - exp(-lum / cap));
Dust is two layers of motes that only show where there is light, so it sits inside the lamps the way it does in a real beam. Phones get one layer, reduced motion gets none.
04Bit carries you between pages
Leaving a page, an orange sheet sweeps over it. The next page starts already covered, before its first paint, by a small early script in the head, and the sheet is only let go once frames come steadily, so the wipe never fights the page's own startup. When Bit has moved and you follow it, Bit pulls the sheet itself, wearing its current look, which is passed across as a small PNG in session storage.
// js/page-trans-early.js, before first paint const fresh = Date.now() - t < 4000; const dir = fresh ? +sessionStorage.getItem('lab:ptb') || 0 : 0; if (dir || (fresh && !still)) { /* add the covering layer, with Bit on it if it pulled */ }
05Measured
- Frames: 2.8 ms median frame time with 20 lamps on the page.
- Arrival: the long task on arriving at a page was 340-451 ms. Moving the terminal and desk scripts to light obfuscation and deferring modules that are not needed for the first frame brought it to 51-74 ms.
- Coming back: repeat navigation went from 35.9 KB to 2.8 KB, because every script and stylesheet URL carries a content hash and is served immutable.
The room is live on every page of this site. If the page you are on is lit, someone else is reading it too.