<html lang="en">
<head></head>
<body>

<form id="mainForm" method="post" action="https://stackblitz.com/run" target="_self">
<input type="hidden" name="project[files][README.md]" value="# Cloudflare Workers for RivetKit

Example project demonstrating Cloudflare Workers deployment with [RivetKit](https://rivetkit.org).

[Learn More →](https://github.com/rivet-dev/rivetkit)

[Discord](https://rivet.dev/discord) — [Documentation](https://rivetkit.org) — [Issues](https://github.com/rivet-dev/rivetkit/issues)

## Getting Started

### Prerequisites

- Node.js
- Cloudflare account with Actors enabled
- Wrangler CLI installed globally (`npm install -g wrangler`)

### Installation

```sh
git clone https://github.com/rivet-dev/rivetkit
cd rivetkit/examples/cloudflare-workers
npm install
```

### Development

```sh
npm run dev
```

This will start the Cloudflare Workers development server locally at http://localhost:8787.

### Testing the Client

In a separate terminal, run the client script to interact with your actors:

```sh
npm run client
```

### Deploy to Cloudflare

First, authenticate with Cloudflare:

```sh
wrangler login
```

Then deploy:

```sh
npm run deploy
```

## License

Apache 2.0
">
<input type="hidden" name="project[files][package.json]" value="{&quot;name&quot;:&quot;example-cloudflare-workers&quot;,&quot;version&quot;:&quot;2.0.20&quot;,&quot;private&quot;:true,&quot;type&quot;:&quot;module&quot;,&quot;scripts&quot;:{&quot;dev&quot;:&quot;wrangler dev&quot;,&quot;deploy&quot;:&quot;wrangler deploy&quot;,&quot;check-types&quot;:&quot;tsc --noEmit&quot;,&quot;client&quot;:&quot;tsx scripts/client.ts&quot;},&quot;devDependencies&quot;:{&quot;@cloudflare/workers-types&quot;:&quot;^4.20250129.0&quot;,&quot;@types/node&quot;:&quot;^22.13.9&quot;,&quot;tsx&quot;:&quot;^3.12.7&quot;,&quot;typescript&quot;:&quot;^5.5.2&quot;,&quot;wrangler&quot;:&quot;^4.22.0&quot;},&quot;dependencies&quot;:{&quot;rivetkit&quot;:&quot;https://pkg.pr.new/rivet-dev/rivetkit/rivetkit@54999214372f9ecb3f4251a3be45c7a0eb8dfacf&quot;,&quot;@rivetkit/cloudflare-workers&quot;:&quot;https://pkg.pr.new/rivet-dev/rivetkit/@rivetkit/cloudflare-workers@54999214372f9ecb3f4251a3be45c7a0eb8dfacf&quot;},&quot;stableVersion&quot;:&quot;0.8.0&quot;}">
<input type="hidden" name="project[files][tsconfig.json]" value="{
  &quot;compilerOptions&quot;: {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    &quot;target&quot;: &quot;esnext&quot;,
    /* Specify a set of bundled library declaration files that describe the target runtime environment. */
    &quot;lib&quot;: [&quot;esnext&quot;],
    /* Specify what JSX code is generated. */
    &quot;jsx&quot;: &quot;react-jsx&quot;,

    /* Specify what module code is generated. */
    &quot;module&quot;: &quot;esnext&quot;,
    /* Specify how TypeScript looks up a file from a given module specifier. */
    &quot;moduleResolution&quot;: &quot;bundler&quot;,
    /* Specify type package names to be included without being referenced in a source file. */
    &quot;types&quot;: [&quot;@cloudflare/workers-types&quot;],
    /* Enable importing .json files */
    &quot;resolveJsonModule&quot;: true,

    /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
    &quot;allowJs&quot;: true,
    /* Enable error reporting in type-checked JavaScript files. */
    &quot;checkJs&quot;: false,

    /* Disable emitting files from a compilation. */
    &quot;noEmit&quot;: true,

    /* Ensure that each file can be safely transpiled without relying on other imports. */
    &quot;isolatedModules&quot;: true,
    /* Allow &#39;import x from y&#39; when a module doesn&#39;t have a default export. */
    &quot;allowSyntheticDefaultImports&quot;: true,
    /* Ensure that casing is correct in imports. */
    &quot;forceConsistentCasingInFileNames&quot;: true,

    /* Enable all strict type-checking options. */
    &quot;strict&quot;: true,

    /* Skip type checking all .d.ts files. */
    &quot;skipLibCheck&quot;: true
  },
  &quot;include&quot;: [&quot;src/**/*&quot;]
}
">
<input type="hidden" name="project[files][turbo.json]" value="{
  &quot;$schema&quot;: &quot;https://turbo.build/schema.json&quot;,
  &quot;extends&quot;: [&quot;//&quot;]
}
">
<input type="hidden" name="project[files][wrangler.json]" value="{
  &quot;name&quot;: &quot;rivetkit-cloudflare-workers-example&quot;,
  &quot;main&quot;: &quot;src/index.ts&quot;,
  &quot;compatibility_date&quot;: &quot;2025-01-20&quot;,
  &quot;compatibility_flags&quot;: [&quot;nodejs_compat&quot;],
  &quot;migrations&quot;: [
    {
      &quot;tag&quot;: &quot;v1&quot;,
      &quot;new_classes&quot;: [&quot;ActorHandler&quot;]
    }
  ],
  &quot;durable_objects&quot;: {
    &quot;bindings&quot;: [
      {
        &quot;name&quot;: &quot;ACTOR_DO&quot;,
        &quot;class_name&quot;: &quot;ActorHandler&quot;
      }
    ]
  },
  &quot;kv_namespaces&quot;: [
    {
      &quot;binding&quot;: &quot;ACTOR_KV&quot;,
      &quot;id&quot;: &quot;example_namespace&quot;,
      &quot;preview_id&quot;: &quot;example_namespace_preview&quot;
    }
  ],
  &quot;observability&quot;: {
    &quot;enabled&quot;: true
  }
}
">
<input type="hidden" name="project[files][scripts/client.ts]" value="import { createClient } from &quot;rivetkit/client&quot;;
import type { registry } from &quot;../src/registry&quot;;

// Create RivetKit client
const client = createClient&lt;typeof registry&gt;(
	process.env.RIVETKIT_ENDPOINT ?? &quot;http://localhost:8787/rivet&quot;,
);

async function main() {
	console.log(&quot;🚀 Cloudflare Workers Client Demo&quot;);

	try {
		// Create counter instance
		const counter = client.counter.getOrCreate(&quot;demo&quot;);

		// Increment counter
		console.log(&quot;Incrementing counter &#39;demo&#39;...&quot;);
		const result1 = await counter.increment(1);
		console.log(&quot;New count:&quot;, result1);

		// Increment again with larger value
		console.log(&quot;Incrementing counter &#39;demo&#39; by 5...&quot;);
		const result2 = await counter.increment(5);
		console.log(&quot;New count:&quot;, result2);

		// Create another counter
		const counter2 = client.counter.getOrCreate(&quot;another&quot;);
		console.log(&quot;Incrementing counter &#39;another&#39; by 10...&quot;);
		const result3 = await counter2.increment(10);
		console.log(&quot;New count:&quot;, result3);

		console.log(&quot;✅ Demo completed!&quot;);
	} catch (error) {
		console.error(&quot;❌ Error:&quot;, error);
		process.exit(1);
	}
}

main().catch(console.error);
">
<input type="hidden" name="project[files][src/index.ts]" value="import { createHandler } from &quot;@rivetkit/cloudflare-workers&quot;;
import { registry } from &quot;./registry&quot;;

const { handler, ActorHandler } = createHandler(registry);
export { handler as default, ActorHandler };
">
<input type="hidden" name="project[files][src/registry.ts]" value="import { actor, setup } from &quot;rivetkit&quot;;

export const counter = actor({
	state: { count: 0, connectionCount: 0, messageCount: 0 },
	actions: {
		increment: (c, x: number) =&gt; {
			c.state.count += x;
			c.broadcast(&quot;newCount&quot;, c.state.count);
			return c.state.count;
		},
	},
});

export const registry = setup({
	use: { counter },
});
">
<input type="hidden" name="project[description]" value="generated by https://pkg.pr.new">
<input type="hidden" name="project[template]" value="node">
<input type="hidden" name="project[title]" value="example-cloudflare-workers">
</form>
<script>document.getElementById("mainForm").submit();</script>

</body></html>