Extend the capabilities of Yjs using our REST API and webhooks
Today, we'll dive into the different ways Liveblocks enables developers to extend Yjs, allowing you to integrate your document data seamlessly into existing systems with our REST API and webhooks.
In recent years, Yjs has grown to be the preferred
way for developers to build collaborative rich text editors owing to its
powerful features such as multiplayer undo/redo, lazy loading subdocuments, and
offline support.
If you’ve already built collaborative experiences with Yjs, then you’ve probably
discovered that integrating it with other parts of your system can be
challenging. Because every update gets stored, Yjs documents can be hard to
index, parse, and tend to grow quickly in size over time.
Today, we’ll dive into the different ways Liveblocks enables developers to
extend Yjs capabilities in order to integrate Yjs data seamlessly into an
exising system.
It’s possible to update a Yjs document by REST API, using the
Send a Binary Yjs Update API.
Here’s a code snippet, showing how it works.
// Create a Yjs documentconst yDoc =newY.Doc(); // Create your data structures and make your updateconst yText = yDoc.getText("my-y-text");yText.insert(0,"Hello world"); // Encode the document state as an update messageconst yUpdate =Y.encodeStateAsUpdate(yDoc); const roomId ="my-room-name";awaitfetch(`https://api.liveblocks.io/v2/rooms/${roomId}/ydoc`,{ method:"PUT", body: yUpdate,});
If you’re interested in using this, make sure to read our full guide on the
topic:
We’ve added new Yjs-triggered events to our
Webhooks enabling you to
automatically perform custom actions in your back end, such as sending
notifications or saving your data to a database. This can all be implemented
this by listening to the
YDocUpdatedEvent, which runs
whenever a Yjs document is modified.
Yjs webhook events sent to your backend.
Our previous webhooks continue to work with Yjs too, for example you can respond
to rooms being created and
deleted, or users
entering and
leaving rooms.
When our webhooks and REST API are combined, it makes it easy to synchronize
your Yjs document with a database. Here are some detailed guides on how to
synchronize your Yjs document data to various databases:
Liveblocks is already the most advanced platform for Yjs, yet we have plans to
extend Yjs even further, and we’re busy working on them right now—make sure to
check back soon. We’re excited to see what you build with Liveblocks Yjs.