Quickstart

React Quickstart

Build realtime React applications with Rivet Actors

Steps

Install Dependencies

npm install rivetkit @rivetkit/react
Command Line

Create Backend Actor

Create your actor registry on the backend:

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 },
});
backend/registry.ts

Setup Backend Server

Integrate with your preferred web framework:

Create React Frontend

Set up your React application:

import { createRivetKit } from "@rivetkit/react";
import { useState } from "react";
import type { registry } from "./registry";

const { useActor } = createRivetKit<typeof registry>();

function Counter() {
	const [count, setCount] = useState(0);

	// Get or create a counter actor for the key "my-counter"
	const counter = useActor({
		name: "counter",
		key: ["my-counter"]
	});

	// Listen to realtime events
	counter.useEvent("newCount", (x: number) => setCount(x));

	const increment = async () => {
		// Call actions
		await counter.connection?.increment(1);
	};

	return (
		<div>
			<p>Count: {count}</p>
			<button onClick={increment}>Increment</button>
		</div>
	);
}
Counter.tsx

For detailed information about the React client API, see the React Client API Reference.

Setup Vite Configuration

Configure Vite for development:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  server: {
    port: 5173,
  },
})
vite.config.ts

Run Your Application

Start both the backend and frontend:

Terminal 1: Start the backend

Terminal 2: Start the frontend

npx vite
Command Line

Open http://localhost:5173 in your browser. Try opening multiple tabs to see realtime sync in action.

Deploy

By default, Rivet stores actor state on the local file system.

To scale Rivet in production, follow a guide to deploy to your hosting provider of choice:

Configuration Options

  • Server Setup: Different ways to run your server with serve(), handler(), or framework adapters.
  • Clients: Connect to actors from JavaScript, React, or other platforms.
  • Authentication: Secure actor connections with custom authentication logic.
  • CORS: Configure origin restrictions to secure your actors from unauthorized access.
  • Logging: Configure logging output for debugging and monitoring.
  • Runtime Modes: Serverless vs runners for different deployment scenarios.