How to get the document text(the decode content, not binary content) in y-websocket

Now I am tried to use this code to get the document text in y-websocket:

if (typeof persistenceDir === 'string') {
  console.info('Persisting documents to "' + persistenceDir + '"')
  // @ts-ignore
  const LeveldbPersistence = require('y-leveldb').LeveldbPersistence
  const ldb = new LeveldbPersistence(persistenceDir)
  persistence = {
    provider: ldb,
    bindState: async (docName, ydoc) => {
      const persistedYdoc = await ldb.getYDoc(docName)
      const newUpdates = Y.encodeStateAsUpdate(ydoc)
      ldb.storeUpdate(docName, newUpdates)
      Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(persistedYdoc))
      ydoc.on('update', update => {
        ldb.storeUpdate(docName, update)
        console.info(persistedYdoc.getText(docName))
        console.info('content:', Y.decodeSnapshot(update))
        if (persistedYdoc && persistedYdoc.snapshot) {
          const content = Y.decodeSnapshot(persistedYdoc.snapshot)
          handleFileSync(docName, ydoc, content)
        }
      })
    },
    writeState: async (docName, ydoc) => { }
  }
}

non of them could work. is it possible to get the document text in y-websocket and flush the latest document text into disk?

Your code for storing the updates looks correct. I’m doing something very similar in my y-websocket server. When you call bindState it will store the current doc and then subscribe to updates.

If it doesn’t seem to be working at all, I would verify the individual components: Is bindState called at the right time, is LeveldbPersistence able to persist the doc (even test with a hard-coded doc), etc. It seems like a usage issue rather than an issue with the core logic.

I also tried in the outer of subscribe upate, still could not work. does you have a demo cod how to get the document text? @raine I have work for this for a long time and tried so many ways, still could not fix it.

The built-in y-websocket server is a good working demo: https://github.com/yjs/y-websocket/blob/master/bin/utils.js

Are you trying to get the “document text” or the binary updates? You need to persist the binary updates. Though perhaps you want to access the text content of the document for a different reason?

I want to get the text content, not the binary content. I have made a latex online editor, when the users edit the latex project document online(one project may contains many latex document), I want to flush the newest document to disk for the next step latex compile. the latex compile engine read the document on disk, could not read document from leveldb. I am already using what you pasted project.

Once you load the Y.Doc on the server, you can access the shared types from there:

const ytext = ydoc.getText('mytext')
const filename = ydoc.guid // unique to the user and doc
fs.writeFile(filename, ytext.toString())

I tried what you provide, it works. Thank you. I found the ytext value did not change when I edit context in the UI editor, but I will tried to found out the reason, your solution works.

1 Like