Repository navigation
feat: implement real-time collaborative editing using Yjs - #723
Conversation
- Added a new `CollabHub` class to manage collaborative editing sessions for project documents and task descriptions. - Introduced socket events for joining, updating, and leaving collaboration rooms. - Implemented awareness updates for cursor presence among collaborators. - Updated the server to handle collaborative editing events and integrate with existing Socket.IO infrastructure. - Enhanced documentation in `README.md` to cover collaborative editing features and usage. - Added tests for the collaborative editing functionality to ensure reliability and correctness.
There was a problem hiding this comment.
Important
The relay's authorization, eviction and ordering logic hold up, but the seed lifecycle and the join/fallback path have failure modes that leave a document blank and then overwrite its stored content, and one revocation path is weaker than the README claims.
Reviewed changes
- Realtime hub (
services/realtime/src/collab.ts,server.ts,index.ts): in-memoryY.Doc+ awareness per doc/task relayed over Socket.IO, per-update IAM checks, IAM-change eviction, UUID/5 MB/2 000-room limits and a 30 s room grace period. - Client session (
apps/web/src/lib/collab-session.ts,hooks/use-collab-session.ts): reference-counted session, rejoin with missing-state exchange, seed retry loop,off | connecting | readystate machine. - Editor integration (
doc-editor.tsx,description-section.tsx,lib/utils.ts): BlockNotecollaborationoption, peer edits excluded via theyjs-remotechange source, seeding only by the client the room picks. - Tests and docs:
collab.test.ts,collab-session.test.ts,doc-editor.collab.test.tsx, the new e2e spec and feature file, and the realtime README section.
I verified the load-bearing third-party contracts against the actual packages: BlockNote 0.48.1 maps y-sync$ transactions to { type: "yjs-remote" } and accepts a { awareness } provider, and socket.io-client 4.8.1 starts the ack timer immediately even for buffered packets.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
Incremental review — fe2a47a
The seed-lifecycle and ghost-member fixes are in and verified. The seed grant now has a 10 s deadline, a handover to another member, and a collab:seed event; the client-side whenSeeder/isCollabDocEmpty guard and the per-account room cap are sound. Tests pass: bun test 55/55, vitest 10/10, tsc --noEmit clean on both workspaces.
One issue found
services/realtime/src/collab.ts:445). reassignSeed picks the first member whose id differs from the current seeder, so a room with two idle members and no content alternates A → B → A → B every 10 s, emitting collab:seed each time. A client that already has seed set ignores the event, so the practical effect is repeated no-op events and a timer that never stops for an empty room. Excluding the previous seeder (or tracking the members already tried) would end the cycle.
Nits
apps/web/src/components/projects/docs/doc-editor.tsx:198andapps/web/src/components/projects/interactions/task-detail/description-section.tsx:247:whenSeederreturns an unsubscribe that the effect never calls.mountedRefmakes the listener a no-op, but the listener stays in the set until the session is destroyed.services/realtime/README.md:147: the "Limits" bullet is now duplicated; the old one at line 149 covers REST-write conflicts.apps/web/src/lib/collab-session.ts:428:isCollabDocEmptyrunsfragment.toString()on every one of the 20 seed attempts; the server'sisEmptydoes the same per update. Not a correctness issue, but O(n) per attempt on a large document.
Verified
collab.tsjoinnow rejects a dead socket (isAlive) before adding a member, and the test covers the leak.seedTimer/markSeeded/reassignSeed/destroyclear the timer on every path.MAX_ROOMS_PER_USERis checked before the room is created; excluding the current room fromroomsOfkeeps rejoins working.- The client retry no longer retries on a timeout; the
"dropped"sentinel only retries on a disconnect. useCollabSessionfalls back toOFFwhen the user query resolves without a user.seedingRefis now read in bothhandleChangehandlers.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
…e cycling of members
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
This run reviewed 1c7dc42, the fix-only delta on top of the previous incremental review; every open thread from both prior reviews is now addressed and resolved.
- Stopped the seed-grant cycle —
collab.tsgainedRoom.seedTried:grantSeedrecords the member it grants,reassignSeedonly picks untried members, and when none is left it frees the grant and clears the timer instead of alternating A → B → A → B.markSeededclears the set, and the join path still grantsseed: trueto a new joiner, so a freed grant is recoverable. A test asserts both halves (grantedstays["b"], thencjoins withseed: true). - Dropped the
whenSeederlistener on real unmount — bothdoc-editor.tsxanddescription-section.tsxkeep the unsubscribe inseedUnsubRefand call it from the mount effect's cleanup through asetTimeout(…, 0)gated onmountedRef.current, so StrictMode's synchronous simulated unmount keeps the listener and a real unmount removes it. The seed effect is stillinitializedRef-guarded, so at most one listener is ever registered. - De-duplicated the README bullets — the two "Limits" entries are now
CapacityandREST writes.
Verified locally: bun test in services/realtime (56 pass), the two collab vitest suites in apps/web (11 pass), and tsc clean in both workspaces. The one remaining prior nit (isCollabDocEmpty/isEmpty serializing the fragment per attempt) is unchanged and informational only.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
Incremental review — c4c9979
✅ No new issues found.
The delta is a hardening pass over the prior review's concerns and is verified: fail-open access checks, seed hand-off cancellation, awareness ownership, per-room/per-socket capacity limits, and a tree-walking isEmpty replacing the XML-string regex.
Tests: bun test in services/realtime 64/64; bunx vitest run src/lib/collab-session.test.ts src/components/projects/docs/doc-editor.collab.test.tsx in apps/web 14/14; tsc --noEmit in services/realtime and tsc -b in apps/web clean.
Verified
canWritereturnsboolean | undefined;joinfails closed onundefined,updatedrops only that update and emitscollab:error,recheckonly evicts onfalse. A transient API blip no longer evicts every editor.collab:seed-cancelis sent to the previous seeder before the grant moves; the client'sseedRevokedstopsseedCollabDoc, andgrantSeedclears it on a later grant.awarenessClientIdsparses the y-protocols wire format;Room.awarenessOwnersrejects a socket speaking for another client's id;evictclears its own ids.MAX_ROOM_BYTES,RATE_BURST/RATE_PER_SEC,MAX_PENDING_UPDATESare enforced;enqueuereturnsfalseand the client getsbacklog.hasContentwalksY.XmlFragment/Y.XmlElement/Y.XmlText/Y.XmlHook.Y.XmlTextis not aY.XmlFragment, so text is checked first;Y.XmlElementis aY.XmlFragment, so placeholder elements fall through to recursion. Placeholder-only trees returntrue; real text or non-placeholder blocks returnfalse.onServerErrorre-joins once at a time (resyncingguard, 1 s delay) and applies the result;applyJoinre-sends the client's missing state.
Nits (non-blocking)
room.bytesis incremented beforeY.applyUpdate; a failed apply leaves the counter slightly high (conservative, bounded).bucketsis only cleared onleaveAll, not on a singleleave; minor memory retention.
All prior Pullfrog threads remain resolved.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Summary
Real-time multiplayer editing (Yjs) for the BlockNote editors of project docs and task descriptions. Several people can type in the same doc or description at once and see each other's cursors. Comment editors are intentionally not collaborative: a comment composer is one person's draft.
How it works
Realtime service (
services/realtime/src/collab.ts, wired inserver.ts/index.ts)CollabHubkeeps one in-memoryY.Doc+ awareness per edited resource and relays updates over the existing Socket.IO connection (no second WebSocket server).collab:join(ack),collab:update,collab:awareness,collab:leave; server → clientcollab:revoked.docs:write/tasks:writeon the exact resource, checked through the sharedAccessDecider. When IAM changes, every member is re-checked and evicted if access was lost. Ids are validated as UUIDs; updates are capped at 5 MB; rooms are capped.seed: trueand is the only client that writes the stored content into the Y.Doc. A room holding only BlockNote's empty placeholder paragraph still counts as empty.Web (
apps/web)lib/collab-session.ts: a reference-countedCollabSession(Y.Doc + awareness) per resource. Handles binary payloads arriving asArrayBuffer, rejoins and exchanges missing state after a reconnect, and retries the join if the socket drops mid-join. Seeding retries until the write actually reaches the shared doc.hooks/use-collab-session.ts: joins the room only when the user can edit; otherwise (no write access, realtime service unreachable) the editors fall back to the previous single-user behavior.doc-editor.tsx,description-section.tsx: BlockNote is built with itscollaborationoption; peers' edits don't mark the local editor dirty, so only the author saves. While collaborating, the shared doc is the source of truth: refetched content from a peer's save is not replayed into it. "Write with AI" on task descriptions is applied once, from the requesting user's client.yjs,y-protocols(apps/web,services/realtime).Known limitations
services/realtime/README.md.Testing
services/realtime: newcollab.test.ts(authorization, seeding, relay, revocation, reconnect, placeholder rooms); all 51 tests pass,tscand biome clean.apps/web:collab-session.test.ts(join, retry on drop, echo suppression, revocation) anddoc-editor.collab.test.tsx(seeding under StrictMode);tsc -bclean, full suite passed before the last two fixes (StrictMode seed, join retry), which were covered by targeted tests only.apps/e2e: newtests/realtime/collaborative-editing.spec.ts(+.feature): two members in separate browsers share a doc and a task description (content appears once, live typing both ways, REST copy holds both edits). 20/20 repeats on Chromium; the existing docs, task-detail and realtime specs also pass (108).mobile-chrome, a read-only user, losing access mid-edit, and a manual check in a dev (StrictMode) browser session after the final fix.