Sign In
Frontend & Clients


title: Node.js & Bun icon: node-js

The Rivet JavaScript client allows you to connect to and interact with actors from browser and Node.js applications.


Quickstart

Create a new Node.js project

Create a new Node.js project with TypeScript support:

mkdir my-app
cd my-app
npm init -y
npm pkg set type=module
mkdir my-app
cd my-app
pnpm init
pnpm pkg set type=module
mkdir my-app
cd my-app
yarn init -y
yarn pkg set type=module
mkdir my-app
cd my-app
bun init -y

Install Rivet packages

Install the Rivet client and Node.js platform packages:

npm install @rivetkit/actor
pnpm add @rivetkit/actor
yarn add @rivetkit/actor
bun add @rivetkit/actor

Define your actor schema and implementation. Create a new file for your actor:

// actors/counter.ts
import { Actor } from '@rivetkit/actor';

export interface CounterState {
  count: number;
}

export class CounterActor extends Actor<CounterState> {
  getInitialState() {
    return { count: 0 };
  }

  increment() {
    this.setState({ count: this.state.count + 1 });
  }

  decrement() {
    this.setState({ count: this.state.count - 1 });
  }
}

Create your client

Create a file src/client.ts in your project to connect to your actor:

TypeScript
import { createClient } from "@rivetkit/actor/client";
import type { App } from "../actors/app";

async function main() {
  // Replace with your endpoint URL after deployment
  const client = createClient<App>("http://localhost:8080");

  // Get or create a actor instance
  const counter = await client.counter.get();

  // Subscribe to events
  counter.on("newCount", (count: number) => console.log("Event:", count));

  // Call an action
  const out = await counter.increment(5);
  console.log("Action:", out);

  // Clean up when done
  await counter.dispose();
}

main().catch(console.error);

Start the Rivet development studio to test your actors locally:

npx rivetkit dev

This will start the local development server and open the Rivet Studio in your browser where you can interact with your actors.

Run your client

In a separate terminal, run your client code:

npx tsx src/client.ts
pnpm exec tsx src/client.ts
yarn tsx src/client.ts
bun run src/client.ts

You should see output like:

C++
Event: 5 Action: 5

Run it again to see the state update.

Deploy your actors to production:

npx rivetkit deploy

This will build and deploy your actors to the Rivet cloud platform. Make sure you're logged in with rivetkit login.


Next Steps

For more information on communicating with actors, including event handling and RPC calls, see Communicating with Actors.

Suggest changes to this page