live-cursors

โดย rivet-dev

เคอร์เซอร์สดและการแสดงสถานะผู้เล่นหลายคนด้วย Rivet Actors: สถานะเคอร์เซอร์ต่อการเชื่อมต่อ อัปเดตแบบเรียลไทม์ผ่านอีเวนต์หรือ WebSockets ดิบ และการจำกัดความถี่

npx skills add https://github.com/rivet-dev/skills --skill live-cursors

Live Cursors and Presence

IMPORTANT: Before doing anything, you MUST read BASE_SKILL.md in this skill's directory. It contains essential guidance on debugging, error handling, state management, deployment, and project setup. Those rules and patterns apply to all RivetKit work. Everything below assumes you have already read and understood it.

Working Examples

If you need a reference implementation, read the raw working example code in these templates:

Patterns for building live cursors, multiplayer presence, and realtime cursor sharing with RivetKit. One room actor fans cursor positions out to every connected client, keyed per room with actor keys.

Starter Code

Start with one of the two working variants on GitHub. Both implement the same collaborative cursor canvas with persistent text labels; they differ only in transport.

VariantStarter CodeTransportPresence Storage
cursorsGitHubTyped actions and events over the RivetKit connectionconnState per connection
cursors-raw-websocketGitHubRaw onWebSocket handler with a custom JSON message protocolSocket map in createVars

Use cursors by default: typed actions, typed events, and automatic connection tracking cover most apps with less code. Use cursors-raw-websocket when you need full control of the wire format, for example a custom JSON or binary protocol, or clients that do not use the RivetKit client library.

Connection State vs Persistent State

Presence is ephemeral by definition. A cursor position is only meaningful while its connection is alive, so it belongs in per-connection storage, not in persistent actor state. Persistent state is reserved for data that must survive disconnects and actor restarts.

DataWhere It LivesWhy
Cursor positionconnState (cursors) or the createVars socket map (cursors-raw-websocket)Scoped to one connection and discarded with it. Stale presence cannot accumulate in storage.
Text labels (textLabels)Persistent actor state in both variantsCanvas content must survive disconnects and actor restarts.

In the cursors variant, updateCursor writes c.conn.state.cursor and getRoomState rebuilds the presence snapshot by iterating c.conns.values(), so the cursor map is always derived from live connections rather than stored. See Connections for connState and State for persistence semantics.

Presence Lifecycle

  • Join: The cursors-raw-websocket variant pushes an init message with the current { cursors, textLabels } snapshot as soon as a socket connects. The cursors variant has no explicit join broadcast; the client calls the getRoomState action once after connecting to seed its local maps, and peers first see a new user on that user's first cursorMoved broadcast.
  • Move: Every updateCursor call writes the connection's presence entry, then broadcasts cursorMoved to all connections, including the sender.
  • Leave: The cursors variant handles leave in onDisconnect, broadcasting cursorRemoved with the connection's last cursor. The raw variant does the same from the socket close listener, then deletes the session from the vars.websockets map. Clients delete that user from their local cursor map, so stale cursors disappear the moment a tab closes.

See Lifecycle for onDisconnect and createVars.

Update Throttling

Neither example throttles. Both frontends send a cursor update on every raw mousemove event with no debounce or interval cap. That is fine for a demo, but a fast mouse on a high-refresh display can emit hundreds of events per second per user. The patterns below are recommended production hardening on top of the starter code, not something the examples implement.

LayerPatternGuidance
Client (smoothness)Throttle to 20-30HzSample the latest pointer position every 33-50ms and send only that. Drop intermediate moves, but always flush the final position so cursors settle at the true location. Interpolate between received positions on the rendering side.
Server (enforcement)Per-connection rate limitTrack the last accepted update timestamp per connection and drop or coalesce updates arriving faster than your cap. Client throttles are cooperative; the actor is the enforcement boundary.

Actors

  • Key: cursorRoom[roomId] (the frontend defaults roomId to "general")

  • Responsibility: Holds per-connection cursor presence in connState, persists shared text labels in actor state, and broadcasts cursor and text updates to all connections.

  • Actions

    • updateCursor
    • updateText
    • removeText
    • getRoomState
  • Events

    • cursorMoved
    • cursorRemoved
    • textUpdated
    • textRemoved
  • Queues

    • None
  • State

    • JSON
    • textLabels (persistent)
    • connState.cursor per connection (ephemeral)
  • Key: cursorRoom[roomId] (resolved via client.cursorRoom.getOrCreate(roomId))

  • Responsibility: Exposes a raw WebSocket endpoint, tracks live sockets and their cursors in a createVars map keyed by a sessionId query parameter, persists text labels, and manually fans JSON frames out to every socket.

  • Actions

    • getOrCreate (stub returning { status: "ok" }; the frontend resolves the actor ID with the client handle's getOrCreate(roomId).resolve(), which creates the actor without dispatching this action)
    • getRoomState
  • Queues

    • None
  • State

    • JSON
    • textLabels (persistent)
    • vars.websockets map of sessionId to socket and cursor (in-memory, lost on restart)

The raw variant defines no RivetKit events. Its message names are type fields on raw JSON frames:

DirectionMessage typePayload
Client to serverupdateCursor{ userId, x, y }
Client to serverupdateText{ id, userId, text, x, y }
Client to serverremoveText{ id }
Server to clientinit{ cursors, textLabels } snapshot on connect
Server to clientcursorMoved, textUpdated, textRemoved, cursorRemovedThe corresponding cursor, label, or ID payload

Lifecycle

cursors (Actions + Events)

sequenceDiagram
	participant A as Client A
	participant R as cursorRoom
	participant B as Other Clients

	A->>R: connect via useActor (cursorRoom[roomId])
	A->>R: getRoomState()
	R-->>A: {cursors, textLabels}
	loop every mouse move
		A->>R: updateCursor(userId, x, y)
		Note over R: write c.conn.state.cursor
		R-->>B: cursorMoved (broadcast)
	end
	A->>R: updateText(id, userId, text, x, y)
	Note over R: upsert persistent state.textLabels
	R-->>B: textUpdated (broadcast)
	Note over A: tab closes
	Note over R: onDisconnect reads conn.state.cursor
	R-->>B: cursorRemoved (broadcast)

cursors-raw-websocket

sequenceDiagram
	participant A as Client A
	participant R as cursorRoom
	participant B as Other Clients

	A->>R: getOrCreate(roomId).resolve()
	R-->>A: actorId
	A->>R: open WebSocket /gateway/{actorId}/websocket?sessionId=...
	Note over R: close 1008 if sessionId is missing
	Note over R: store socket in vars.websockets
	R-->>A: init {cursors, textLabels}
	loop every mouse move
		A->>R: {type: "updateCursor"} frame
		Note over R: update session cursor in vars
		R-->>B: cursorMoved frame
	end
	Note over A: socket closes
	R-->>B: cursorRemoved frame
	Note over R: delete session from vars.websockets

Security Checklist

Both examples ship without authentication so the presence pattern stays readable. Everything below is recommended hardening for production, not behavior the examples implement.

  • Identity: Bind presence identity to the connection (c.conn.id in the actions variant, a server-generated session ID in the raw variant). Never trust a client-supplied userId; in the examples it is a random client-generated string, so any client can impersonate or remove any cursor.
  • Authorization: Authorize label mutations by owner. In the examples, updateText accepts arbitrary id and userId arguments and removeText accepts an arbitrary id, so any client can edit or delete any label.
  • Input validation: Clamp x and y to canvas bounds, cap text label length, and cap the total textLabels count so persistent state cannot grow unbounded.
  • Rate limiting: Enforce a per-connection cap on updateCursor (for example 30Hz) and on label writes, as described in Update Throttling.
  • Protocol strictness (raw variant): Validate message shape before use and close the socket on malformed JSON instead of logging and continuing. Reject duplicate sessionId values rather than silently overwriting another session's socket entry.

Reference Map

Actors

Cli

Clients

Cookbook

Deploy

General

Self Hosting

Skills เพิ่มเติมจาก rivet-dev

ai-agent
rivet-dev
สร้างแบ็กเอนด์ AI agent ที่มีหน่วยความจำถาวร: หนึ่ง Rivet Actor ต่อการสนทนา การจัดการข้อความในคิว และการสตรีมการตอบสนองของ LLM เป็นเหตุการณ์แบบเรียลไทม์
official
ai-agent-workspace
rivet-dev
มอบคอมพิวเตอร์ส่วนตัวให้กับเอเจนต์ AI ทุกตัว: พื้นที่ทำงานถาวรที่มีระบบไฟล์ โปรเซส เชลล์ เครือข่าย และเซสชันของเอเจนต์บนระบบในกระบวนการที่มีน้ำหนักเบา…
official
chat-room
rivet-dev
สร้างแบ็กเอนด์ห้องแชทแบบเรียลไทม์ด้วย Rivet Actors: หนึ่ง actor ต่อหนึ่งห้อง, ประวัติข้อความที่ใช้ SQLite, และการกระจายสัญญาณ WebSocket ไปยังทุกไคลเอนต์ที่เชื่อมต่อ
official
collaborative-text-editor
rivet-dev
สร้างแบ็กเอนด์ของโปรแกรมแก้ไขข้อความแบบทำงานร่วมกันด้วย Yjs CRDTs และ Rivet Actors: ตัวแทนต่อเอกสารจะถ่ายทอดการอัปเดตการซิงค์และการรับรู้ และบันทึกสแนปช็อต
official
cron-jobs
rivet-dev
Durable cron jobs with Rivet Actors: schedule.after and schedule.at timers survive restarts and crashes, plus re-arming recurring jobs and idempotent handlers.
official
per-tenant-database
rivet-dev
การแยกข้อมูลแบบหลายผู้เช่าโดยใช้ Rivet Actor หนึ่งตัวต่อผู้เช่า: คีย์ของแอคเตอร์คือรหัสผู้เช่า ดังนั้นผู้เช่าแต่ละรายจะได้รับชุดข้อมูลและการย้ายข้อมูลที่แยกออกจากกัน
official
rivetkit-client-javascript
rivet-dev
ไคลเอนต์ JavaScript สำหรับเชื่อมต่อกับ Rivet Actors ด้วยการเชื่อมต่อแบบไร้สถานะหรือมีสถานะ รองรับสภาพแวดล้อมเบราว์เซอร์ Node.js และ Bun พร้อมการตรวจจับเอนด์พอยต์อัตโนมัติผ่านตัวแปรสภาพแวดล้อมหรือการกำหนดค่าแบบชัดเจน มีโหมดการโต้ตอบสองแบบ: การเรียกแอคชันแบบไร้สถานะสำหรับคำขออิสระ และการเชื่อมต่อแบบมีสถานะพร้อมการสมัครรับเหตุการณ์แบบเรียลไทม์ รวมถึงการเข้าถึง HTTP และ WebSocket ระดับต่ำสำหรับแอคเตอร์ที่ใช้ตัวจัดการ onRequest หรือ onWebSocket ให้การเข้าถึงแบบอาร์เรย์ผสม...
official
rivetkit-client-react
rivet-dev
React client สำหรับเชื่อมต่อกับ Rivet Actors โดยใช้ hooks และการจัดการสถานะแบบเรียลไทม์ สร้าง typed hooks ด้วย createRivetKit() และเชื่อมต่อกับอินสแตนซ์ของ actor โดยใช้ useActor() พร้อม keys และพารามิเตอร์เสริม สมัครรับอีเวนต์ของ actor ด้วย useEvent() และตรวจสอบวงจรการเชื่อมต่อผ่านสถานะ connStatus และ error states ใช้ createClient() สำหรับการเรียกแบบ stateless แบบครั้งเดียว, วิธีการค้นหา actor (get, getOrCreate, create, getForId), และการเข้าถึง HTTP/WebSocket ระดับต่ำ รองรับ compound array keys...
official