Friends
Friends is the reference for your own backend. A Cloudflare Worker with a Durable Object keeps who is friends with whom and who is online on which server. A lobby bundle on each Paper server calls it. Players type /friend add <name> and /friend list, and see their friends online when they join.
Source: examples/friends. Its README explains how to deploy the Worker to your own Cloudflare account.
What it shows
Section titled “What it shows”- One project, two plugins.
vite.config.tshas Cloudflare’scloudflare()next tominekube().vite buildbuilds the Worker and the bundle;vite devruns the Worker inworkerdand installs the bundle on the dev server on every save. See Next to a Worker. - Typed calls both ways.
src/FriendsApi.tsis an EffectRpcGroupthat the bundle and the Worker both import, so a change breaks the type check on both sides. - The Worker knows who calls. The bundle connects with a fresh instance token on each connect, offered as the WebSocket’s second protocol. The Worker verifies it with
joseagainst the network’s keys, checks that the key that signed it is the server it names, and hands the connection to the Durable Object with the server’s instance ID, which closes it when the token runs out. A minute before that, the bundle opens a new connection, announces its players there, and closes the old one, so they never show as offline. A member server vouches for its players: the backend believes what it says about who joined it, and lets it read and change the friends only of those players. That is no guard against a dishonest member, which can say any player joined it, and then act for them or rename them; a server you don’t trust that far doesn’t belong in the network. The token’s labels are the server’s own claim, so the Worker uses them as hints only. - Rules apart from the server.
src/Lobby.tsimports no Java. Its Commands call the backend and answer with what it said, so Stories test the lobby without a Worker.worker/Friends.tsis tested in Bun with Effect’sRpcTestand with a WebSocket in memory.
Run it
Section titled “Run it”From the repository:
mk apicd examples/friendsmk serverbun run devmk server started in the folder puts the dev server into the network friends (dev.join), so it gets instance tokens. bun run dev runs vite dev with Node, since Cloudflare’s plugin cannot start workerd from Bun. bun test examples/ runs the rules, the backend and the token checks.
There are no recordings and no end-to-end test with clients yet.
Read next
Section titled “Read next”- Authenticate servers to your backend: instance tokens, and how a backend verifies them.
- Vite plugin and configuration:
issuer()and the Worker next to the bundles.
Was this helpful?
Thanks for the feedback.