Skip to content

Commit def9fd8

Browse files
committed
docs: clean up last few missing pieces of content (#3538)
1 parent 7d3ed98 commit def9fd8

35 files changed

+966
-650
lines changed

CLAUDE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,8 @@ Data structures often include:
179179
## Optimizations
180180

181181
- Never build a new reqwest client from scratch. Use `rivet_pools::reqwest::client().await?` to access an existing reqwest client instance.
182+
183+
## Documentation
184+
185+
- When talking about "Rivet Actors" make sure to capitalize "Rivet Actor" as a proper noun and lowercase "actor" as a generic noun
186+

rivetkit-typescript/packages/rivetkit/src/actor/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export class DatabaseNotEnabled extends ActorError {
297297
}
298298
}
299299

300-
export class RequestHandlerNotDfeined extends ActorError {
300+
export class RequestHandlerNotDefined extends ActorError {
301301
constructor() {
302302
super(
303303
"handler",

rivetkit-typescript/packages/rivetkit/src/actor/instance/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
674674
this.assertReady();
675675

676676
if (!this.#config.onRequest) {
677-
throw new errors.RequestHandlerNotDfeined();
677+
throw new errors.RequestHandlerNotDefined();
678678
}
679679

680680
try {

website/src/content/docs/actors/actions.mdx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,33 +163,38 @@ For example:
163163
import { actor, UserError } from "rivetkit";
164164

165165
const user = actor({
166-
state: { users: [] },
166+
state: { username: "" },
167167
actions: {
168-
registerUser: (c, username) => {
168+
updateUsername: (c, username: string) => {
169169
// Validate username
170170
if (username.length > 32) {
171171
// Throw a simple error with a message
172-
throw new UserError("Invalid username", {
173-
code: "invalid_username",
174-
meta: {
172+
throw new UserError("Username is too long", {
173+
code: "username_too_long",
174+
metadata: {
175175
maxLength: 32
176176
}
177177
});
178178
}
179-
180-
// Rest of the user registration logic...
179+
180+
// Update username
181+
c.state.username = username;
181182
}
182183
}
183184
});
184185
```
185186

186187
```typescript {{"title":"client.ts"}}
188+
import { ActorError } from "rivetkit/client";
189+
187190
try {
188-
await userActor.registerUser("extremely_long_username_that_exceeds_limit");
191+
await userActor.updateUsername("extremely_long_username_that_exceeds_limit");
189192
} catch (error) {
190-
console.log("Message", error.message); // "Invalid username"
191-
console.log("Code", error.code); // "invalid_username"
192-
console.log("Metadata", error.metadata); // { maxLength; 32 }
193+
if (error instanceof ActorError) {
194+
console.log("Message", error.message); // "Username is too long"
195+
console.log("Code", error.code); // "username_too_long"
196+
console.log("Metadata", error.metadata); // { maxLength: 32 }
197+
}
193198
}
194199
```
195200

@@ -267,3 +272,10 @@ function incrementCount(c: ActionContextOf<typeof counter>) {
267272

268273
See [helper types](/docs/actors/helper-types) for more details on using `ActionContextOf` and other utility types.
269274

275+
## API Reference
276+
277+
- [`Actions`](/typedoc/interfaces/rivetkit.mod.Actions.html) - Interface for defining actions
278+
- [`ActionContext`](/typedoc/interfaces/rivetkit.mod.ActionContext.html) - Context available in action handlers
279+
- [`ActorDefinition`](/typedoc/interfaces/rivetkit.mod.ActorDefinition.html) - Interface for defining actors with actions
280+
- [`ActorHandle`](/typedoc/types/rivetkit.client_mod.ActorHandle.html) - Handle for calling actions from client
281+
- [`ActorActionFunction`](/typedoc/types/rivetkit.client_mod.ActorActionFunction.html) - Type for action functions

website/src/content/docs/actors/authentication.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,9 @@ const cachedAuthActor = actor({
480480
});
481481
```
482482

483+
## API Reference
484+
485+
- [`AuthIntent`](/typedoc/types/rivetkit.mod.AuthIntent.html) - Authentication intent type
486+
- [`OnBeforeConnectContext`](/typedoc/interfaces/rivetkit.mod.OnBeforeConnectContext.html) - Context for auth checks
487+
- [`OnConnectContext`](/typedoc/interfaces/rivetkit.mod.OnConnectContext.html) - Context after connection
488+

website/src/content/docs/actors/clients.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,11 @@ const actorId = await handle.resolve();
464464
console.log(actorId); // "lrysjam017rhxofttna2x5nzjml610"
465465
```
466466

467+
## API Reference
468+
469+
- [`createClient`](/typedoc/functions/rivetkit.client_mod.createClient.html) - Function to create clients
470+
- [`Client`](/typedoc/types/rivetkit.mod.Client.html) - Client type
471+
- [`ActorHandle`](/typedoc/types/rivetkit.client_mod.ActorHandle.html) - Handle for interacting with actors
472+
- [`ActorConn`](/typedoc/types/rivetkit.client_mod.ActorConn.html) - Connection to actors
473+
- [`ClientRaw`](/typedoc/interfaces/rivetkit.client_mod.ClientRaw.html) - Raw client interface
474+

website/src/content/docs/actors/communicating-between-actors.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,9 @@ const results = await Promise.all(
166166
);
167167
```
168168

169+
## API Reference
170+
171+
- [`ActorHandle`](/typedoc/types/rivetkit.client_mod.ActorHandle.html) - Handle for calling other actors
172+
- [`Client`](/typedoc/types/rivetkit.mod.Client.html) - Client type for actor communication
173+
- [`ActorAccessor`](/typedoc/interfaces/rivetkit.client_mod.ActorAccessor.html) - Accessor for getting actor handles
174+

website/src/content/docs/actors/connections.mdx

Lines changed: 139 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { createClient } from "rivetkit/client";
1717
import type { registry } from "./src/index";
1818

1919
const client = createClient<typeof registry>("http://localhost:8080");
20-
const gameRoom = await client.gameRoom.get({
20+
const gameRoom = client.gameRoom.getOrCreate("room-123", {
2121
params: { authToken: "supersekure" }
2222
});
2323
```
@@ -38,7 +38,7 @@ const gameRoom = actor({
3838
state: {},
3939

4040
// Handle connection setup
41-
createConnState: (c, opts, params: ConnParams): ConnState => {
41+
createConnState: (c, params: ConnParams): ConnState => {
4242
// Validate authentication token
4343
const authToken = params.authToken;
4444

@@ -49,7 +49,7 @@ const gameRoom = actor({
4949
// Create connection state
5050
return { userId: getUserIdFromToken(authToken), role: "player" };
5151
},
52-
52+
5353
actions: {
5454
// ...
5555
}
@@ -133,16 +133,136 @@ There are two ways to define an actor's connection state:
133133
</Tab>
134134
</Tabs>
135135

136-
## Connection Lifecycle Hooks
136+
## Connection Lifecycle
137+
138+
Each client connection goes through a series of lifecycle hooks that allow you to validate, initialize, and clean up connection-specific resources.
139+
140+
**On Connect** (per client)
141+
142+
- `onBeforeConnect`
143+
- `createConnState`
144+
- `onConnect`
145+
146+
**On Disconnect** (per client)
147+
148+
- `onDisconnect`
149+
150+
### `createConnState` and `connState`
151+
152+
[API Reference](/typedoc/interfaces/rivetkit.mod.CreateConnStateContext.html)
153+
154+
There are two ways to define the initial state for connections:
155+
1. `connState`: Define a constant object that will be used as the initial state for all connections
156+
2. `createConnState`: A function that dynamically creates initial connection state based on connection parameters. Can be async.
157+
158+
### `onBeforeConnect`
159+
160+
[API Reference](/typedoc/interfaces/rivetkit.mod.OnBeforeConnectContext.html)
161+
162+
The `onBeforeConnect` hook is called whenever a new client connects to the actor. Can be async. Clients can pass parameters when connecting, accessible via `params`. This hook is used for connection validation and can throw errors to reject connections.
163+
164+
The `onBeforeConnect` hook does NOT return connection state - it's used solely for validation.
165+
166+
```typescript
167+
import { actor } from "rivetkit";
168+
169+
const chatRoom = actor({
170+
state: { messages: [] },
171+
172+
// Method 1: Use a static default connection state
173+
connState: {
174+
role: "guest",
175+
joinTime: 0,
176+
},
177+
178+
// Method 2: Dynamically create connection state
179+
createConnState: (c, params: { userId?: string, role?: string }) => {
180+
return {
181+
userId: params.userId || "anonymous",
182+
role: params.role || "guest",
183+
joinTime: Date.now()
184+
};
185+
},
186+
187+
// Validate connections before accepting them
188+
onBeforeConnect: (c, params: { authToken?: string }) => {
189+
// Validate authentication
190+
const authToken = params.authToken;
191+
if (!authToken || !validateToken(authToken)) {
192+
throw new Error("Invalid authentication");
193+
}
194+
195+
// Authentication is valid, connection will proceed
196+
// The actual connection state will come from connState or createConnState
197+
},
198+
199+
actions: { /* ... */ }
200+
});
201+
```
137202

138-
The connection lifecycle has several hooks:
203+
Connections cannot interact with the actor until this method completes successfully. Throwing an error will abort the connection. This can be used for authentication, see [Authentication](/docs/actors/authentication) for details.
139204

140-
- `onBeforeConnect`: Called before a client connects, returns the connection state
141-
- `onConnect`: Called when a client successfully connects
142-
- `createState`: Called when creating a connection state
143-
- `onDisconnect`: Called when a client disconnects
205+
### `onConnect`
144206

145-
See the documentation on [Actor Lifecycle](/docs/actors/lifecycle) for more details.
207+
[API Reference](/typedoc/interfaces/rivetkit.mod.OnConnectContext.html)
208+
209+
Executed after the client has successfully connected. Can be async. Receives the connection object as a second parameter.
210+
211+
```typescript
212+
import { actor } from "rivetkit";
213+
214+
const chatRoom = actor({
215+
state: { users: {}, messages: [] },
216+
217+
onConnect: (c, conn) => {
218+
// Add user to the room's user list using connection state
219+
const userId = conn.state.userId;
220+
c.state.users[userId] = {
221+
online: true,
222+
lastSeen: Date.now()
223+
};
224+
225+
// Broadcast that a user joined
226+
c.broadcast("userJoined", { userId, timestamp: Date.now() });
227+
228+
console.log(`User ${userId} connected`);
229+
},
230+
231+
actions: { /* ... */ }
232+
});
233+
```
234+
235+
Messages will not be processed for this actor until this hook succeeds. Errors thrown from this hook will cause the client to disconnect.
236+
237+
### `onDisconnect`
238+
239+
[API Reference](/typedoc/interfaces/rivetkit.mod.ActorDefinition.html)
240+
241+
Called when a client disconnects from the actor. Can be async. Receives the connection object as a second parameter. Use this to clean up any connection-specific resources.
242+
243+
```typescript
244+
import { actor } from "rivetkit";
245+
246+
const chatRoom = actor({
247+
state: { users: {}, messages: [] },
248+
249+
onDisconnect: (c, conn) => {
250+
// Update user status when they disconnect
251+
const userId = conn.state.userId;
252+
if (c.state.users[userId]) {
253+
c.state.users[userId].online = false;
254+
c.state.users[userId].lastSeen = Date.now();
255+
}
256+
257+
// Broadcast that a user left
258+
c.broadcast("userLeft", { userId, timestamp: Date.now() });
259+
260+
console.log(`User ${userId} disconnected`);
261+
},
262+
263+
actions: { /* ... */ }
264+
});
265+
```
146266

147267
## Connection List
148268

@@ -210,3 +330,12 @@ await c.conn.disconnect("Too many requests");
210330
```
211331

212332
This ensures the underlying network connections close cleanly before continuing.
333+
334+
## API Reference
335+
336+
- [`Conn`](/typedoc/interfaces/rivetkit.mod.Conn.html) - Connection interface
337+
- [`ConnInitContext`](/typedoc/interfaces/rivetkit.mod.ConnInitContext.html) - Connection initialization context
338+
- [`CreateConnStateContext`](/typedoc/interfaces/rivetkit.mod.CreateConnStateContext.html) - Context for creating connection state
339+
- [`OnBeforeConnectContext`](/typedoc/interfaces/rivetkit.mod.OnBeforeConnectContext.html) - Pre-connection lifecycle hook context
340+
- [`OnConnectContext`](/typedoc/interfaces/rivetkit.mod.OnConnectContext.html) - Post-connection lifecycle hook context
341+
- [`ActorConn`](/typedoc/types/rivetkit.client_mod.ActorConn.html) - Typed connection from client side

0 commit comments

Comments
 (0)