napolab / y-durableobjects

Real-time collaboration with Yjs on Cloudflare Workers using Durable Objects, eliminating Node.js dependencies. Inspired by y-websocket
https://yjs.napochaan.dev/
MIT License
117 stars 7 forks source link

yDoc must be saved with each change. #28

Closed naporin0624 closed 7 months ago

naporin0624 commented 7 months ago

Problems with existing implementation

In the Hibernation API, instances of DurableObjects go dormant as soon as no data is sent from the client, so yDocs stored in-memory are quickly volatile.

Therefore, the existing implementation of storing the in-memory state in transaction storage when the number of connections drops below a certain number is not appropriate.

Reason why it seems to work in the demo site

However, https://yjs.napochaan.dev, which is now available on the demo site, seems to work fine. This is because the client implementation of y-websocket, WebSocketProvider, wrote a constructor that periodically sends local yDoc when the connection becomes Open, so it does not go into hibernation state even if Hibernation API is used, It is assumed that the server-side yDoc remained unvolatilized until the client communication was disconnected.

https://github.com/yjs/y-websocket/blob/master/src/y-websocket.js#L302-L311

What should be changed

naporin0624 commented 7 months ago

The resync defined in the constructor of y-websocket's Provider was not firing and lexical was periodically updating its awareness on the client side, which led to the periodic transmission.

https://github.com/yjs/y-websocket/blob/master/src/y-websocket.js#L344-L353

naporin0624 commented 7 months ago

Yjs Update Document

https://docs.yjs.dev/api/document-updates#alternative-update-api

External Storage Implements

https://github.com/yjs/y-leveldb/blob/master/src/y-leveldb.js#L322

https://github.com/partykit/partykit/blob/main/packages/y-partykit/src/storage.ts