# [sv](https://svelte.dev/docs/cli/overview) community add-on: [@omysoul/cf-ws-do](https://github.com/@omysoul/cf-ws-do)

> [!IMPORTANT]
> Svelte maintainers have not reviewed community add-ons for malicious code. Use at your discretion

This allows `DO`s 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:

```shell
npm create cloudflare@latest
```

Choose `framework` then `sveltekit` and `npm` as the package manager.

Then run:

```shell
npx sv add @omysoul/cf-ws-do
npm run cf-typegen
npm run dev
```

Say 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/chat
```

I 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 `DO`s and `WebSockets` should be incredibly cheap even for vast numbers of users spread across many `DO`s. 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.
