API Reference - @liveblocks/yjs
@liveblocks/yjs
is a Yjs provider enabling you to use
Liveblocks as the hosted back end of your realtime collaborative application.
Read our getting started guides to learn more.
Setup
To set up Yjs, it’s recommended to use getYjsProviderForRoom
. It’s no
longer recommended to use LiveblocksYjsProvider
directly, as issues may
happen when dynamically switching between rooms.
React
In React, pass your room object with useRoom
. It’s fine to use this
function in your React components. From here, you can access your Y.Doc
.
JavaScript
In JavaScript, pass the room retrieved with client.enterRoom
.
getYjsProviderForRoom
Gets the current or creates a new LiveblocksYjsProvider
for a room—this is
the recommended way to use Yjs. This provider will automatically be cleaned up
when the room is destroyed, so you don’t need to destroy the provider manually.
The second argument is the LiveblocksYjsProvider
options.
- yProviderLiveblocksYjsProvider
The
LiveblocksYjsProvider
for the room. The provider is automatically cleaned up and destroyed when necessary. Fetch yourY.Doc
withLiveblocksYjsProvider.getYDoc
.
- roomRoomRequired
The Liveblocks room, retrieved with
useRoom
orclient.enterRoom
. - options.autoloadSubdocsbooleanDefault is false
This option will load subdocs automatically.
- options.enablePermanentUserDatabooleanDefault is false
This option enables Yjs permanent user data class used by some libraries for tracking changes by author.
- options.offlineSupport_experimentalbooleanDefault is false
Experimental. Enable offline support using IndexedDB. This means the after the first load, documents will be stored locally and load instantly.
- options.useV2Encoding_experimentalbooleanDefault is false
Experimental. Use V2 encoding.
LiveblocksYjsProvider
LiveblocksYjsProvider
is a
Yjs provider that allows you to connect
a Yjs document to Liveblocks. Any changes you make to the document will be
stored on Liveblocks servers and synchronized with other clients in the room.
You can connect by creating a Yjs document, then passing it to
LiveblocksYjsProvider
along with the currently connected Liveblocks room.
- roomRoomRequired
The Liveblocks room, retrieved with
useRoom
orclient.enterRoom
. - yDocY.DocRequired
The
Y.Doc
for the document. - options.autoloadSubdocsbooleanDefault is false
This option will load subdocs automatically.
- options.enablePermanentUserDatabooleanDefault is false
This option enables Yjs permanent user data class used by some libraries for tracking changes by author.
- options.offlineSupport_experimentalbooleanDefault is false
Experimental. Enable offline support using IndexedDB. This means the after the first load, documents will be stored locally and load instantly.
- options.useV2Encoding_experimentalbooleanDefault is false
Experimental. Use V2 encoding.
LiveblocksYjsProvider.getYDoc
Returns the current room’s root Y.Doc
.
LiveblocksYjsProvider.awareness
The awareness instance attached to the provider.
LiveblocksYjsProvider.destroy
Cleanup function. Destroys the LiveblocksYjsProvider
instance and removes
all resources.
LiveblocksYjsProvider.on("sync")
Add an event listener for the sync
event. The sync
event is triggered when
the client has received content from the server. Can be used to fire events when
the document has loaded.
Aliased by LiveblocksYjsProvider.on("synced")
.
LiveblocksYjsProvider.off("sync")
Remove an event listener for the sync
event. The sync
event is triggered
when the client has received content from the server. Used to clean up
LiveblocksYjsProvider.on("sync")
.
Aliased by LiveblocksYjsProvider.on("synced")
.
LiveblocksYjsProvider.once("sync")
Add a one-time event listener for the sync
event. The sync
event is
triggered when the client has received content from the server. Can be used to
fire events when the document has loaded.
Aliased by LiveblocksYjsProvider.once("synced")
.
LiveblocksYjsProvider.emit("sync")
Synchronously call each listener for the sync
event in the order they were
registered, passing the supplied arguments to each.
Aliased by LiveblocksYjsProvider.emit("synced")
.
LiveblocksYjsProvider.synced
Boolean. Returns whether the client is synchronized with the back end.
LiveblocksYjsProvider.connect
Does nothing, added for compatibility. Connections are handled by the Liveblocks client.
LiveblocksYjsProvider.disconnect
Does nothing, added for compatibility. Connections are handled by the Liveblocks client.
Awareness
LiveblocksYjsProvider
instances have an awareness
property, which is
powered by
Liveblocks Presence.
You can pass it to various bindings which implement awareness, for example
plugins that enable multiplayer cursors in text editors.
Because awareness is part of presence, it’s also accessible with
room.getPresence
and useMyPresence
under the __yjs
property.
Awareness.doc
The Yjs document that the current awareness instance is attached to.
Awareness.clientId
A unique number identifying which client this awareness object is attached to.
Awareness.getLocalState
Get the current user’s awareness state.
Awareness.setLocalState
Set the current user’s awareness state. Accepts JSON-compatible objects.
Awareness.setLocalStateField
Set a single property in the current user’s awareness state. Accepts
JSON-compatible objects, or null
to remove a property.
Awareness.getStates
Returns a Map
of states for each client, with each user’s unique clientId
as
the key.
Awareness.states
A Map
of states for each client, with each user’s unique clientId
as the
key.
Awareness.meta
Provided for compatibility, but generally not necessary. This would be used for handling user awareness timeouts, but internally awareness uses Liveblocks Presence, and this handles it for you.
Awareness.destroy
Provided for compatibility, but generally not necessary. Cleanup function.
Destroys the Awareness
instance and removes all resources. Used internally
by LiveblocksYjsProvider
.
Awareness.on("destroyed")
Provided for compatibility, but generally not necessary. Add an event listener
for the destroy
event. The destroy
event is triggered when
awareness.destroy
has been called.
Awareness.off("destroyed")
Provided for compatibility, but generally not necessary. Remove an event
listener for the destroy
event. The destroy
event is triggered when
awareness.destroy
has been called. Used to clean up
[Awareness.on("destroyed")
.]
Awareness.once("destroyed")
Provided for compatibility, but generally not necessary. Add a one-time event
listener for the destroy
event. The destroy
event is triggered when
awareness.destroy
has been called.
Awareness.emit("destroyed")
Synchronously call each listener for the destroy
event in the order they were
registered, passing the supplied arguments to each.