How to debug that yText isn't receiving updates?


I have a project that uses yjs to sync a text using a yText. My current test setup consists of a server and two clients.
Every party has one ydoc. On connection the clients and the server try to sync by sending their state vectors (using encodeStateVector). The other side answers with an encoded state update (using encodeStateAsUpdate) using these vectors. They should be synced now.

Now the yText on one of the clients get altered externally which triggers the update callback of the yDoc which sends
the update array as it is to the server which applies it (using applyUpdate) into its own yDoc. On every update of the server ydoc all clients (Except the one that sent the original update) get this new update that comes from the update event of the server yDoc. The clients take this and apply it into their local yDoc.

So far so good. Now comes the problem:
If the clients send an update to the server it is received and processed as it should. I set up an observer on the yText that shows that the content gets updated.
But when the server distributes updates to the clients then the clients fail to apply these. The update event of the yText gets called but the content is still the same and there are no deltas in the event.
The clients don’t even receive the initial content from the state vector encoded sync.

I checked the encoding and decoding of the messages to verify that the apply function gets the exact uint8array that is given in the update event. The transportation is also fine. No messages or bytes get lost. There are no errors while applying the update.

This is a brief description of the process. If you need any more information or access to code tell me.

Do you have any idea how I can debug this problem? How can I find out why the local yDoc doesn’t want to apply the updates correctly?

I found the answer!
The communication on the client was done by another lib (self written and imported via node module), that handles the communication and applies incoming updates into the ydoc.
After about TWO WEEKS of debugging this problem that yielded NO errors, messages or anything else I had the idea that a warning I never took really care of may be more than I thought.
It was “Yjs was already imported. Importing different versions of Yjs often leads to issues.”. Why didn’t I check this out sooner? Well. This warning reads like “oh oh. you’re using yjs 13.xx and yjs 13.yy. you should check this.” which clearly wasn’t a problem because my yarn.lock said “you’re only importing 13.xx”. But because I tested EVERYTHING except yjs itself I digged through the source code and found this:

That’s when I noticed that my client used the mjs import of yjs and the lib uses the cjs import. So this was the problem. Some constructor check deep in yjs failed silently because it was checking constructors from the mjs version against the cjs constructors and vice versa. I mean … technically these are two different versions of yjs but if I wouldn’t have found that comment I would have NO idea that this would cause such big trouble.

So: PLEASE. Make the error message in the console a bit more verbose or link the comment in the source . Right now it reads like “yeah… you’re probably using two different versions (as in semver!) of this lib.” which can easily be overseen when you’ve already checked the package-lock or yarn.lock.

I need a drink now.

See ya

Phew, I feel for you… This is one of the most frustrating bugs that I ever had to debug as well.

You are probably right about that. Will do.

1 Like

Not sure if related, but when importing the latest yJs and y-webrtcprovider on Stackblitz you get a Can't find package:y-webrtc message even when installed.