Skip to content

Latest commit

 

History

History
143 lines (108 loc) · 2.92 KB

File metadata and controls

143 lines (108 loc) · 2.92 KB
title Node.js & Bun
icon node-js

import MvpWarning from "/snippets/mvp-warning.mdx"; import StepDefineActor from "/snippets/step-define-actor.mdx"; import StepRunStudio from "/snippets/step-run-studio.mdx"; import StepDeploy from "/snippets/step-deploy.mdx"; import SetupNextSteps from "/snippets/setup-next-steps.mdx";

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

Quickstart

Create a new Node.js project with TypeScript support:
<CodeGroup>
  ```sh npm
  mkdir my-app
  cd my-app
  npm init -y
  npm pkg set type=module
  ```
  
  ```sh pnpm
  mkdir my-app
  cd my-app
  pnpm init
  pnpm pkg set type=module
  ```
  
  ```sh yarn
  mkdir my-app
  cd my-app
  yarn init -y
  yarn pkg set type=module
  ```
  
  ```sh bun
  mkdir my-app
  cd my-app
  bun init -y
  ```
</CodeGroup>
Install the RivetKit client and Node.js platform packages:
<CodeGroup>
  ```sh npm
  npm install rivetkit
  ```
  
  ```sh pnpm
  pnpm add rivetkit
  ```
  
  ```sh yarn
  yarn add rivetkit
  ```
  
  ```sh bun
  bun add rivetkit
  ```
</CodeGroup>
Create a file `src/client.ts` in your project to connect to your actor:
```typescript src/client.ts
import { createClient } from "rivetkit/client";
import type { App } from "../actors/app";

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

  // 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);
```
In a separate terminal, run your client code:
<CodeGroup>
  ```sh npm
  npx tsx src/client.ts
  ```
  
  ```sh pnpm
  pnpm exec tsx src/client.ts
  ```
  
  ```sh yarn
  yarn tsx src/client.ts
  ```
  
  ```sh bun
  bun run src/client.ts
  ```
</CodeGroup>

You should see output like:
```
Event: 5
Action: 5
```

Run it again to see the state update.

Next Steps

See the Interacting with Actors documentation for information on how to use the client.