I stopped using Cloudflare RPC for this reason and I wanted RPC all the way to the browser. Instead,
I stopped using Cloudflare RPC for this reason and I wanted RPC all the way to the browser. Instead, I implemented a simple JSON-RPC Server in my DO's fetch handler and a proxy in my Worker.
For my newer stuff, I'm using the
Agent
DO base class, My JSON-RPC server code is in the onMessage
handler. I created a simple wrapper for AgentClient
in my client-side code that implements the client-side of RPC so now I have JSON-RPC over Websockets all the way to/from the browser over WebSockets. My Worker code is now super-simple. It simply runs the Cloudflare-provided routeAgentRequest
with my own code to handle authentication.
I highly recommend you look into the Agent
base class. Even if you never use the AI Agent stuff, it has PartyServer baked in which manages your WebSocket connections including reconnects afer network drop outs.
Cloudflare's RPC has one advantage over this. Mine is JSON-only. Cloudflare's works with any data structure that is compatible with the StructuredClone algorithm (using Cap-n-proto, I assume).0 Replies