So basically just saving our list in a different area. Notice that we did not have to change much of the component's logic itself. The key adjustment was simply using the Yjs shared type instead of the local use state. The operations like adding or deleting still feel very familiar, but now they're backed by Yjs's collaborative power. This sets us up to now extend our app's ability to leverage Yjs for synchronization and support and offline support.
And for that, we introduce Yjs providers. These are modular pieces that connect your local Y doc to various external systems for persistence or real-time communication. The first one we look at for real-time collaboration is Y WebSocket or WebSocket. These are independent. To make these independent copies talk to each other in real-time, especially when we want to collaborate live across different devices, we use Y WebSocket. A Yjs provider that handles the synchronization of document updates over a WebSocket connection to a lightweight server.
Here's how our Y provider DS file on the client gets updated to include the WebSocket provider package. And just like that, as you can see, all we're doing is importing a new class, instantiating it, and that's about it. What we're doing here is that we're letting Y WebSocket handle all incoming and outgoing changes for us. When changes happen on one client's Y doc, for instance, in our case, the to-do item, Y WebSocket sends those small incremental updates to the configured WebSocket server. The server then acts as a simple messenger, broadcasting those updates to all clients currently connected to the same room name.
Crucially, the server is a simple relay. It doesn't perform complex conflict resolution or store the definitive truth. That heavy lifting, the merging, and ensuring consistency is within each Y doc on the client themselves. This design keeps the server light, scalable, and focused purely on data transmission. Now to truly thrive offline, we need a way to process the data directly on the user's device so that even if they close their browser or lose Internet connectivity, their work is safe and immediately available. That's where IndexedDB comes into the picture.
Yjs leverages a package called y-index-db-provider to automatically save the entire Y doc to a local IndexedDB. This means that even if the user closes their browser, loses Internet connection, or reboots their computer, their local copy of data is safe and immediately available at the moment they reopen the app. Pretty much what local storage would do in this case, right? Only just a little more powerful because it's being managed in incremental snapshots. This is the core of enabling the instant responsiveness and seamless offline work we discussed earlier. Your app now always has access to data regardless of network conditions. This diagram here illustrates the simplified flow. Each client, as you can see, has its own application powered by a local Yjs document. This document is where all of the data lives.
Comments