r/place on the atmosphere, targeting cloudflare's free tier
README.md

atplace #

An r/place clone on atproto, running on Cloudflare Workers.

Running locally #

You need Node 24+, pnpm, and an atproto account (e.g. Bluesky) with an app password.

  1. Install dependencies:

    pnpm install
    
  2. Create a canvas record in your repo:

    ATP_IDENTIFIER=you.bsky.social ATP_APP_PASSWORD=xxxx-xxxx-xxxx-xxxx pnpm create-canvas --rkey test
    

    The script logs in at your account's own PDS, found from your handle; set ATP_SERVICE to override it. It prints the canvas's AT-URI. Optional flags: --name, --width, --height, --cooldown (seconds), --palette (comma-separated #rrggbb), --starts-at and --ends-at (ISO datetimes). The defaults are 1000×1000, the 2017 r/place palette and a 5-minute cooldown.

  3. Tell your local instance to serve that canvas:

    echo 'CANVAS_ALLOWLIST=at://did:plc:.../dev.marantz.atplace.canvas/test' > .dev.vars
    
  4. Start the dev server:

    pnpm dev
    
  5. Open http://127.0.0.1:5173. Use 127.0.0.1, not localhost, or OAuth sign-in will fail.

If you add more than one canvas, the URIs in CANVAS_ALLOWLIST are comma-separated. Pick one with ?canvas=<at-uri>.

Building a custom client #

Any atproto client can play. The page in src/web is just one implementation.

Placing a pixel takes two calls:

  1. Create a dev.marantz.atplace.pixel record in the player's own repo with com.atproto.repo.createRecord, leaving the rkey unset so the PDS generates one:

    { "canvas": "<canvas at-uri>", "x": 12, "y": 34, "color": 5, "createdAt": "<ISO datetime>" }
    

    color is an index into the canvas palette. The schema is in lexicons/.

  2. POST the new record's URI to the instance:

    POST /api/canvas/<canvas did>/<canvas rkey>/place
    {"uri": "at://<player did>/dev.marantz.atplace.pixel/<rkey>"}
    

    The response is {"ok": true, "nextPlaceAt": <ms>} or {"ok": false, "error": "...", "nextPlaceAt"?: <ms>}. If a placement is rejected, delete the record so it doesn't linger in the player's repo.

The instance fetches the record from the player's PDS to verify it, so the POST needs no authentication. Each new pixel record must have a newer rkey than the player's last accepted one.

Watching a canvas: open a WebSocket to /api/canvas/<canvas did>/<canvas rkey>/ws. GET /api/canvases lists the canvases an instance serves.

Message Format
Config JSON text: {"type": "config", "config": {...}}
Snapshot Binary: byte 1, then gzipped palette indices (1 byte per pixel, row-major)
Diff Binary: byte 2, then repeated 5-byte entries (u16 x, u16 y, u8 color, big-endian)

The server sends the config and then the snapshot when you connect, then diffs about once a second. A config message can arrive again at any time if the canvas owner edits it. Diffs can arrive before the snapshot, so buffer them until it does. src/shared/protocol.ts has an encoder and decoder you can reuse.

Deploying your own instance #

Each instance runs on its operator's own Cloudflare account. It fits within the free plan.

  1. Create a canvas record as in step 2 above. Choose its size carefully, because it can't change later.

  2. Copy deploy.env.example to deploy.env and fill it in. You need CANVAS_ALLOWLIST, and optionally DEPLOY_DOMAIN, a custom domain on a zone in your Cloudflare account. Without a domain, the instance is served on workers.dev.

  3. Log in and deploy:

    pnpm exec wrangler login
    pnpm run deploy
    

    Use pnpm run deploy, not pnpm deploy, which is a pnpm built-in.

The deploy sets the allowlist on every run. Change deploy.env and redeploy, rather than editing variables in the Cloudflare dashboard, which the next deploy overwrites.

See CONTRIBUTING.md for how it works.