yjs / y-websocket

Websocket Connector for Yjs
https://docs.yjs.dev/ecosystem/connection-provider/y-websocket
MIT License
467 stars 251 forks source link

Persisting State: Documentation? #14

Open canadaduane opened 4 years ago

canadaduane commented 4 years ago

As per your comment, it would be great to learn more about persisting state in conjunction with y-websocket. Thanks!

Huly®: YJS-533

dmonad commented 4 years ago

Hey @canadaduane , I'm currently updating y-redis and y-leveldb. I'll also restructure the API that adds persistence to y-websocket. Until now, I expected people to clone the y-websocket server, but I think it makes sense to at least allow users to share persistence modules properly.

canadaduane commented 4 years ago

That would be fantastic! Thank you.

renieravin commented 4 years ago

Hey @dmonad , any luck on this front? Would love to implement persistence in Yjs 13!

dmonad commented 4 years ago

@renieravin Sure, I'd love to get a contribution on y-leveldb.

If you want to get started, it would be best to start a new project based on y-indexeddb and rewrite it based on leveldb. I recommend to use [leveldown]https://github.com/Level/leveldown) unless there is a good alternative.

dmonad commented 4 years ago

Actually, @renieravin I just might have found a sponsor for the leveldb adapter. I'll let you know if it works out :)

anilgulecha commented 4 years ago

@dmonad was any update made on this persistence feature? The README does talk about:

PORT=1234 YPERSISTENCE=./dbDir node ./node_modules/y-websocket/bin/server.js

So is this available for use, or a WIP?

mikkorantalainen commented 3 years ago

Any new info about persisting state? I'd prefer pushing and pulling the persisting state to/from Postgres database so any pointers about that would be great.

AdventureBeard commented 3 years ago

@mikkorantalainen, you might be able to rig up a connection to Postgres by leveraging y-leveldb + level + SQLdown, though I'd be careful with SQLdown, as it looks pretty old and opinionated. You might use SQLdown as inspiration for fulfilling the level interface in a way that best suits your application's needs. Haven't tried level + SQLdown myself yet, but probably will pretty soon. Will report back if so.

rebolyte commented 3 years ago

Any update here? Being able to drop in y-redis here would be 🔥

dmonad commented 3 years ago

Hi @rebolyte

No update.

In the limited time I have for this project (very little funding), I'm working on the new Yjs documentation site.

y-leveldb is stable now and could also be used over the different databases that levelup supports.

jnbarlow commented 3 years ago

@dmonad The sample project has :

// Super hacky way to provide a initial value from the client, if
        // you plan to use y-websocket in prod you probably should provide the
        // initial state from the server.

but I can't find docs and/or figure out how to send the state up on the initial connection.
I've got this far:

utils.setPersistence({
    bindState: async (docName, yDoc) => {
        log.debug('bindState:', docName);
        //load from db and seed?
        const document = yDoc.getArray();
        console.log(document.toJSON());
        document.insert(0, [{ type: 'paragraph', children: [{ text: 'Hello world from server!' }] }]);
        console.log(document.toJSON());

        yDoc.on('update', update => {
            //do some crazy saving here.
            const doc = utils.getYDoc(docName).getArray('content').toJSON();
            log.debug(docName);
            log.debug(JSON.stringify(doc, null, 2));
        });
    },
    writeState: async (docName, yDoc) => {
        log.debug('writeState:', docName);
        //this fires on connection close
    }
});

and document has the initial state, but I just for the life of me can't figure out how to push that to the front end.

hanspagel commented 3 years ago

Just wanted to chime in and say that we’re working on a wrapper on top of y-websocket, y-redis, and y-leveldb (in collaboration with Kevin) called hocuspocus. If you struggle to put together all the building blocks, it’s probably worth a look.

It’s only available for GitHub sponsors for now, but we’ll publish it as a MIT-licensed package in the next months. If you can’t afford sponsoring our efforts, reach out to humans@hocuspocus.dev and I’ll invite you manually.