Communication

Connections

Connections represent client connections to your actor. They provide a way to handle client authentication, manage connection-specific data, and control the connection lifecycle.

For documentation on connecting to actors from clients, see the Clients documentation.

Parameters

When clients connect to an actor, they can pass connection parameters that are handled during the connection process.

For example:

import { createClient } from "rivetkit/client";
import type { registry } from "./src/index";

const client = createClient<typeof registry>("http://localhost:8080");
const gameRoom = client.gameRoom.getOrCreate("room-123", {
  params: { authToken: "supersekure" }
});

Connection State

There are two ways to define an actor's connection state:

Define connection state as a constant value:

import { actor } from "rivetkit";

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: {
    // ...
  }
});
TypeScript

This value will be cloned for every new connection using structuredClone.

Connection Lifecycle

Each client connection goes through a series of lifecycle hooks that allow you to validate, initialize, and clean up connection-specific resources.

On Connect (per client)

  • onBeforeConnect
  • createConnState
  • onConnect

On Disconnect (per client)

  • onDisconnect

createConnState and connState

API Reference

There are two ways to define the initial state for connections:

  1. connState: Define a constant object that will be used as the initial state for all connections
  2. createConnState: A function that dynamically creates initial connection state based on connection parameters. Can be async.

onBeforeConnect

API Reference

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.

The onBeforeConnect hook does NOT return connection state - it's used solely for validation.

import { actor } from "rivetkit";

const chatRoom = actor({
  state: { messages: [] },

  // Method 1: Use a static default connection state
  connState: {
    role: "guest",
    joinTime: 0,
  },

  // Method 2: Dynamically create connection state
  createConnState: (c, params: { userId?: string, role?: string }) => {
    return {
      userId: params.userId || "anonymous",
      role: params.role || "guest",
      joinTime: Date.now()
    };
  },

  // Validate connections before accepting them
  onBeforeConnect: (c, params: { authToken?: string }) => {
    // Validate authentication
    const authToken = params.authToken;
    if (!authToken || !validateToken(authToken)) {
      throw new Error("Invalid authentication");
    }

    // Authentication is valid, connection will proceed
    // The actual connection state will come from connState or createConnState
  },

  actions: { /* ... */ }
});
TypeScript

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 for details.

onConnect

API Reference

Executed after the client has successfully connected. Can be async. Receives the connection object as a second parameter.

import { actor } from "rivetkit";

const chatRoom = actor({
  state: { users: {}, messages: [] },

  onConnect: (c, conn) => {
    // Add user to the room's user list using connection state
    const userId = conn.state.userId;
    c.state.users[userId] = {
      online: true,
      lastSeen: Date.now()
    };

    // Broadcast that a user joined
    c.broadcast("userJoined", { userId, timestamp: Date.now() });

    console.log(`User ${userId} connected`);
  },

  actions: { /* ... */ }
});
TypeScript

Messages will not be processed for this actor until this hook succeeds. Errors thrown from this hook will cause the client to disconnect.

onDisconnect

API Reference

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.

import { actor } from "rivetkit";

const chatRoom = actor({
  state: { users: {}, messages: [] },

  onDisconnect: (c, conn) => {
    // Update user status when they disconnect
    const userId = conn.state.userId;
    if (c.state.users[userId]) {
      c.state.users[userId].online = false;
      c.state.users[userId].lastSeen = Date.now();
    }

    // Broadcast that a user left
    c.broadcast("userLeft", { userId, timestamp: Date.now() });

    console.log(`User ${userId} disconnected`);
  },

  actions: { /* ... */ }
});
TypeScript

Connection List

All active connections can be accessed through the context object's conns property. This is an array of all current connections.

This is frequently used with conn.send(name, event) to send messages directly to clients. To send an event to all connections at once, use c.broadcast() instead. See Events for more details on broadcasting.

For example:

import { actor } from "rivetkit";

const chatRoom = actor({
  state: { users: {} },
  
  actions: {
    sendDirectMessage: (c, recipientId, message) => {
      // Find the recipient's connection
      const recipientConn = c.conns.find(conn => conn.state.userId === recipientId);
      
      if (recipientConn) {
        // Send a private message to just that client
        recipientConn.send("directMessage", {
          from: c.conn.state.userId,
          message: message
        });
      }
    }
  }
});
TypeScript

conn.send() has no effect on low-level WebSocket connections. For low-level WebSockets, use the WebSocket API directly (e.g., websocket.send()).

Disconnecting clients

Connections can be disconnected from within an action:

import { actor } from "rivetkit";

const secureRoom = actor({
  state: {},
  
  actions: {
    kickUser: (c, targetUserId, reason) => {
      // Find the connection to kick
      const targetConn = c.conns.find(conn => conn.state.userId === targetUserId);
      
      if (targetConn) {
        // Disconnect with a reason
        targetConn.disconnect(reason || "Kicked by admin");
      }
    }
  }
});
TypeScript

If you need to wait for the disconnection to complete, you can use await:

await c.conn.disconnect("Too many requests");
TypeScript

This ensures the underlying network connections close cleanly before continuing.

API Reference

Suggest changes to this page