Fix cross-window Yjs sync: broadcast doc updates to all connections

The Vite Yjs plugin was only sending sync responses back to the
sender, not broadcasting updates to other WebSocket connections
in the same room. This caused cross-window sync to only work via
BroadcastChannel (same browser context), not across separate
browser windows/incognito.

Fix: Add doc.on('update') listener that broadcasts updates to all
connections in the room except the origin sender.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-03-23 07:30:00 +01:00
parent 55806fc84a
commit 288e9c07f2
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9

View file

@ -28,6 +28,21 @@ export function yjsDevPlugin(): Plugin {
awareness = new awarenessProtocol.Awareness(doc);
awarenessMap.set(docName, awareness);
// Broadcast doc updates to all connections in this room
doc.on("update", (update: Uint8Array, origin: unknown) => {
const encoder = encoding.createEncoder();
encoding.writeVarUint(encoder, messageSync);
syncProtocol.writeUpdate(encoder, update);
const message = encoding.toUint8Array(encoder);
for (const [ws, meta] of conns) {
// Don't send back to the origin connection
if (meta.docName === docName && ws !== origin && ws.readyState === ws.OPEN) {
ws.send(message);
}
}
});
awareness.on("update", ({ added, updated, removed }: { added: number[]; updated: number[]; removed: number[] }) => {
const changedClients = added.concat(updated, removed);
const encoder = encoding.createEncoder();