Alchemy Now Supports Rivet: Self-Host Rivet in a Few Lines of TypeScript
Deploy the Rivet control plane and your Actors to your own AWS, GCP, Kubernetes, Fly or Hetzner account with one command, all declared in TypeScript next to your app.
Alchemy now supports Rivet. You can run the Rivet control plane and your Rivet Actors in your own cloud with one command, alchemy deploy.
Why Alchemy
Infrastructure as code keeps your cloud setup in Git, where it is reviewed, versioned and reproducible instead of clicked together in a console. Alchemy brings this workflow to Rivet.
- Declarative infrastructure. It makes for a fluid deployment experience:
alchemy deployapplies only what changed, so when you edit an Actor, only its node pool rolls out. - CI integration for deploys. The same command deploys from CI, with a stage per pull request, and each stage gets its own Rivet namespace.
- Any cloud, your account, your data. It runs on AWS, Google Cloud, Kubernetes, Fly, Hetzner or Docker, in the account you sign in to.
- Effect integration. Alchemy is built on Effect, so your infrastructure and your app are one typed program.
- What you can do with Effect. With Alchemy’s own Durable Object API, a Lambda, Worker or container calls an Actor with
yield*and gets typed methods, streams and tagged errors across the wire.
See Architecture for how the control plane and nodes fit together.
Getting started
Deploy an existing or bootstrap a new Rivet project with Alchemy by pasting this prompt into your coding agent:
Help me deploy a Rivet project with Alchemy. If I don't have one yet, bootstrap a minimal Rivet Actor first by following https://rivet.dev/docs/actors/quickstart/backend/.
Then read https://alchemy.run/rivet/setup and follow it: install Alchemy and the Rivet provider, and create an `alchemy.run.ts` Stack.
- Deploy my existing Actors unchanged by pointing a `Rivet.DurableObject` at my registry entry file, as https://alchemy.run/rivet/rivetkit describes (or https://alchemy.run/rivet/rivetkit-effect for the Effect SDK).
- Ask me which cloud to use, then pick its control plane and node pool from https://alchemy.run/rivet/control-planes and https://alchemy.run/rivet/pools.
For anything else, fetch https://alchemy.run/llms.txt and look up the page you need instead of guessing URLs.
Or set it up by hand:
Install
bun add alchemy @alchemy.run/rivet @alchemy.run/aws effect @effect/platform-bun @effect/platform-node rivetkit
Write a Rivet Actor
TypeScript
import { actor, setup } from "rivetkit";
import { db } from "rivetkit/db";
const chatRoom = actor({
db: db({
onMigrate: async (db) => {
await db.execute("CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, sender TEXT, text TEXT)");
},
}),
actions: {
sendMessage: async (c, sender: string, text: string) => {
await c.db.execute("INSERT INTO messages (sender, text) VALUES (?, ?)", sender, text);
c.broadcast("newMessage", { sender, text });
},
history: (c) => c.db.execute("SELECT sender, text FROM messages ORDER BY id"),
},
});
export const registry = setup({ use: { chatRoom } });
registry.start();
Run it locally.
npx tsx watch src/rivet/main.ts
Effect
This version also needs the @rivetkit/effect package.
import { NodeRuntime } from "@effect/platform-node";
import { Action, Actor, Client, Registry } from "@rivetkit/effect";
import { Effect, Layer, Schema } from "effect";
import { db } from "rivetkit/db";
const SendMessage = Action.make("SendMessage", {
payload: { sender: Schema.String, text: Schema.String },
});
const History = Action.make("History", {
success: Schema.Array(Schema.Struct({ sender: Schema.String, text: Schema.String })),
});
const ChatRoom = Actor.make("chatRoom", { actions: [SendMessage, History] });
const ChatRoomLive = ChatRoom.toLayer(
Effect.fnUntraced(function* ({ rawRivetkitContext: c }) {
const sql = <Row extends Record<string, unknown>>(query: string, ...args: unknown[]) =>
Effect.tryPromise(() => c.db.execute<Row>(query, ...args)).pipe(Effect.orDie);
return ChatRoom.of({
SendMessage: Effect.fnUntraced(function* ({ payload }) {
yield* sql("INSERT INTO messages (sender, text) VALUES (?, ?)", payload.sender, payload.text);
c.broadcast("newMessage", payload);
}),
History: () => sql<{ sender: string; text: string }>("SELECT sender, text FROM messages ORDER BY id"),
});
}),
{
db: db({
onMigrate: async (db) => {
await db.execute("CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, sender TEXT, text TEXT)");
},
}),
},
);
const endpoint = process.env.RIVET_ENDPOINT ?? "http://127.0.0.1:6420";
const Actors = ChatRoomLive.pipe(Layer.provide(Client.layer({ endpoint })));
Layer.launch(Registry.serve(Actors).pipe(Layer.provide(Registry.layer()))).pipe(NodeRuntime.runMain);
Run it locally.
npx tsx watch src/rivet/main.ts
Alchemy
This version uses Alchemy’s own Durable Object API. The class defines both the Actor and its deployment, so it replaces src/rivet/main.ts and the src/alchemy/chat-room.ts in the next step.
import * as Rivet from "@alchemy.run/rivet";
import * as Effect from "effect/Effect";
import { Default } from "./cluster.ts";
export class ChatRoom extends Rivet.DurableObject<ChatRoom>()(
"ChatRoom",
{ main: import.meta.url },
Effect.gen(function* () {
return Effect.gen(function* () {
const sql = (yield* Rivet.DurableObjectState).storage.sql;
yield* (yield* sql.exec("CREATE TABLE IF NOT EXISTS messages (sender TEXT, text TEXT)")).toArray();
return {
sendMessage: (sender: string, text: string) =>
Effect.gen(function* () {
yield* (yield* sql.exec("INSERT INTO messages (sender, text) VALUES (?, ?)", sender, text)).toArray();
}),
history: () =>
Effect.gen(function* () {
const rows = yield* sql.exec<{ sender: string; text: string }>("SELECT sender, text FROM messages");
return yield* rows.toArray();
}),
};
});
}).pipe(Effect.provide(Default)),
) {}
To run it locally on Docker, use RivetDocker.SwarmControlPlane as shown in the tutorial.
Declare the control plane and node pool
import * as RivetAWS from "@alchemy.run/rivet/AWS";
import * as Layer from "effect/Layer";
export const Cluster = RivetAWS.Ec2ControlPlane("Rivet");
export const Default = RivetAWS.EcsPool("Default").pipe(Layer.provide(Cluster));
import * as Rivet from "@alchemy.run/rivet";
import * as Effect from "effect/Effect";
import { Default } from "./cluster.ts";
export const ChatRoom = Rivet.DurableObject("ChatRoom", { main: "./src/rivet/main.ts" }).pipe(
Effect.provide(Default),
);
import * as AWS from "@alchemy.run/aws";
import * as Rivet from "@alchemy.run/rivet";
import * as Alchemy from "alchemy";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { ChatRoom } from "./src/alchemy/chat-room.ts";
export default Alchemy.Stack(
"ChatApp",
{
providers: Layer.mergeAll(AWS.providers(), Rivet.providers()),
state: Alchemy.localState(),
},
Effect.gen(function* () {
yield* ChatRoom;
}),
);
Connect your AWS account
bunx alchemy profile edit --add AWS
Deploy
bunx alchemy deploy
Destroy (optional)
bunx alchemy destroy