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:
parent
55806fc84a
commit
288e9c07f2
1 changed files with 15 additions and 0 deletions
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue