Hey Guys,
I am building a collaborative editor for my canvas editor, I am using Yjs for the collaborative editor. For every change one client is doing i am receiving that update at the backend and updating my yjs doc at the backend. I am using React and Nodejs along with sockets.
But one problem I am facing with this backend code (given), my backend Yjs doc is not updating and it’s saving the older data to Redis.
I tried this [Y.applyUpdate(doc, new Uint8Array(update)); & const diff = Y.diffUpdate(new Uint8Array(update), Y.encodeStateVector(doc));] but some times my older data is not updating : I am adding both backend and frontend code.
Canvas is JSON objects of elements
My data is the json object.
Any help will be grateful.
Yjs version Node : “yjs”: “^13.6.18”
Yjs version React : “yjs”: “13.0.0-103”
I understand that you maintain a Yjs document on the server and that you distribute messages via y-redis.
I suggest that you distribute the messages that you receive via the ydoc.on('update', update => {..}) event.
Alternatively, you can simply put all updates that you receive on redis.
Y.diffUpdate computes the diff between a Yjs document and a state vector. From the docs:
Y.diffUpdate(update: Uint8Array, stateVector: Uint8Array): Uint8Array Encode the missing differences to another update message. This function works similarly to Y.encodeStateAsUpdate(ydoc, stateVector) but works on updates instead.
It works similarly to Y.encodeStateAsUpdate(ydoc, stateVector).
That version is really old. I suggest that you update that ^^
I am not using y-redis, please have a look of the above implementation and suggest something which can improve this.
The problem is sometimes update is not applied to the ydoc, and it saves the older data into the redis, the updates are emitted but not saved in the db.
getOrCreateDoc(contentId); is giving me the existing doc present for this particular content.
Please suggest something why this piece of code is not able to apply the difference :
doc.transact(() => {
Y.applyUpdate(doc, new Uint8Array(update));
});
const diff = Y.diffUpdate(new Uint8Array(update), Y.encodeStateVector(doc));
Y.applyUpdate(doc, diff);
It’s happening sometimes only. For each update, the applyUpdate is not functioning properly.
This really is a lot of source code. I’m having trouble reading it. I understand this ticket is important to you, but I doubt that you will get the solution you are looking for like this.
Regarding this code… I’m really not sure what this is supposed to do.