Skip to main content
This release is 1 version behind 24.6.2 — the latest version of @rivet-gg/actor. Jump to latest

🔩 Rivet Actors have built-in RPC, state, and events — the easiest way to build modern applications.

This package works with Deno
This package works with Deno
JSR Score
88%
Published
a month ago (24.6.1-rc.2)
class Connection

Represents a client connection to an actor.

Manages connection-specific data and controls the connection lifecycle.

Constructors

new
Connection(
id: ConnectionId,
websocket: WSContext<WebSocket>,
protocolFormat: ProtocolFormat,
state: ExtractActorConnState<A> | undefined,
stateEnabled: boolean,
)

Initializes a new instance of the Connection class.

This should only be constructed by Actor.

Type Parameters

A extends AnyActor

Properties

Protocol format used for message serialization and deserialization.

WebSocket context for managing the connection.

readonly
id: ConnectionId

Unique identifier for the connection.

writeonly
state: ExtractActorConnState<A>

Sets the state of the connection.

Throws an error if the state is not enabled.

readonly
state: ExtractActorConnState<A>

Gets the current state of the connection.

Throws an error if the state is not enabled.

Methods

_parse(data: IncomingWebSocketMessage): Promise<unknown>

Parses incoming WebSocket messages based on the protocol format.

_sendWebSocketMessage(message: OutgoingWebSocketMessage): void

Sends a WebSocket message to the client.

_serialize(value: unknown): OutgoingWebSocketMessage

Serializes a value into a WebSocket message based on the protocol format.

disconnect(reason?: string): void

Disconnects the client with an optional reason.

send(
eventName: string,
...args: unknown[],
): void

Sends an event with arguments to the client.

See

Add Package

deno add jsr:@rivet-gg/actor

Import symbol

import { Connection } from "@rivet-gg/actor";

---- OR ----

Import directly with a jsr specifier

import { Connection } from "jsr:@rivet-gg/actor";