Next.js Quickstart
Connect a Next.js client component to Rivet Actors running in a separate node process.
Run your Rivet registry as a separate node process alongside Next.js. Client components connect to the control plane.
Quickstart
Install RivetKit
In your Next.js project, install the SDK and client integration:
npm install rivetkit @rivetkit/next-jspnpm add rivetkit @rivetkit/next-jsyarn add rivetkit @rivetkit/next-jsbun add rivetkit @rivetkit/next-jsDefine an Actor and start the node
Create the registry and a separate entry point under src/rivet/:
import { actor, setup } from "rivetkit";
export const counter = actor({
state: { count: 0 },
actions: {
increment: (c, x: number) => {
c.state.count += x;
c.broadcast("newCount", c.state.count);
return c.state.count;
},
},
});
export const registry = setup({
use: { counter },
});
Add a client component
Create src/rivet/Counter.tsx and render it from src/app/page.tsx:
"use client";
import { createRivetKit } from "@rivetkit/next-js/client";
import { useState } from "react";
import type { registry } from "./registry";
const { useActor } = createRivetKit<typeof registry>({
endpoint: process.env.NEXT_PUBLIC_RIVET_ENDPOINT ?? "http://localhost:6420",
});
export function Counter() {
const [count, setCount] = useState(0);
const counter = useActor({ name: "counter", key: ["my-counter"] });
counter.useEvent("newCount", (value: number) => setCount(value));
return (
<div>
<p>Count: {count}</p>
<button onClick={() => counter.connection?.increment(1)}>Increment</button>
</div>
);
}
Run the node and Next.js
Start the node in one terminal, then your Next.js app in another:
npx tsx --watch src/rivet/node.ts
npm run dev
Open your Next.js app and click Increment. Open http://localhost:6420 to inspect the Actor in the local developer tools.
Deploy
Deploy the node as a long-running process. Set its private RIVET_ENDPOINT and your Next.js app’s NEXT_PUBLIC_RIVET_ENDPOINT to the corresponding production endpoints. Keep the private token on the node.
In development, Rivet runs a local control plane for you and keeps Actor data on your machine.
To scale Rivet in production, pick how much of it you want to run yourself:
Fully managed
Bring your own compute
Full self-hosting
If you are running your own nodes, follow the guide for your hosting provider:
See the React Client API Reference for hooks and event subscriptions.