Skip to content

About

Cloudflare Worker server example implementation of the Apron Chat Protocol

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

Apron on Cloudflare Workers

The public demo server for the Apron chat protocol. It runs the whole chat — rooms, threads, reactions, history, passkeys, and bots — in a single SQLite Durable Object, and stays inside the usage included with the Cloudflare Workers Paid ($5/month) plan. The Workers Free budgets are kept in src/plans/free.ts for a zero-overage deployment.

Try it: web.apron.chat  ·  Connect: wss://server.apron.chat/

Highlights

  • One Durable Object, hibernating WebSockets. A permanent general room, rooms admins and mods create, and thread rooms, with complete-snapshot history, edits, moves, and emoji reactions.
  • Guests and passkeys. Anyone can read as a guest; sign in with a passkey to post, and run /invite-bot to get a token for your own bot. Clients on other domains and native apps use the passkey page on the server's own origin, and finish with an OAuth code.
  • Private rooms for DMs. Signed-in users start private rooms (private: true) that only their members see, and add people to them; everyone else is answered as if the room did not exist. A room goes when its last member leaves or its history expires; see limits for how many each user may have.
  • Images, voice clips, Markdown files and avatars. Signed-in users attach images, voice clips and Markdown files and set avatars (embed:upload), stored in R2 within its free tier: attachments for a month, as long as their messages, and avatars while their owners keep signing in. Admins, mods and friends add custom emoji with /emoji add :name:, which everyone can react with (ext:emoji).
  • Live terminals and streamed replies. /embed-terminal and /embed-append give a link to curl -T a terminal session or an LLM's reply into the room, which everyone watches grow, and bots do the same with a message's terminal or append embed (embed:terminal, embed:append). Streams last up to two minutes (five for admins and mods), one at a time per user, held in memory and stored once when they end; see live text.
  • Push for mentions and replies. With VAPID keys set, a mention or a reply wakes a signed-in user whose every tab is idle or closed, through their browser's Web Push service (server.push kind webpush), unless they muted it with status, everywhere or in that room, or chose dnd. The push waits 30 seconds first, and doesn't go if they come back or post in that room meanwhile. Admins and mods can also ask to be woken for every message in the rooms they joined (joined).
  • Who's around. With push on, member lists show each user as online, idle, busy (dnd), or offline, with when they were last seen, and a user can choose dnd, invisible, or no status at all with me. A chosen status, or coming back online, shows at once; other changes are coalesced to one a minute per user, and seen may be up to 10 minutes early (see user status).
  • Bring your own frontend. Guest connections are accepted from any origin — even localhost or a browser console.
  • Built to stay inside its plan. Every operation is metered against daily budgets, a budget guard caps the month's bill (on Workers Paid, $5 a day and $25 a month past the plan's $5; see configuration), with rolling retention (30 days on Workers Paid, which production runs, and one day on Workers Free) and graceful read-only degradation when budgets run out.

Quick start

npm ci
npx wrangler dev --port 8080

Then run the web client from apron-chat/apron-web with npm run dev (see its README); its dev server proxies /ws to port 8080. Open http://localhost:5173. Check your work with:

npm run typecheck
npm test

Or say hello from any browser console:

const socket = new WebSocket('wss://server.apron.chat/');
socket.onmessage = ({ data }) => {
  const frame = JSON.parse(data);
  console.log(frame);
  if (frame.method === 'server') {
    socket.send(JSON.stringify({ id: 'auth', method: 'auth', params: { scheme: 'guest' } }));
    socket.send(JSON.stringify({ id: 'history', method: 'history', params: { room_id: 'general' } }));
  }
};

Documentation

Developer and operator guide Local development, custom frontends, bots, user-visible policies, deployment, Previews, and operations
Implementation specification The protocol contract, architecture, and limits
Authentication and policy Guests, passkeys, and demo policy
Limits for client developers Every limit a client can hit, how it shows up, and a checklist for a well-behaved client
Configuration reference Every variable, secret and budget
Edge admission WAF rules and the quota-exhaustion runbook
Cost report Measured resource bounds and assumptions

License

MIT

About

Cloudflare Worker server example implementation of the Apron Chat Protocol

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages