🐇 Burrow 10 · Nov 12 · 5 min read

Multiplayer on a Site With No Server

Climb out

Wait, Cloudflare does databases?

I thought the whole site had to stay static with no database, then found out Cloudflare does databases too and pretty well. So I wanted some kind of community thing, not stories or anything just drawings, like r/place where everyone draws on one big canvas at the same time and you can save it to a gallery and sign your name if you want.

So what even is a Worker?

A Worker is a small chunk of code that runs on Cloudflare's servers instead of in your browser. A page asks it something and it answers from whatever Cloudflare spot is closest to you, usually in a few milliseconds. I don't have a server sitting anywhere, Cloudflare just wakes the code up when someone needs it and the free plan covers 100,000 of those a day.

The site has two kinds. The small ones sit right next to the pages and do the boring stuff like counting page opens for the popular tools list and loading the Community Gallery. Then there's a separate one at live.localscriptbox.com that runs everything multiplayer, and that one uses Durable Objects, which are basically Workers with their own little database attached. There's only ever one copy of each so everybody on the page is talking to the same one.

How everyone sees the same drawing

When you open Live Canvas your browser keeps a connection open to the server (a WebSocket). One Durable Object on Cloudflare holds the whole drawing as a 640 by 400 grid of 32 colors, and every line you draw gets sent to it, it adds it, and sends it to everyone else who's on. No accounts and nothing about the people drawing gets saved.

live-canvas/src/index.jsWhat a stroke looks like on the wire
// Messages, page -> server (binary): byte 1, then strokes of 10 bytes each:
//   x1, y1, x2, y2 (16-bit little-endian), color (palette index), size (1..MAX_SIZE)
const W = 640, H = 400, COLORS = 32, MAX_SIZE = 16;
const RATE = 25;   // messages per second per person

Every piece of a line is only 10 bytes for the start, end, color and size, which is way smaller than sending pictures around. The rate limit is so one person or a bot can't flood everyone else.

"It says 2 people here"

The canvas shows how many people are on it, and the first time I tested it it said 2 and I was like who is that lol, it was just my other tab.

Up to 250 people can be on it at once. The free plan only gives you 100,000 database writes a day, so strokes get bundled together and the drawing only saves every 15 seconds, which means a lot of people would have to draw nonstop all day to hit that.

Getting into the gallery

Live Canvas has a vote to save button. If half the people on right then vote for it (and at least 2) it saves a snapshot, and I look at it before it goes in the Community Gallery so nobody can just dump something nasty in there.

Draw & Guess

Then a draw and guess game. I figured real games might suck with the Cloudflare limits but draw and guess sounded fun, and what I kept thinking about was what happens if 5,000 people are on the page at once, like who gets to draw and for how long?

So it picks one person from the line to be the artist and they get 5 minutes to accept or it moves on to someone else, then 10 minutes to draw. It can be a whole phrase and the drawing stays up for a day or till somebody guesses it.

live-canvas/src/guess.jsAll the timers in one spot
const ACCEPT_MS = 5 * 60e3, DRAW_MS = 10 * 60e3, ROUND_MS = 24 * 3600e3, HINT_MS = 3 * 3600e3;
const SHOW_RESULT_MS = 60e3, COOLDOWN_MS = 24 * 3600e3, IP_TURNS = 3, MIN_INK = 150, KEEP_ROUNDS = 40;

5 minutes to accept, 10 to draw, and it stays up 24 hours with a hint every 3 hours if nobody's got it. After you draw you have to wait a day before you can again, and if someone barely draws anything (MIN_INK) the round gets thrown out and it just picks the next person.

No accounts though

I didn't want accounts so your browser just makes up a random key when it connects, and that key holds your spot in line and lets you draw when it's your turn.

The phrases get made up from word lists too so you get stuff like a grumpy penguin surfing on the moon or a wizard in a bathtub lol.

Imposter

The last one that night was an imposter word game. You make a room and get a code to send people, and the host can pick how many imposters, kick people, set the timers and turn word hints on or off. Everyone gets the same secret word except the imposter, then everyone gives clues and votes on who it is.

There's extra roles too like Accomplice, Jester, Detective and Undercover if you want it chaotic, and up to 16 people can be in a room.

live-canvas/src/imposter.jsThe imposter never even gets the word
if (role === 'imposter') { y.word = null; y.hint = r.settings.hint ? g.category : null; y.fellow = Object.keys(g.roles).filter((k) => k !== id && g.roles[k] === 'imposter'); }

Every player gets their own copy of the game state. If you're the imposter your word gets set to null before it leaves the server, so you can't open dev tools and peek at it, it was never sent to you. You just get the category if hints are on and who the other imposters are.

Party Cards

Party Cards is a fill in the blank one, somebody's the judge and reads a prompt and everyone else plays their funniest card face down. The decks it comes with are kept PG-13 so the site stays ok for ads, but you can import your own deck for whatever you want.

Rooms clean themselves up

Imposter and Party Cards use rooms with codes. A room with nobody in it for 6 hours deletes itself so nothing just sits there forever, and Cloudflare's free plan has limits on how much you can write so the games only save what they actually need. Names also go through a bad word filter before anyone else sees them.

The burrow ends hereClimb back up to the surface