PotaLab
← All examples
SnippetRealtimeBroadcastPresenceNext.js

Live cursors and presence

Cursors are Broadcast frames, throttled to 12 per second because a connection may send 20 frames per second in total. Online and typing are Presence: one entry per user, removed by the server when the socket closes, and a late joiner receives the current state at once. A single channel_access policy decides who may join, send and be listed.

Tested with 3 simulated clients plus a late joiner: every client saw all the others online, cursors reached everyone but the sender, 200 moves in about one second produced at most 12 frames per second, typing cleared itself, and a user who left disappeared from the others. Broadcast is not stored: use a table with postgres_changes for anything that must not be lost. One project, one environment: a correctness check, not a performance guarantee.

localhost:3000/canvas

Shared canvas

3 online

Ana Bo
  • You
  • Ana
  • Bo

Cursors are Broadcast frames (throttled to 12 per second); online and typing are Presence. No table. Ana and Bo are simulated here.

Preview · sample data
'use client'
import { useEffect, useRef, useState } from 'react'
import { createBrowserClient } from '@potalab/base/next'
import { joinRoom, type Me, type Peer } from '@/lib/cursor-room'
export function Cursors({ roomId, user }: { roomId: string; user: Me }) {
const canvas = useRef<HTMLDivElement>(null)
const room = useRef<ReturnType<typeof joinRoom> | null>(null)
const [peers, setPeers] = useState<Peer[]>([])
const [pos, setPos] = useState<Record<string, { x: number; y: number }>>({})
useEffect(() => {
const base = createBrowserClient({
url: process.env.NEXT_PUBLIC_POTALAB_BASE_URL!,
key: process.env.NEXT_PUBLIC_POTALAB_BASE_KEY!,
})
const r = joinRoom(base, roomId, user, {
onCursor: (id, x, y) => setPos((p) => ({ ...p, [id]: { x, y } })),
onPeers: (list) => {
setPeers(list)
// someone who left takes their cursor with them
setPos((p) => Object.fromEntries(Object.entries(p).filter(([id]) => list.some((x) => x.id === id))))
},
})
room.current = r
return () => void r.leave()
}, [roomId, user.id])
const others = peers.filter((p) => p.id !== user.id)
const typing = others.filter((p) => p.typing).map((p) => p.name)
return (
<div>
<p>
{peers.length} online{typing.length > 0 && `, ${typing.join(', ')} typing...`}
</p>
<div
ref={canvas}
style={{ position: 'relative', height: 320, border: '1px solid #ccc' }}
onMouseMove={(e) => {
const b = canvas.current!.getBoundingClientRect()
room.current?.moveCursor((e.clientX - b.left) / b.width, (e.clientY - b.top) / b.height)
}}
>
{others.map(
(p) =>
pos[p.id] && (
<span
key={p.id}
style={{ position: 'absolute', left: `${pos[p.id]!.x * 100}%`, top: `${pos[p.id]!.y * 100}%`, color: p.color }}
>
{p.name}
</span>
),
)}
</div>
<input placeholder="Type here" onChange={() => room.current?.setTyping(true)} />
</div>
)
}

Snippet for illustration, written for the Next.js App Router with @potalab/base/next,@potalab/base/react and @potalab/base. Set NEXT_PUBLIC_POTALAB_BASE_URL andNEXT_PUBLIC_POTALAB_BASE_KEY, adapt names and redirect URLs, and review policies before production.

Next: Todo app with RLS →