Bicameral: one live table on Yjs
A conspiracy-night talk where the speaker's phone is the Game Master, the projector follows, and every phone in the room is a player. Two Yjs documents and one free server hold it together.
Yjs turned "keep a room of devices in sync" into "edit two shared objects", and Hocuspocus turned "who may edit" into one flag per connection.
A talk you play, not watch
I gave a talk on Julian Jaynes’ Bicameral Mind at a local conspiracy night, and I built it as a tabletop game. I am the Game Master. The room are my players.
Jaynes argued that until about 3,000 years ago, people did not introspect. One half of the brain spoke commands, and the other half heard them as the voice of a god and obeyed. That idea begs to be performed, not described. So at one point in the talk, every phone in the room turns into a cloud-god and tells its owner to raise their right hand. Some phones whisper a private command that only that person hears.
There are three kinds of screen:
| URL | Device | What it does |
|---|---|---|
/gm | My phone | Scroll the talk, open encounters, whisper, spotlight, laser pointer, blackout |
/screen | Projector laptop | Nothing; it follows me |
/ | Audience phones, via a QR code | Join as a character, then write, answer, obey or walk only while I allow it |
The talk runs 20 scenes. Between them sit encounters: open-ended thoughts that float into the projector’s sky, trivia and Stroop speed rounds, the Voice of the Gods, and a free-roam mode where players steer tiny avatars across the projector. XP and a “Most Bicameral” leaderboard close it out.
The problem: many screens, one truth, no budget
Every device in the room has to agree on what is happening, within a frame or two, over venue Wi-Fi and mobile data. Three constraints shaped everything.
- One writer for the story, many writers for the crowd. Only I may move the talk forward. Players may write, but only inside a window I open, and the server must enforce that, not the client. Someone in a conspiracy-night crowd will open devtools.
- It must be free. No paid realtime service, no managed database. It runs on Render’s free tier, with a Cloudflare quick tunnel from my laptop as the backup.
- It must survive the room. Phones lock, reload and drop off Wi-Fi. My own phone might reload mid-talk. A reconnecting device has to land in the right place without a “resync” button.
A hand-rolled WebSocket server with JSON messages would cover the happy path. It would not cover reconnection, late joiners, out-of-order delivery or merging two writes to the same list. Those are exactly what a CRDT gives you for free.
How Yjs carries it
Yjs let me treat the whole room as two shared JavaScript objects. Every device holds a local copy, writes to it, and Yjs ships the binary diffs. Reconnects, late joiners and concurrent pushes to the same array merge by themselves. Hocuspocus is the server: a thin WebSocket relay for Yjs documents with hooks for auth and persistence.
Two documents per room, split by who may write
The key design move was splitting state by write permission rather than by feature.
| Document | Holds | Who writes |
|---|---|---|
room:stage (a Y.Map) | scene, scroll progress, the open encounter, laser pointer, blackout, QR, spotlight, sound cues | The GM only, always |
room:crowd | thoughts (Y.Array), answers and obeys (Y.Map, keyed encounterId:uid) | Players, only while an encounter is open; the GM always |
Awareness on crowd | Each player’s character and avatar position | Each player, for themselves; never stored |
Because the permission boundary is the document boundary, the server never inspects individual writes. It only decides, per connection, whether that connection is read-only.
The GM’s writes to the stage are always accepted. A player’s write to the crowd lands only while the stage’s encounter is open, because the server watches that flag and flips each player connection’s readOnly.
// server/index.ts: role from the token, read-only decided per connection
if (role !== 'gm') {
connectionConfig.readOnly = doc === 'stage' || role === 'screen'
? true
: !encounterOpen(instance.documents.get(docName(room, 'stage')))
}
The server also watches the stage doc. When I open or close an encounter, it flips every player’s crowd connection in place, with no reconnect:
crowd.connections.forEach((_v, conn) => {
if (conn.context?.role === 'player') conn.readOnly = !open
})
Awareness for things that move
Avatars walking across the projector are not document state. They use Yjs awareness, an ephemeral side channel that is broadcast and then forgotten. Players publish at most every 80 ms (about 12 Hz). On Render’s free tier those arrive in batches of about 5 Hz, so the projector eases each avatar with a 250 ms CSS transition to hide the steps. The server also clears any awareness a non-GM sends on the stage doc, so nobody can fake my laser pointer.
Derived, never written: XP
No client ever writes a score. Every screen computes XP from the crowd doc: 15 per thought (capped at 3 per encounter), 25 per obeyed command, and trivia at 100 for a right answer plus up to 100 for speed. A player who edits their own client can only change what their own screen shows.
Persistence that costs nothing
Hocuspocus’ database extension snapshots each doc to a .bin file, debounced to every 2 s. If the server restarts mid-talk, scene position, thoughts and scores come back. React reads Yjs through useSyncExternalStore with a cached snapshot, so components re-render only when their own type changes.
One interaction, end to end
Take the showpiece: I whisper “Stand up” to a single player.
- On my phone I pick the command, choose Whisper to one…, pick the player, and press Speak.
- My phone writes one transaction to the stage doc:
encounter = { type: "voice", open: true, prompt, voice, targets: [uid] }. - Hocuspocus relays the diff to every device. Its own observer sees
openflip to true and makes every player’s crowd connection writable. - Each phone checks
targets. Only the named player’s phone turns into the cloud-god, vibrates and plays the whisper clip. Everyone else sees nothing new. - The player taps I obeyed. Their phone writes
obeys.set("encounterId:uid", …)to the crowd doc. - Every screen observes that write and re-derives XP, so the projector’s party list and the player’s own badge both gain 25.
- I close the encounter. The server sees
openflip back and every player’s crowd connection becomes read-only again.
Scrolling the talk itself is the same pattern at a higher rate. My phone turns scroll position into { scene, progress } and writes it at most every 50 ms (about 20 Hz), always sending the latest value. The laser pointer streams at about 30 Hz. If my phone reloads, it reads the stage doc and scrolls itself back to where the projector already is.
Reading the numbers in the demo
The whole 144-second demo, with two players, a thoughts round, two god commands, two trivia rounds and a walk, moved 18.7 KB of Yjs document updates to the projector. The peak was 18 updates per second.
The demo video is a single take of a /showcase page. It puts the GM phone, the projector, two player phones and two terminal-style panels on one 1920×1080 canvas. The panels are not mock-ups. The page opens its own connection to the room with the read-only screen role and prints what it actually receives.
The status bar
| Stat | What it counts | Seen in the demo |
|---|---|---|
| updates/s | Yjs update events on both docs, counted over 500 ms windows and scaled to per second | 0 at rest; 10–18 while I scroll or draw with the pointer |
| KB synced | Running total of the bytes in those binary updates since the page opened | 0 → 18.7 KB over 144 s |
| awareness/s | Awareness change events per second (presence that actually moved) | 0 until the walk; 12 with two players walking |
| players | Awareness states whose role is player | 0 → 1 → 2 as the phones join |
Three caveats when you explain these:
- They are what one follower received, not server totals. With N followers the server sends roughly N times this.
- KB synced counts document updates only. Awareness traffic and WebSocket framing are not in it, which is why walking barely moves it.
- The rates are sampled every 500 ms, so a single tap often reads as 2 updates/s.
Traffic comes in short bursts when I scroll or draw with the pointer, and sits at zero between them. The walk at 2:08 sends awareness to 12 events a second, yet the total barely moves, because awareness is not counted.
The log, interaction by interaction
Each row is a moment in the video, the line the log prints, and what the panels do.
| Time | Interaction | Log line | Panels |
|---|---|---|---|
| 0:08, 0:10 | Two players join | aware · awareness: 2 players at the table (+Gargi) | players 0 → 2 |
| 0:16–0:21 | I tap Next, scene 2 | stage.scene = 1, stage.progress = 0.250 | 10–14 updates/s |
| 0:25 | I open a Thoughts encounter | stage.encounter = { type: "thoughts", open: true, prompt: "…" } | Roles panel: crowd OPEN, players get write |
| 0:28, 0:31 | Players post thoughts | crowd · thoughts.push("…") · Hector | KB ticks up; bubbles float onto the projector |
| 0:36 | I close it | stage.encounter = { type: "thoughts", open: false } | crowd LOCKED |
| 0:59 | Laser pointer from my phone | stage.pointer = { x: 0.42, y: 0.31, on: true } | Updates climb with the ~30 Hz stream |
| 1:11 | Voice of the Gods, to all | stage.encounter = { type: "voice", open: true, prompt: "Raise your right hand" } | Both phones become the cloud-god |
| 1:14, 1:15 | Players tap I obeyed | crowd · obeys.set(Hector) · "I obeyed" | +25 XP on both phones |
| 1:17 | Whisper to one player | stage.encounter = { type: "voice", … } | Only one phone lights up |
| 1:50, 1:58 | Trivia, then a Stroop round | crowd · answers.set(Gargi, choice 0, 2140 ms) | Histogram on the projector; XP after reveal |
| 2:08 | Walk the Earth | stage.walk = true | awareness/s 0 → 12; KB nearly flat |
| 2:19 | Leaderboard | stage.leaderboard = true | Final total 18.7 KB |
The example values inside quotes and braces are illustrative; the shape of each line is exactly what the inspector prints.
Two things engineers will ask about
Why does the log repeat stage.scene = 3? Every scroll tick writes scene and progress together, and Yjs emits a change even when a key is set to the value it already holds. The log shows at most four progress lines a second to stay readable, so the repeated scene lines are what is left. It is harmless but wasteful; see the lessons below.
Is the roles panel reading the server? No. It applies the server’s rule to the encounter it sees: GM writes both docs, projector writes neither, players write crowd only while an encounter is open. The proof that the server enforces it is npm run acl-test, which passes all seven checks against a live server: a player cannot write the stage, cannot write the crowd while it is locked, and a wrong presenter key is rejected.
The craft: a shoebox on every screen
Sync is half of it; the other half is that a room should want to look up. Each of the 20 scenes is a paper diorama in a shoebox, and the same box is rendered on the projector and on every phone.
- Plates. One generated plate per scene, all matched to a single reference style, with rules that keep the cutting pipeline happy: separate foreground figures, a calm caption band along the bottom, no text. A script cuts each plate into a back wall and foreground cutouts listed in a
layers.json. - The box. The projector screen is the box’s opening. An off-axis projection pins the opening to the screen edges while the eye drifts, so only the inside moves. Cutouts pivot at their feet and sway, and light shafts with dust fall from the painted beams.
- Phones look around. On a phone, tilting the device moves the eye inside the box. Tilt is written straight to CSS variables, so it re-projects without React re-renders.
- Collage. Accents are CC0 museum objects from The Met and the Cleveland Museum of Art: a Sumerian worshipper, an Assyrian lamassu, Indus Valley seals, a Corinthian helmet.
- The god’s voice. 40 command lines, generated offline with the open-source Kokoro text-to-speech model and ffmpeg. Each line has three takes: a booming male voice with temple reverb for the projector, and a close female or male whisper for phones. Each phone keeps one voice. No speech API is called at runtime.
- The player’s phone is an aged notepad. When the god speaks, the words write themselves in like an enchanted diary.
What I’d tell another engineer
What worked
- Split documents by who may write, not by feature. Server-side permissions shrink to one boolean per connection. Hocuspocus can flip
readOnlyon a live connection, so opening an encounter needs no reconnect. - Derive anything players would want to cheat. Scores are a pure function of the crowd doc. There is no score field to forge.
- Put motion in awareness, and smooth it on the receiver. Document history never fills with avatar coordinates, and a 250 ms ease hides the free tier’s ~5 Hz batching.
- Throttle at the writer, always send the latest value. 20 Hz for scroll and 30 Hz for the pointer feel live, and an idle room costs nothing.
- Build the inspector. The
/showcasepage made sync visible, caught bugs, and became the demo. Ayjs tailpanel is worth an afternoon on any Yjs project. - Test the rules, not the UI.
npm run acl-test(7 checks) andnpm run load -- 80 15(80 simulated players walking and posting for 15 s) ran against the real server.
Limits and what I’d change
- Skip unchanged keys. Each scroll tick rewrites
sceneeven when it has not changed. Comparing before each set would trim every scroll update and clean up the log. - Permissions are per document, not per key. While an encounter is open, a player with devtools could write an answer under someone else’s id. For one evening that was acceptable. A product would validate each write on the server or give each player their own sub-document.
- The presenter key is one shared secret in the connection token. That is fine for one speaker and one night, not for a multi-GM platform.
- The free tier is a trade. Render’s free instance sleeps after about 15 idle minutes, so I opened
/screena few minutes early to wake it. A restart there also wipes the disk, so the 2-second snapshots only protect a run on my own laptop. The Cloudflare quick tunnel from my laptop was the backup.
The one-line version for engineers: Yjs turned “keep a room of devices in sync” into “edit two shared objects”, and Hocuspocus turned “who may edit” into one flag per connection.
It also lives in rabbit holes, with a recording.