Skip to main content
The runtime constructs and activates agent instances for you; these are the members involved in that lifecycle — construction, the inherited actor primitives, the inbound HTTP/WebSocket entry points, and the alarm slot the SDK reserves for the task scheduler.

constructor

new Agent<E, State>(ctx, env): Agent<E, State>
Called by the runtime on every activation — never construct an agent yourself. Re-arms the task scheduler to the earliest pending task (inside ctx.blockConcurrencyWhile), so persistent timers resume after a crash or restart. A subclass constructor must call super(ctx, env) first. Parameters Returns Agent<E, State> Overrides StatefulActor<E>.constructor

ctx

protected readonly ctx: ActorContext
The actor’s identity + storage + concurrency primitives. Inherited from StatefulActor.ctx

env

protected readonly env: E
The bindings environment for this actor’s worker scope. Inherited from StatefulActor.env

fetch()

fetch(_req): Promise<Response>
Optional HTTP-style entry. stub.fetch(req) from a caller routes here. Subclasses override; the base default returns 404. Request and Response are the standard Fetch API globals (Node 18+ exposes both natively). Parameters Returns Promise<Response> Inherited from StatefulActor.fetch

webSocket()

optional webSocket(ws, req): void | Promise<void>
Take ownership of an accepted WebSocket connection. ws is the live socket; req is the handshake request, whose headers carry whatever the front door attached (auth, identity). Declaring this method is what opts the actor into holding sockets; ctx.broadcast / ctx.count / ctx.sockets then operate on the sockets it holds. Parameters Returns void | Promise<void> Inherited from StatefulActor.webSocket

onConnect()

protected onConnect(_conn): Promise<void>
Hook: a client connection is established. Override to authorize, seed, or close the connection. The default implementation does nothing. Parameters Returns Promise<void>

alarm()

alarm(_info): Promise<void>
Reserved: the SDK claims the actor’s alarm slot to drive the task scheduler. When the alarm fires, this drains every due task, dispatches each to the method it names (or `onTask`), and re-arms the alarm to the next deadline. Do not override alarm() in an Agent subclass — doing so breaks queue / schedule / every. If you need timed work, schedule a task instead. Parameters Returns Promise<void> Overrides StatefulActor.alarm