@omysoul/cf-ws-do
v0.0.5
Published
sv add-on for @omysoul/cf-ws-do
Readme
sv community add-on: @omysoul/cf-ws-do
[!IMPORTANT] Svelte maintainers have not reviewed community add-ons for malicious code. Use at your discretion
This allows DOs and WebSockets to run in dev using a single worker with live reload (but not hot module). This is a DX game changer for developing collaborative apps on Cloudflare.
These can be nested in routes with a naming convention .do.ts or ws.ts. You can even name a file .do.svelte.ts this gives you rune based server side reactive state in your DO (just because you can does not mean you should). The WebSockets also pass through the server hooks so they can make use of auth etc.
cloudflare: imports also work.
Usage
First to create a project run:
npm create cloudflare@latestChoose framework then sveltekit and npm as the package manager.
Then run:
npx sv add @omysoul/cf-ws-do
npm run cf-typegen
npm run devSay yes to install demo routes. Having demo route will help LLMs add the routes you want.
Try opening the demo routes:
http://localhost:8787/do-examples/counter
http://localhost:8787/do-examples/chatI have found it pretty easy just to ask an LLM to build a chat room or wire up a text editor to Y.js.
Disclaimer
This is an early proof of concept. Use at your own risk. Used correctly hibernating DOs and WebSockets should be incredibly cheap even for vast numbers of users spread across many DOs. I think:
$7.50 per billion incoming DO WS requests
$0 for out going WS requests
(small charge for memory as long a DO hibernates correctly)However there is a foot gun to be aware of. If a client receiving a message causes them to immediately resend a message in a infinite feedback loop then you could end up with an infinite bill. So worth adding some billing alarms and detecting unreasonably high numbers of incoming WS requests and circuit breaking.
