@liveblocks/client
@liveblocks/client provides you with JavaScript bindings for our realtime
collaboration APIs, built on top of WebSockets. Read our
getting started guides to learn more.
createClient
Creates a client that allows you to connect to Liveblocks servers.
You must define either authEndpoint or publicApiKey. Resolver functions
should be placed inside here, and a number of other options are available.
- clientClient
Returns a Client, used for connecting to Liveblocks.
- authEndpoint
The URL of your back end’s authentication endpoint as a string, or an async callback function that returns a Liveblocks token result. Either
authEndpointorpublicApiKeyare required. Learn more about using a URL string and using a callback. - publicApiKeystring
The public API key taken from your project’s dashboard. Generally not recommended for production use. Either
authEndpointorpublicApiKeyare required. Learn more. - throttlenumberDefault is 100
The throttle time between WebSocket messages in milliseconds, a number between
16and1000is allowed. Using16means your app will update 60 times per second. Learn more. - preventUnsavedChangesbooleanDefault is false
When set, navigating away from the current page is prevented while Liveblocks is still synchronizing local changes. Learn more.
- lostConnectionTimeoutnumberDefault is 5000
After a user disconnects, the time in milliseconds before a
"lost-connection"event is fired. Learn more. - backgroundKeepAliveTimeoutnumber
The time before an inactive WebSocket connection is disconnected. This is disabled by default, but setting a number will activate it. Learn more.
- resolveUsers
A function that resolves user information in Comments, Text Editor, and Notifications. Return an array of
UserMeta["info"]objects in the same order they arrived. Learn more. - resolveRoomsInfo
A function that resolves room information in Notifications. Return an array of
RoomInfoobjects in the same order they arrived. Learn more. - resolveGroupsInfo
A function that resolves group information in Comments and Text Editor. Return an array of
GroupInfoobjects in the same order they arrived. Learn more. - resolveMentionSuggestions
A function that resolves mention suggestions in Comments and Text Editor. Return an array of user IDs or mention objects. Learn more.
- polyfills
Place polyfills for
atob,fetch, andWebSocketinside here. Useful when using a non-browser environment, such as Node.js or React Native. - badgeLocationDefault is "bottom-right"
The location of the "Powered by Liveblocks" badge. Can be set to either
"top-right","bottom-right","bottom-left", or"top-left". Learn more.
createClient with public key
When creating a client with a public key, you don’t need to set up an authorization endpoint. We only recommend using a public key when prototyping, or on public landing pages, as it makes it possible for end users to access any room’s data. You should instead use an auth endpoint.
createClient with auth endpoint
If you are not using a public key, you need to set up your own authEndpoint.
Please refer to our Authentication guide.
createClient with auth endpoint callback
If you need to add additional headers or use your own function to call your
endpoint, authEndpoint can be provided as a custom callback. You should return
the token created with
Liveblocks.prepareSession
or liveblocks.identifyUser,
learn more in authentication guide.
room is the room ID that the user is connecting to. When using
Notifications, room
can be undefined, as the client is requesting a token that grants access to
multiple rooms, rather than a specific room.
Fetch your endpoint
Here’s an example of fetching your API endpoint at /api/liveblocks-auth within
the callback.
Token details
You should return the token created with
Liveblocks.prepareSession
or liveblocks.identifyUser.
These are the values the functions can return.
- A valid token, it returns a
{ "token": "..." }shaped response. - A token that explicitly forbids access, it returns an
{ "error": "forbidden", "reason": "..." }shaped response. If this is returned, the client will disconnect and won’t keep trying to authorize.
Any other error will be treated as an unexpected error, after which the client will retry the request until it receives either 1. or 2.
WebSocket throttle
By default, the client throttles the WebSocket messages sent to one every 100
milliseconds, which translates to 10 updates per second. It’s possible to
override that configuration with the throttle option with a value between 16
and 1000 milliseconds.
This option is helpful for smoothing out realtime animations in your application, as you can effectively increase the framerate without using any interpolation. Here are some examples with their approximate frames per second (FPS) values.
Prevent users losing unsaved changes
Liveblocks usually synchronizes milliseconds after a local change, but if a user
immediately closes their tab, or if they have a slow connection, it may take
longer for changes to synchronize. Enabling preventUnsavedChanges will stop
tabs with unsaved changes closing, by opening a dialog that warns users. In
usual circumstances, it will very rarely trigger.
More specifically, this option triggers when:
- There are unsaved changes after calling any hooks or methods, in all of our products.
- There are unsaved changes in a Text Editor.
- There’s an unsubmitted comment in the Composer.
- The user has made changes and is currently offline.
Internally, this option uses the beforeunload event.
Lost connection timeout
If you’re connected to a room and briefly lose connection, Liveblocks will reconnect automatically and quickly. However, if reconnecting takes longer than usual, for example if your network is offline, then the room will emit an event informing you about this.
How quickly this event is triggered can be configured with the
lostConnectionTimeout setting, and it takes a number in milliseconds.
lostConnectionTimeout can be set between 1000 and 30000 milliseconds. The
default is 5000, or 5 seconds.
You can listen to the event with room.subscribe("lost-connection"). Note
that this also affects when others are reset to an empty array after a
disconnection. This helps prevent temporary flashes in your application as a
user quickly disconnects and reconnects. For a demonstration of this behavior,
see our connection status example.
Background keep-alive timeout
By default, Liveblocks applications will maintain an active WebSocket connection
to the Liveblocks servers, even when running in a browser tab that’s in the
background. However, if you’d prefer for background tabs to disconnect after a
period of inactivity, then you can use backgroundKeepAliveTimeout.
When backgroundKeepAliveTimeout is specified, the client will automatically
disconnect applications that have been in an unfocused background tab for at
least the specified time. When the browser tab is refocused, the client will
immediately reconnect to the room and synchronize the document.
backgroundKeepAliveTimeout accepts a number in milliseconds—we advise using a
value of at least a few minutes, to avoid unnecessary disconnections.
resolveUsers
Comments and Text Editor store user IDs in their system, but no other user information. To display user information in Comments, Text Editor, and Notifications components, such as a user’s name or avatar, you need to resolve these IDs into user objects. This function receives a list of user IDs and you should return a list of user objects of the same size, in the same order.
User IDs are automatically resolved in batches with a maximum of 50 users per batch to optimize performance and prevent overwhelming your user resolution function.
The name and avatar you return are rendered in
Thread components.
User objects
The user objects returned by the resolver function take the shape of
UserMeta["info"], which contains name and avatar by default. These two
values are optional, though if you’re using the
Comments default components,
they are necessary. Here’s an example of userIds and the exact values
returned.
You can also return custom information, for example, a user’s color:
Accessing user data in React
You can access any values set within resolveUsers with the
useUser hook.
resolveRoomsInfo
When using
Notifications with
Comments, room IDs will be used to
contextualize notifications (e.g. “Chris mentioned you in room-id”) in the
InboxNotification
component. To replace room IDs with more fitting names (e.g. document names,
“Chris mentioned you in Document A”), you can provide a resolver function to
the resolveRoomsInfo option in createClient.
This resolver function will receive a list of room IDs and should return a list of room info objects of the same size and in the same order.
In addition to the room’s name, you can also provide a room’s URL as the url
property. If you do so, the
InboxNotification
component will automatically use it. It’s possible to use an inbox
notification’s roomId property to construct a room’s URL directly in React and
set it on
InboxNotification
via href, but the room ID might not be enough for you to construct the URL,
you might need to call your backend for example. In that case, providing it via
resolveRoomsInfo is the preferred way.
resolveGroupsInfo
When using group mentions with Comments
and Text Editor, group IDs will be used
instead of user IDs. Similarly to resolveUsers,
you can provide a resolver function to the resolveGroupsInfo option in
createClient to assign information like names and avatars to
group IDs.
Accessing group info in React
You can access any values set within resolveGroupsInfo with the
useGroupInfo hook.
resolveMentionSuggestions
To enable creating mentions in Comments
and Text Editor, you can provide a
resolver function to the resolveMentionSuggestions option in
createClient. These mentions will be displayed in the
Composer component and in
text editors.
This resolver function will receive the mention currently being typed (e.g. when
writing “@jane”, text will be jane) and should return a list of user IDs
matching that text. This function will be called every time the text changes but
with some debouncing.
Group mentions
To support group mentions in Comments and Text Editor, you can return a list of mention objects instead of user IDs to suggest a mix of user and group mentions.
The mention objects specify which kind of mention it is, the ID to mention (user ID or group ID), etc.
createClient for Node.js
To use @liveblocks/client in Node.js, you need to provide WebSocket and
fetch polyfills. As polyfills, we recommend installing ws and
node-fetch.
Then, pass them to the createClient polyfill option as below.
Note that node-fetch v3+
does not support CommonJS.
If you are using CommonJS, downgrade node-fetch to v2.
createClient for React Native
To use @liveblocks/client with React Native, you
need to add an atob polyfill. As a polyfill, we recommend installing
base-64.
Then you can pass the decode function to our atob polyfill option when you
create the client.
Powered by Liveblocks branding
By default, Liveblocks displays a "Powered by Liveblocks" badge in your
application. You can adjust the position of the badge by setting the
badgeLocation property on createClient.
If you wish to remove the badge entirely, you can do so by following these steps:
- In the Liveblocks dashboard, navigate to your team's settings.
- Under General, toggle on the remove "Powered by Liveblocks" branding option.
Removing the "Powered by Liveblocks" badge on your projects requires a paid plan. See the pricing page for more information.
Client
Client returned by createClient which allows you to connect to Liveblocks
servers in your application, and enter rooms.
Client.enterRoom
Enters a room and returns both the local Room instance, and a leave
unsubscribe function. The authentication endpoint is called as soon as you call
this function. Used for setting initial Presence
and initial Storage values.
Note that it’s possible to add types to your room.
- roomRoom<Presence, Storage, UserMeta, RoomEvent>
A Room, used for building your Liveblocks application. Learn more about typing your room.
- leave() => void
A function that’s used to leave the room and disconnect.
- roomIdstringRequired
The ID of the room you’re connecting to.
- options.initialPresenceJsonObject
The initial Presence of the user entering the room. Each user has their own presence, and this is readable for all other connected users. A user’s Presence resets every time they disconnect. This object must be JSON-serializable. Learn more.
- options.initialStorageLsonObject
The initial Storage structure for the room when it’s joined for the first time. This is only set a single time, when the room has not yet been populated. This object must contain conflict-free live structures. Learn more.
- options.autoConnectbooleanDefault is true
Whether the room immediately connects to Liveblocks servers.
- options.engine1 | 2
Preferred storage engine version to use when creating the room. Only takes effect if the room doesn’t exist yet. The v2 Storage engine supports larger documents, is more performant, has native streaming support, and will become the default in the future. Learn more.
Setting initial Presence
Presence is used for storing temporary user-based values, such as a user’s
cursor coordinates, or their current selection. Each user has their own
presence, and this is readable for all other connected users. Set your initial
Presence value by using initialPresence.
Each user’s Presence resets every time they disconnect, as this is only meant
for temporary data. Any JSON-serializable object is allowed (the JsonObject
type).
Setting initial Storage
Storage is used to store permanent data that’s used in your application, such as
shapes on a whiteboard, nodes on a flowchart, or text in a form. The first time
a room is entered, you can set an initial value by using initialStorage.
initialStorage is only read and set a single time, unless a new top-level
property is added.
If a new top-level property is added to initialStorage, the next time a user
connects, the new property will be created. Other properties will be unaffected.
Any
conflict-free live structures
and JSON-serializable objects are allowed (the LsonObject type).
Speed up connecting to a room
To speed up connecting to a room, you can call
Liveblocks.prewarmRoom
on the server, which will warm up a room for the next 10 seconds. Triggering
this directly before a user navigates to a room is an easy to way use this API.
Client.getRoom
Gets a room by its ID. Returns null if client.enterRoom has not been
called previously.
It’s unlikely you’ll need this API if you’re using the newer
client.enterRoom API. Note that it’s possible to
add types to your room.
- roomRoom<Presence, Storage, UserMeta, RoomEvent> | null
A Room, used for building your Liveblocks application. Returns
nullif the room has not yet been joined by the current client. Learn more about typing your room.
- roomIdstringRequired
The ID of the room you’re connecting to.
Client.getSyncStatus
Gets the current Liveblocks synchronization status.
- returns"synchronizing" | "synchronized"
Will be
"synchronizing"if there are any local changes to any part of Liveblocks that still need to be acknowledged by the server. Will be"synchronized"when all local changes have been persisted.
Client.logout
Purges any auth tokens from the client’s memory. If there are any rooms that are still connected, they will be forced to reauthorize.
When to logout
Use this function if you have a single page application (SPA) and you wish to
log your user out, and reauthenticate them. This is a way to update your user’s
info after a connection has begun.
AI Copilots
defineAiTool
Create a custom tool for your AI copilot to use. Defining tools allow the AI copilot to look up information on-demand, render your own components based on the tool’s arguments, or perform actions in your application on behalf of the current user, such as creating content, updating the application state, or interacting with external services.
Note that the function should be called like this defineAiTool()({ ... })
(double-parens). This allows TypeScript’s inference to work correctly. For the
best type inference experience, TypeScript 5.3 or higher is recommended. While
Liveblocks supports TypeScript 5.0+, full type inference for defineAiTool()
requires TypeScript 5.3+.
- toolAiOpaqueToolDefinition
An AI tool.
- descriptionstringRequired
A clear description of what the tool does. Used by AI to understand when to call this tool.
- parametersJSONSchema7Required
JSON Schema defining the tool’s input parameters. The AI will validate arguments against this schema.
- enabledboolean
Whether this tool should be enabled. When set to
false, the tool will not be made available to the AI copilot for any new/future chat messages, but will still allow existing tool invocations to be rendered that are part of the historic chat record. Defaults to true. - executefunction
Async function that performs the tool’s action. Receives validated arguments and execution context, returns structured data. See implementing the tool call via
execute. - renderfunction
React component function that renders the tool’s UI during different execution stages. See tool call rendering stages.
Tools can be registered globally with
RegisterAiTool or
passed directly to AiChat.
Tool call rendering stages
Rendering a tool call can be done before the tool call is executed, which allows you to display a UI during its entire lifecycle. The tool call stages are:
receiving(since 3.4) The tool call is being received and its args are being streamed in. During this stage, you can accesspartialArgsto display a UI while the tool call arguments are still being constructed, but before the tool call is executed.executingThe tool call is currently executing, or is ready to be. In this stage, theargsare fully known, but the result of the tool call is not known yet.executedThe tool call has been executed, and the result is known. This happens after yourexecutefunction was run, or after you calledrespond()insiderender. In this stage, theresultobject will be available.
The render component will automatically re-render when its stage changes.
Implementing tool calls
When you implement a tool call, use one of these combinations:
- Implement
executeandrender - Implement only
execute, but norender - Implement only
render, but make sure to eventually callrespond()
Implementing your tool call via execute
If you implement the execute function, this function will automatically be
invoked when the tool call gets made. The return value of this function will be
the result that will be passed back to the AI copilot.
{ data: any, description?: string }The data to return in case of success.datamust be a legal JSON value. Providing a description is optional. If you provide a description, it will be passed to the AI copilot to help it understand the returned data or to provide follow-up instructions for how to respond to this tool result.{ error: string }The error message in case the tool call failed to execute.{ cancel: true | string }If the tool call should be cancelled. You can optionally provide a cancel reason as an instruction to the AI copilot.
The returned value can be observed in the render method, through the result
param:
If you do not implement render alongside execute, the tool call will still
be executed, but no UI will be displayed. The result will still be passed back
to the AI copilot.
Implementing your tool call via render
Sometimes you may not want to immediately execute the tool call. This is most
common to build a Human-in-the-Loop (HITL) style UI where you want the user to
confirm or correct the tool call’s behavior. In these scenarios, you do not want
to implement execute. Instead, you could display any UI, as long as you
eventually call the respond function that is provided to render’s props.
In this example, until the Confirm button is clicked, the AI chat will remain in “executing” stage, awaiting the result of this tool call.
This example is for illustrative purposes only. In practice, using our
AiTool.Confirmation
tool is preferred for building confirm/cancel flows.
Like with the execute function, the respond function should be called with a
value of this shape:
{ data: any, description?: string }The data to return in case of success.datamust be a legal JSON value. Providing a description is optional. If you provide a description, it will be passed to the AI copilot to help it understand the returned data or to provide follow-up instructions for how to respond to this tool result.{ error: string }The error message in case the tool call failed to execute.{ cancel: true | string }If the tool call should be cancelled. You can optionally provide a cancel reason as an instruction to the AI copilot.
Handling different tool call stages
You can handle all three stages of a tool call in your render function to provide a smooth user experience during tool call streaming and execution:
In this example, the tool arguments stream in progressively during the
receiving stage, causing multiple re-renders as each field appears:
- 1st render:
{ stage: "receiving", partialArgs: {} } - 2nd render:
{ stage: "receiving", partialArgs: { origin: "New York" } } - 3rd render:
{ stage: "receiving", partialArgs: { origin: "New York", destination: "London" } } - 4th render:
{ stage: "receiving", partialArgs: { origin: "New York", destination: "London", departureDate: "2024-12-15" } } - 5th render:
{ stage: "receiving", partialArgs: { ..., passengers: [] } } - 6th render:
{ stage: "receiving", partialArgs: { ..., passengers: [{ name: "John" }] } } - 7th render:
{ stage: "receiving", partialArgs: { ..., passengers: [{ name: "John", age: 3 }] } } - 8th render:
{ stage: "receiving", partialArgs: { ..., passengers: [{ name: "John", age: 30 }] } } - Final render:
{ stage: "executing", args: { /* complete object */ } }
This demonstrates how each field and nested property appears incrementally, providing real-time feedback to users as the AI constructs the tool call arguments.
Arguments are streamed in forward-only order. Once a field begins appearing, all
previous fields are complete and won’t be modified. You’ll never see
{ origin: "New York", destination: "London" } followed by
{ origin: "San Francisco", destination: "London" }, but you might see
{ origin: "New" } then { origin: "New York" } then
{ origin: "New York", destination: "London" }.
Room
Room returned by client.enterRoom (or client.getRoom).
Room.getPresence
Return the current user’s Presence. Presence is used to store custom properties on each user that exist until the user disconnects. An example use would be storing a user’s cursor coordinates.
Presence is set with updatePresence and can be typed
when you enter a room. The example above is using
the following type:
- presenceTPresence
An object holding the Presence value for the currently connected user. Presence is set with
updatePresence. Will always be JSON-serializable.TPresenceis thePresencetype you set yourself, learn more.
Room.updatePresence
Updates the current user’s Presence. Only pass the properties you wish to update—any changes will be merged into the current presence. The entire presence object will not be replaced.
- updateTPresenceRequired
The updated Presence properties for the current user inside an object. The user’s entire Presence object will not be replaced, instead these properties will be merged with the existing Presence. This object must be JSON-serializable.
- options.addToHistorybooleanDefault is false
Adds Presence values to the history stack, meaning using undo and redo functions will change them. Learn more.
Add Presence to history
By default, Presence values are not added to history. However, using the
addToHistory option will add items to the undo/redo stack.
See room.history for more information.
Room.getOthers
Returns an array of currently connected users in the room. Returns a
User object for each user. Note that you can also subscribe to
others using Room.subscribe("others").
- othersUser<Presence, UserMeta>[]
An array holding each connected user’s
Userobject.Usercontains the current user’s Presence value, along with other information. Presence is set withupdatePresence. Returns an empty array when no other users are currently connected. Will always be JSON-serializable.
Room.broadcastEvent
Broadcast an event to other users in the Room. Events broadcast to the room can
be listened to with Room.subscribe("event"). Takes a custom event payload
as first argument. Should be serializable to JSON.
- eventTRoomEventRequired
The event to broadcast to every other user in the room. Must be JSON-serializable.
TRoomEventis theRoomEventtype you set yourself, learn more. - options.shouldQueueEventIfNotReadybooleanDefault is false
Queue the event if the connection is currently closed, or has not been opened yet. We’re not sure if we want to support this option in the future so it might be deprecated to be replaced by something else. Learn more.
Receiving an event
To receive an event, use Room.subscribe("event"). The user property
received on the other end is the sender’s User instance.
We recommend using a property such as type, so that it’s easy to distinguish
between different events on the receiving end.
Typing multiple events
When defining your types, you can pass a RoomEvent type
in your config file to receive type hints in your app. To define multiple
different custom events, use a union.
Broadcasting an event when disconnected
By default, broadcasting an event is a “fire and forget” action. If the sending
client is not currently connected to a room, the event is simply discarded. When
passing the shouldQueueEventIfNotReady option, the client will queue up the
event, and only send it once the connection to the room is (re)established.
We’re not sure if we want to support shouldQueueEventIfNotReady in the future,
so it may be deprecated and replaced with something else.
Room.getSelf
Gets the current User. Returns null if the client is not yet
connected to the room.
- userUser<Presence, UserMeta> | null
Returns the current
User. Returnsnullif the client is not yet connected to the room.
Here’s an example of a full return value, assuming Presence and UserMeta
have been set.
Room.getStatus
Gets the current WebSocket connection status of the room. The possible value
are: initial, connecting, connected, reconnecting, or disconnected.
- status"initial" | "connecting" | "connected" | "reconnecting" | "disconnected"
Returns the room’s current connection status. It can return one of five values:
"initial"The room has not attempted to connect yet."connecting"The room is currently authenticating or connecting."connected"The room is connected."reconnecting"The room has disconnected, and is trying to connect again."disconnected"The room is disconnected, and is no longer attempting to connect.
Room.getStorageStatus
Get the Storage status. Use this to tell whether Storage has been synchronized with the Liveblocks servers.
- status"not-loaded" | "loading" | "synchronizing" | "synchronized"
The current room’s Storage status.
statuscan be one of four types."not-loaded"Storage has not been loaded yet asroom.getStoragehas not been called."loading"Storage is currently loading for the first time."synchronizing"Local Storage changes are currently being synchronized."synchronized"Local Storage changes have been synchronized.
Room.subscribe(storageItem)
Subscribe to updates on a particular storage item, and takes a callback function
that’s called when the storage item is updated. The Storage root is a
LiveObject, which means you can subscribe to this, as well as other live
structures. Returns an unsubscribe function.
- unsubscribe() => void
Unsubscribe function. Call it to cancel the subscription.
- storageItemL extends (LiveObject | LiveMap | LiveList)Required
The
LiveObject,LiveMap, orLiveListwhich is being subscribed to. Each time the structure is updated, the callback is called. - callback(node: L) => voidRequired
Function that’s called when
storageItemupdates. Returns the updated storage structure. - options.isDeepboolean
Subscribe to both
storageItemand its children. The callback function will be passed a list of updates instead of just the new Storage item. Learn more.
Typing Storage
To type the Storage values you receive, make sure to set your Storage type.
The type received in the callback will match the type passed. Learn more under typing your room.
Subscribe to any live structure
You can subscribe to any live structure, be it the Storage root, a child, or a
structure even more deeply nested.
Listening for nested changes
It’s also possible to subscribe to a Storage item and all of its children by
passing an optional isDeep option in the third argument. In this case, the
callback will be passed a list of updates instead of just the new Storage item.
Each such update is a { type, node, updates, source } object.
Checking where a change came from
Every update carries a source field, telling you where the change came from.
Use it to tell changes made by this client apart from changes made by others,
for example when syncing Storage into an editor that already applied the local
change itself.
- source.origin"local" | "remote"
"local"if the change was made by this client,"remote"if it was made by another client and reached this client over the network. - source.via"edit" | "undo" | "redo"
Only present when
originis"local". How the change was made: a regular edit, or a replay from the undo/redo history.
When a single notification merges local and remote changes to the same node, the
merged update is reported as { origin: "remote" }.
Using async functions
You use an async function inside the subscription callback, though bear in
mind that the callback itself is synchronous, and there’s no guarantee the
async function will complete before the callback is run again.
If the order of updates is important in your application, and it’s important to
ensure that your async function doesn’t start before the previous one
finishes, you can use a package such as
async-mutex to help you with
this. Using runExclusive will effectively form a queue for all upcoming
updates, guaranteeing serial execution.
Note that this may cause a performance penalty in your application, as certain updates will be ignored.
Room.subscribe("event")
Subscribe to events broadcast by Room.broadcastEvent. Takes a callback
that’s run when another user calls Room.broadcastEvent. Provides the
event along with the user and their connectionId of the user that sent the
message. Returns an unsubscribe function.
- unsubscribe() => void
Unsubscribe function. Call it to cancel the subscription.
- eventType"event"Required
Listen to events.
- callbackRequired
Function that’s called when another user sends an event. Receives the event, the
userthat sent the event, and theirconnectionId. If this event was sent vialiveblocks.broadcastEventor the Broadcast event API,userwill benullandconnectionIdwill be-1. Learn more
Typing events
When defining your types, you can pass a RoomEvent type
to your config file to receive type hints in your app. To define multiple
different custom events, use a union.
Receiving events from the server
Events can be received from the server with either
liveblocks.broadcastEvent
or the
Broadcast Event API.
In events sent from the server, user will be null, and connectionId will
be -1.
Room.subscribe("my-presence")
Subscribe to the current user’s Presence. Takes a callback that is called every
time the current user presence is updated with Room.updatePresence.
Returns an unsubscribe function.
- unsubscribe() => void
Unsubscribe function. Call it to cancel the subscription.
- eventType"my-presence"Required
Listen to the current user’s presence.
- callback(presence: TPresence) => voidRequired
Function that’s called when the current user’s Presence has updated, for example with
Room.updatePresence. Receives the updates Presence value.
Typing Presence
To type the Presence values you receive, make sure to set your Presence type.
The type received in the callback will match the type passed. Learn more under typing your data.
Room.subscribe("others")
Subscribe to every other users’ updates. Takes a callback that’s called when a user’s Presence updates, or when they enter or leave the room. Returns an unsubscribe function.
- unsubscribe() => void
Unsubscribe function. Call it to cancel the subscription.
- eventType"others"Required
Listen to others.
- callback(others: User<Presence, UserMeta>[], event: OthersEvent) => voidRequired
Function that’s called when another user’s Presence has updated, for example with
Room.updatePresence, or an others event has occurred. Receives an array ofUservalues for each currently connected user. Also received an object with information about the event that has triggered the update, learn more.
Typing Presence
To type the Presence values you receive, make sure to set your Presence type.
The type received in the callback will match the type passed. Learn more under typing your data.
Listening for others events
The event parameter returns information on why the callback has just run, for
example if their Presence has updated, if they’ve just left or entered the room,
or if the current user has disconnected.
Live cursors
Here’s a basic example showing you how to render live cursors.
Room.updatePresence
is being used to update each user’s cursor position.
Check our examples page for live demos.
Room.subscribe("status")
Subscribe to WebSocket connection status updates. Takes a callback that is
called whenever the connection status changes. Possible value are: initial,
connecting, connected, reconnecting, or disconnected. Returns an
unsubscribe function.
- unsubscribe() => void
Unsubscribe function. Call it to cancel the subscription.
- eventType"status"Required
Listen to status updates.
- callbackRequired
Function that’s called when the room’s connection status has changed. It can return one of five values:
"initial"The room has not attempted to connect yet."connecting"The room is currently authenticating or connecting."connected"The room is connected."reconnecting"The room has disconnected, and is trying to connect again."disconnected"The room is disconnected, and is no longer attempting to connect.
When to use status
Status is a low-level API that exposes the WebSocket’s connectivity status. You
can use this, for example, to update a connection status indicator in your UI.
It would be normal for a client to briefly lose the connection and restore it
with quick connected → reconnecting → connected status jumps.
If you’d like to let users know that there may be connectivity issues, don’t use
this API, but instead refer to Room.subscribe("lost-connection") which was
specially built for this purpose.
Do not use this API to detect when Storage or Presence are initialized or
loaded. "Connected" does not guarantee that Storage or Presence are ready. To
detect when Storage is loaded, rely on awaiting the Room.getStorage
promise or using the Room.subscribe("storage-status") event.
Room.subscribe("lost-connection")
A special-purpose event that will fire when a previously connected Liveblocks client has lost connection, for example due to a network outage, and was unable to recover quickly. This event is designed to help improve UX for your users, and will not trigger on short interruptions, those that are less than 5 seconds by default. The event only triggers if a previously connected client disconnects.
- unsubscribe() => void
Unsubscribe function. Call it to cancel the subscription.
- eventType"lost-connection"Required
Listen to lost connection events.
- callbackRequired
Function that’s called when a room’s lost connection event has been triggered. It can return one of three values:
"lost"A connection has been lost for longer thanlostConnectionTimeout."restored"The connection has been restored again."failed"The room has been unable to reconnect again, and is no longer trying. This may happen if a user’s network has recovered, but the room’s authentication values no longer allow them to enter.
When to use lost connection events
Lost connections events allows you to build high-quality UIs by warning your users that the application is still trying to re-establish the connection, for example through a toast notification. You may want to take extra care in the mean time to ensure their changes won’t go unsaved, or to help them understand why they’re not seeing updates made by others yet.
When this happens, this callback is called with the event lost. Then, once the
connection restores, the callback will be called with the value restored. If
the connection could definitively not be restored, it will be called with
failed (uncommon).
Setting lost connection timeout
The lostConnectionTimeout configuration option will determine how quickly
the event triggers after a connection loss occurs. By default, it’s set to
5000ms, which is 5 seconds.
Room.subscribe("error")
Subscribe to unrecoverable room connection errors. This event will be emitted
immediately before the client disconnects and won’t try reconnecting again.
Returns an unsubscribe function. If you’d like to retry connecting, call
room.reconnect.
- unsubscribe() => void
Unsubscribe function. Call it to cancel the subscription.
- eventType"error"Required
Listen to error events.
- callbackRequired
Function that’s called when an unrecoverable error event has been triggered.
error.codecan return one of these values:-1Authentication error.4001Could not connect because you don’t have access to this room.4005Could not connect because room was full.4006The room ID has changed.
When to use error events
You can use this event to trigger a “Not allowed” screen/dialog. It can also be helpful for implementing a redirect to another page.
When a room ID has changed
When a room ID has been changed with
liveblocks.updateRoomId
or the
Update Room ID API,
error.message will contain the new room ID.
Room.subscribe("history")
Subscribe to the current user’s history changes. Returns an unsubscribe function.
- unsubscribe() => void
Unsubscribe function. Call it to cancel the subscription.
- eventType"history"Required
Listen to history events.
- callbackRequired
Room.subscribe("storage-status")
Subscribe to Storage status changes. Use this to tell whether Storage has been synchronized with the Liveblocks servers. Returns an unsubscribe function.
- unsubscribe() => void
Unsubscribe function. Call it to cancel the subscription.
- eventType"storage-status"Required
Listen to Storage status events.
- callbackRequired
Function that’s called when the current user’s Storage updated status have changed.
statuscan be one of four types."not-loaded- Storage has not been loaded yet as [getStorage][] has not been called."loading"- Storage is currently loading for the first time."synchronizing"- Local Storage changes are currently being synchronized."synchronized"- Local Storage changes have been synchronized
Room.batch
Batches Storage and Presence modifications made during the given function. Each modification is grouped together, which means that other clients receive the changes as a single message after the batch function has run. When undoing or redoing these changes, the entire batch will be undone/redone together instead of atomically.
- returnT
Returns the return value from the callback.
- callback() => TRequired
A callback containing every Storage and Presence notification that will be part of the batch. Cannot be an
asyncfunction.
When to batch updates
For the most part, you don’t need to batch updates. For example, given a whiteboard application, it’s perfectly fine to update a note’s position on the board multiple times per second, in separate updates. However, should you implement a “Delete all” button, that may delete 50 notes at once, this is where you should use a batch.
This batch places each
LiveMap.delete call
into a single WebSocket update, instead of sending multiple updates. This will
be much quicker.
Batching groups history changes
Batching changes will also group changes into a single history state.
Doesn’t work with async functions
Note that room.batch cannot take an async function.
Room.history
Room’s history contains functions that let you undo and redo operations made to Storage and Presence on the current client. Each user has a separate history stored in memory, and history is reset when the page is reloaded.
Note that to undo or redo in Yjs, you must use a separate history manager,
Y.UndoManager.
Add Presence to history
By default, history is only enabled for Storage. However, you can use the
addToHistory option to additionally
add Presence state to history.
Room.history.undo
Reverts the last operation. It does not impact operations made by other clients, and will only undo changes made by the current client.
Room.history.redo
Restores the last undone operation. It does not impact operations made by other clients, and will only restore changes made by the current client.
Room.history.canUndo
Returns true or false, depending on whether there are any operations to undo. Helpful for disabling undo buttons.
- canUndoboolean
Whether there is an undo operation in the current history stack.
Room.history.canRedo
Returns true or false, depending on whether there are any operations to redo. Helpful for disabling redo buttons.
- canRedoboolean
Whether there is a redo operation in the current history stack.
Room.history.clear
Clears the undo and redo stacks for the current client. Explicitly clearing history resets the ability to undo beyond the current document state. Other clients’ histories are unaffected.
Room.history.pause
All future modifications made on the Room will be merged together to create a single history item until resume is called.
Room.history.resume
Resumes history after a pause. Modifications made on the Room are not merged into a single history item any more.
Room.history.disable
Executes a callback with history tracking temporarily disabled. Any storage mutations made inside the callback will be applied normally but will not appear on the undo/redo stacks. This is useful for background writes that should not be undoable, such as writing back results from agent updates or reconciling state from an external source.
If the callback throws, the undo/redo stacks are left unchanged (as if the callback never ran).
Batching
When combining with room.batch, always place the batch inside
the disable call. If batch wraps disable, the batched mutations will still
end up on the undo stack.
Async
The history API is synchronous. For long-running async tasks, call
history.disable at each synchronous step rather than wrapping the entire async
function:
- returnT
The return value of the callback.
- fn() => TRequired
The callback to execute while history is disabled.
Room.connect
Connect the local room instance to the Liveblocks server. Does nothing if the room is already connecting, reconnecting or connected. We don’t recommend using this API directly.
Room.reconnect
Reconnect the local room instance to the Liveblocks server, using a new WebSocket connection.
Room.disconnect
Disconnect the local room instance from the Liveblocks server. The room instance will remain functional (for example, it will still allow local presence or storage mutations), but since it’s no longer connected, changes will not be persisted or synchronized until the room instance is reconnected again. We don’t recommend using this API directly.
Comments
Room.getThreads
Returns threads, and their associated inbox notifications and subscriptions, that are in the current room. It also returns the request date that can be used for subsequent polling. It’s possible to filter for a thread’s resolved status and using custom metadata.
- threadsThreadData[]
Threads within the current room.
- inboxNotificationsInboxNotificationData[]
Inbox notifications associated with the threads.
- subscriptionsSubscriptionData[]
Subscriptions associated with the threads.
- requestedAtDate
The request date to use for subsequent polling.
- resolvedboolean
Only return
resolvedorunresolvedthreads. Learn more. - visibility"public" | "private"
Only return
publicorprivatethreads. Permissions are taken into account so users without access to private threads won’t receive them. Learn more. - subscribedboolean
Only return
subscribedorunsubscribedthreads. Learn more. - metadataPartial<ThreadMetadata>
Only return threads containing the custom metadata. Metadata is set yourself when creating a thread, for example
{ priority: "HIGH" }. Learn more.
Filtering resolved status
You can filter threads by those that are resolved, or unresolved, by passing a
boolean to query.resolved.
Filtering visibility
You can filter threads based on visibility by passing "public" or "private"
to query.visibility.
Permissions are taken into account, for example querying private threads returns no threads if the current user does not have access to private threads.
Filtering subscribed status
You can filter threads by those that the user is subscribed to, or not, by
passing a boolean to query.subscribed.
Filtering metadata
You can define custom metadata when
creating a thread,
and the query.metadata option allows you to return only threads that match.
You can also filter for metadata that begins with a specific string.
You can also filter for metadata using numeric operators.
Room.getThreadsSince
Returns threads, and their associated inbox notifications and subscriptions,
that have been updated or deleted since the requested date. Helpful when used in
combination with Room.getThreads to initially fetch all
threads, then receive updates later.
- threads
Threads that have been updated or deleted since the requested date.
- inboxNotifications
Inbox notifications that have been updated or deleted since the requested date.
- subscriptions
Subscriptions that have been updated or deleted since the requested date.
- requestedAtDate
The request date to use for subsequent polling.
- sinceDateRequired
Only return threads that have been updated or deleted after this date.
Room.getThread
Returns a thread and its associated inbox notification and subscription, from its ID, if it exists.
The thread ID can be retrieved from existing threads.
- threadThreadData | undefined
The requested thread, or
undefinedif it doesn’t exist. - inboxNotificationInboxNotificationThreadData | undefined
The inbox notification associated with the thread, or
undefinedif it doesn’t exist. - subscriptionSubscriptionData | undefined
The subscription associated with the thread, or
undefinedif it doesn’t exist.
- valuestringRequired
The ID of the thread you want to retrieve.
Room.createThread
Creates a thread, and its initial comment, in the current room. A comment’s body is an array of paragraphs, each containing child nodes, learn more under creating thread content.
- valueThreadData
The thread that has been created.
- bodyCommentBodyRequired
The content of the comment, see creating thread content.
- attachmentIdsstring[]
The IDs of the comment’s attachments.
- commentMetadataCommentMetadata
Custom metadata to be attached to the initial comment, see defining comment metadata.
- metadataThreadMetadata
Custom metadata to be attached to the thread, see defining thread metadata.
- visibility"public" | "private"Default is "public"
Whether to create a public or private thread. Permissions are taken into account so a user without write access to private threads can’t create a private thread.
Creating thread content
A comment’s body is an array of paragraphs, each containing child nodes. Here’s
an example of how to construct the following simple comment body, which can be
passed to room.createThread.
Hello world
Second paragraph!
It’s also possible to create links and mentions.
@Jody Hekla the Liveblocks website is cool!
Defining thread metadata
Custom metadata can be attached to each thread. string, number, and
boolean properties are allowed.
Creating private threads
Threads are public by default. To create a private thread, pass
visibility: "private".
Permissions are taken into account when threads are created and retrieved. A
user without write access to private threads can’t create a private thread, and
users without read access to private threads won’t receive private threads from
room.getThreads or room.getThread.
Private threads are only available on Team and Enterprise plans.
Room.deleteThread
Deletes a thread by its ID.
- threadIdstringRequired
The ID of the thread to delete.
Room.editThreadMetadata
Edits a thread’s custom metadata. Metadata can be a string, number, or
boolean. To delete an existing metadata property, set its value to null.
- metadataThreadMetadata
The thread metadata.
- threadIdstringRequired
The ID of the thread.
- metadataPatchable<ThreadMetadata>Required
An object containing the metadata properties to update. Metadata can be a
string,number, orboolean. To delete an existing metadata property, set its value tonull.
Room.markThreadAsResolved
Marks a thread as resolved.
- threadIdstringRequired
The ID of the thread to resolve.
Room.markThreadAsUnresolved
Marks a thread as unresolved.
- threadIdstringRequired
The ID of the thread to resolve.
Room.subscribeToThread
Subscribes the user to a thread, meaning they will receive inbox notifications when new comments are posted.
- valueSubscriptionData
The thread’s subscription.
- threadIdstringRequired
The ID of the thread to subscribe to.
Replacing room-level subscriptions
Subscribing will replace any existing subscription for the current thread set at room-level. This value can also be overridden by a room-level call that is run afterwards.
Room.unsubscribeFromThread
Unsubscribes the user from a thread, meaning they will no longer receive inbox notifications when new comments are posted.
- threadIdstringRequired
The ID of the thread to unsubscribe from.
Replacing room-level unsubscriptions
Unsubscribing will replace any existing unsubscription for the current thread set at room-level. This value can also be overridden by a room-level call that is run afterwards.
Room.createComment
Creates a comment in a given thread.
- valueCommentData
The comment that has been created.
- threadIdstringRequired
The ID of the thread that the comment will be added to.
- bodyCommentBodyRequired
The content of the comment, see creating comment content.
- attachmentIdsstring[]
The IDs of the comment’s attachments.
- metadataCommentMetadata
Custom metadata to be attached to the comment, see defining comment metadata.
Creating comment content
A comment’s body is an array of paragraphs, each containing child nodes. Here’s
an example of how to construct the following simple comment body, which can be
passed to room.createComment.
Hello world
Second paragraph!
It’s also possible to create links and mentions.
@Jody Hekla the Liveblocks website is cool!