BitPhinix/slate-yjs-example

[Question] How to boostrap slate with a persisted doc?

mdegrees opened this issue ยท 4 comments

First of all, thank you for sharing this awesome piece of work. I managed to run the example just fine. I am trying to get it to work with persisted docs. For now, just for the sake of prototyping, I'm using y-leveldb with y-websocket.

What I can't get my head around is how to bootstrap Slate with the persisted doc. It says here

// Super hacky way to provide a initial value from the client, if
that the initial state should probably come from the server.

If I've well understood, by the time the sync process is done. The client Ydoc is in complete sync with the Websocket copy. No need to fetch anything from the server. Theoretically, I should reflect the Ydoc state on Slate but I can't get it to work. Is this even the right approach? Can you point me please to any working example?

Thank you ๐Ÿ™

Hi @mdegrees,

Thanks so much for those kind words ๐Ÿ˜Š. To provide an initial value from the server you need to modify the y-websocket code a bit. The initial version of this repo had such an implementation you can use for inspiration:

const content = this.loadDocument(docName);

Sorry in advance for the quality of code in there ๐Ÿ˜…

@BitPhinix thank you so much for your fast response ๐Ÿ™ . Looking at the implementation, it does make perfect sense.
toSlateDoc is now toSharedType right?

I think one way to go about it, without implementing a custom server is to use the exposed setPersistence method. As mentioned here yjs/yjs-demos#8 (comment) and here yjs/yjs#170 (comment)

Do you see any value in keeping Yjs' binary version of the doc alongside slate's value?

Hi @mdegrees,

I think one way to go about it, without implementing a custom server is to use the exposed setPersistence method. As mentioned here yjs/yjs-demos#8 (comment) and here yjs/yjs#170 (comment)

If it works it's definitely the way to go, the custom WebSocket server implementation was just a hack to get things running quickly for this repo. We were slate-yjs with a custom go service to handle doc changes in prod.

Do you see any value in keeping Yjs' binary version of the doc alongside slate's value?

Yes, definitely. If you want to allow clients to perform offline modifications you'll need the binary yjs document to be able to merge the changes. With a single service instance this is relatively easy but scaling it across multiple services comes with a lot of challenges. I really recommend https://discuss.yjs.dev. There are a lot of really smart and helpful people in there that can help you along the way :)

Oh cool, I see. Thank you so much for the pointers.