Connections represent client connections to your actor. They provide a way to handle client authentication, manage connection-specific data, and control the connection lifecycle.
There are two ways to define a actor’s connection state:
Define connection state as a constant value:
Copy
import { actor } from "@rivetkit/actor";const chatRoom = actor({ state: { messages: [] }, // Define default connection state as a constant connState: { role: "guest", joinedAt: 0 }, onConnect: (c) => { // Update join timestamp when a client connects c.conn.state.joinedAt = Date.now(); }, actions: { // ... }});
Define connection state as a constant value:
Copy
import { actor } from "@rivetkit/actor";const chatRoom = actor({ state: { messages: [] }, // Define default connection state as a constant connState: { role: "guest", joinedAt: 0 }, onConnect: (c) => { // Update join timestamp when a client connects c.conn.state.joinedAt = Date.now(); }, actions: { // ... }});
Create connection state dynamically with a function. The data returned is used as the initial state of the connection. The connection state can be accessed through conn.state.