this.ctx (an ActorContext) holds the instance’s identity, per-key storage, single alarm, one-shot init, and the instance’s live WebSocket connections.
ctx.id
The customer-supplied name for this actor — opaque string. You chose it via env.<BINDING>.idFromName(name). Common patterns: caller E.164, CRM user id, email, or a composite key.
ctx.blockConcurrencyWhile(fn)
One-shot init primitive. Use it inside a subclass constructor to gate all other calls until init finishes. 30s budget — a callback that exceeds it fails init (BlockConcurrencyTimeoutError), and the activation is torn down and retried on the next call.
ctx.setAlarm(when)
Top-level alias for ctx.storage.setAlarm(when). when is ms since epoch. See Alarms.
ctx.count()
The number of WebSocket connections open on this instance right now. Synchronous — no await. Scoped to the instance: sockets on other names of the same actor class are never counted, and a socket never carries over between names.
Callable from any handler — a socket message handler, an RPC method, or the alarm handler.
ctx.broadcast(data)
Send one frame to every WebSocket open on this instance; returns the number of sockets the frame was sent to. A string is sent as a text frame; an ArrayBuffer or ArrayBufferView as a binary frame. Scoped to the instance, like count() — a broadcast never reaches another name’s sockets. Like ws.send(), the write happens immediately; it is not held until the turn’s storage writes commit.
Callable from any handler. Calling it from alarm() is the server-initiated push pattern — the actor sends with no inbound frame prompting it:
Related
- Actor Storage —
ctx.storage - Base Class — where
this.ctxcomes from - Alarms — the alarm contract
- WebSockets — the connection contract behind
count()andbroadcast()