ueberdosis / hocuspocus

The CRDT Yjs WebSocket backend for conflict-free real-time collaboration in your app.
https://tiptap.dev/docs/hocuspocus/introduction
MIT License
1.19k stars 115 forks source link

The server document judgment is always empty, resulting in duplicate data. #792

Closed LT1211 closed 5 months ago

LT1211 commented 6 months ago

Description I use y-indexedDB and hocuspocus to realize document storage and collaboration, but the document is always empty when the backend is initialized, resulting in default data being added to my yjs document every time I connect.

frontend code

const docName = id || 'slate-yjs-demo'
  const provider = useMemo(
    () =>
      new HocuspocusProvider({
        url: `ws://127.0.0.1:3000/ws?clientId=${docName}`,
        name: docName,
        connect: false
      }),
    []
  )

  const indexedDB = useMemo(
    () => new IndexeddbPersistence(docName, provider.document),
    []
  )

  const editor = useMemo(() => {
    const sharedType = provider.document.get(docName, Y.XmlText)
    const e = withReact(withCursors(withYjs(createEditor(), sharedType), provider.awareness as Awareness,{
      data: randomCursorData()
    }))

    return e
  }, [provider.awareness, provider.document])

  useEffect(() => {
    provider.connect()
    return () => provider.disconnect()
  }, [provider])

backend code

const server = Server.configure({
  name: 'hocuspocus-fra1-01',
  timeout: 30000,
  debounce: 5000,
  maxDebounce: 30000,

  async onLoadDocument(data) {
    if (data.document.isEmpty(data.documentName)) {
      const insertDelta = slateNodesToInsertDelta(initialValue)
      const sharedRoot = data.document.get(data.documentName, Y.XmlText) as unknown as Y.XmlText
      sharedRoot.applyDelta(insertDelta)
    }

    return data.document
    }
  })

The document judgment of the backend is always true,Is there something missing?

davisg123 commented 5 months ago

https://tiptap.dev/docs/hocuspocus/guides/persistence

LT1211 commented 5 months ago

https://tiptap.dev/docs/hocuspocus/guides/persistence

This is very useful to me, thank you.