API Reference@liveblocks/react

@liveblocks/react provides you with React bindings for our realtime collaboration APIs, built on top of WebSockets. Read our getting started guides to learn more.

Suspense

All Liveblocks React components and hooks can be exported from two different locations, @liveblocks/react/suspense and @liveblocks/react. This is because Liveblocks provides two types of hooks; those that support React Suspense, and those that don’t.

// Import the Suspense hookimport { useThreads } from "@/liveblocks/react/suspense";
// Import the regular hookimport { useThreads } from "@/liveblocks/react";

We recommend importing from @liveblocks/react/suspense and using Suspense by default, as it often makes it easier to build your collaborative application.

Suspense hooks

Suspense hooks can be wrapped in ClientSideSuspense, which acts as a loading spinner for any components below it. When using this, all components below will only render once their hook contents have been loaded.

import { ClientSideSuspense, useStorage } from "@liveblocks/react/suspense";
function App() { <ClientSideSuspense fallback={<div>Loading…</div>}> <Component /> </ClientSideSuspense>;}
function Component() { // `animals` is always defined const animals = useStorage((root) => root.animals);
// ...}

Advanced hooks using the { ..., error, isLoading } syntax, such as useThreads, can also use ErrorBoundary to render an error if the hook runs into a problem.

import { ClientSideSuspense, useThreads } from "@liveblocks/react/suspense";import { ErrorBoundary } from "react-error-boundary";
function App() { return ( <ErrorBoundary fallback={<div>Error</div>}> <ClientSideSuspense fallback={<div>Loading…</div>}> <Component /> </ClientSideSuspense> </ErrorBoundary> );}
function Component() { // `threads` is always defined const { threads } = useThreads();
// ...}

An advantage of Suspense hooks is that you can have multiple different hooks in your tree, and you only need a single ClientSideSuspense component to render a loading spinner for all of them.

Regular hooks

Regular hooks often return null whilst a component is loading, and you must check for this to render a loading spinner.

import { useStorage } from "@liveblocks/react";
function Component() { // `animals` is `null` when loading const animals = useStorage((root) => root.animals);
if (!animals) { return <div>Loading…</div>; }
// ...}

Advanced hooks using the { ..., error, isLoading } syntax, such as useThreads, require you to make sure there isn’t a problem before using the data.

import { useThreads } from "@liveblocks/react";
function Component() { // Check for `error` and `isLoading` before `threads` is defined const { threads, error, isLoading } = useThreads();
if (error) { return <div>Error</div>; }
if (isLoading) { return <div>Loading…</div>; }
// ...}

ClientSideSuspense

Liveblocks provides a component named ClientSideSuspense which works as a replacement for Suspense. This is helpful as our Suspense hooks will throw an error when they’re run on the server, and this component avoids this issue by always rendering the fallback on the server.

import { ClientSideSuspense } from "@liveblocks/react/suspense";
function Page() { return ( <LiveblocksProvider authEndpoint="/api/liveblocks-auth"> <RoomProvider id="my-room-id"> <ClientSideSuspense fallback={<div>Loading…</div>}> <App /> </ClientSideSuspense> </RoomProvider> </LiveblocksProvider> );}

Loading spinners

Instead of wrapping your entire Liveblocks application inside a single ClientSideSuspense component, you can use multiple of these components in different parts of your application, and each will work as a loading fallback for any components further down your tree.

import { ClientSideSuspense } from "@liveblocks/react/suspense";
function Page() { return ( <LiveblocksProvider authEndpoint="/api/liveblocks-auth"> <RoomProvider id="my-room-id"> <header>My title</header>
<main> <ClientSideSuspense fallback={<div>Loading…</div>}> <Canvas /> </ClientSideSuspense> </main>
<aside> <ClientSideSuspense fallback={<div>Loading…</div>}> <LiveAvatars /> </ClientSideSuspense> </aside> </RoomProvider> </LiveblocksProvider> );}

This is a great way to build a static skeleton around your dynamic collaborative application.

Liveblocks

LiveblocksProvider

Sets up a client for connecting to Liveblocks, and is the recommended way to do this for React apps. You must define either authEndpoint or publicApiKey. Resolver functions should be placed inside here, and a number of other options are available, which correspond with those passed to createClient. Unlike RoomProvider, LiveblocksProvider doesn’t call Liveblocks servers when mounted, and it should be placed higher in your app’s component tree.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider // publicApiKey="" // authEndpoint="/api/liveblocks-auth" > {/* children */} </LiveblocksProvider> );}
Props
  • 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 authEndpoint or publicApikey are 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 authEndpoint or publicApikey are required. Learn more.

  • throttlenumberDefault is 100

    The throttle time between WebSocket messages in milliseconds, a number between 16 and 1000 is allowed. Using 16 means 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. Return an array of UserMeta["info"] objects in the same order they arrived. Learn more.

  • resolveRoomsInfo

    A function that resolves room information in Comments. Return an array of RoomInfo objects in the same order they arrived. Learn more.

  • resolveMentionSuggestions

    A function that resolves mention suggestions in Comments. Return an array of user IDs. Learn more.

  • polyfills

    Place polyfills for atob, fetch, and WebSocket inside here. Useful when using a non-browser environment, such as Node.js or React Native.

  • unstable_fallbackToHTTPbooleanDefault is false

    Experimental. Automatically fall back to HTTP when a message is too large for WebSockets.

  • unstable_streamDatabooleanDefault is false

    Experimental. Stream the initial Storage content over HTTP, instead of waiting for a large initial WebSocket message to be sent from the server.

LiveblocksProvider 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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider publicApiKey={""}> {/* children */} </LiveblocksProvider> );}

LiveblocksProvider 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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider authEndpoint="/api/liveblocks-auth"> {/* children */} </LiveblocksProvider> );}

LiveblocksProvider 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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider authEndpoint={async (room) => { // Fetch your authentication endpoint and retrieve your access or ID token // ...
return { token: "..." }; }} > {/* children */} </LiveblocksProvider> );}

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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider authEndpoint={async (room) => { const response = await fetch("/api/liveblocks-auth", { method: "POST", headers: { Authentication: "<your own headers here>", "Content-Type": "application/json", }, // Don't forget to pass `room` down. Note that it // can be undefined when using Notifications. body: JSON.stringify({ room }), }); return await response.json(); }} > {/* children */} </LiveblocksProvider> );}
Token details

You should return the token created with Liveblocks.prepareSession or liveblocks.identifyUser. These are the values the functions can return.

  1. A valid token, it returns a { "token": "..." } shaped response.
  2. 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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider throttle={16}
// Other options // ... > {/* children */} </LiveblocksProvider> );}

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.

throttle:  16, // 60 FPSthrottle:  32, // 30 FPSthrottle: 200, //  5 FPS

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.

function Page() {  return (    <LiveblocksProvider      preventUnsavedChanges
// Other options // ... > ... </LiveblocksProvider> );}

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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider lostConnectionTimeout={5000}
// Other props // ... > {/* children */} </LiveblocksProvider> );}

You can listen to the event with useLostConnectionListener. 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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider // Disconnect users after 15 minutes of inactivity backgroundKeepAliveTimeout={15 * 60 * 1000}
// Other props // ... > {/* children */} </LiveblocksProvider> );}

backgroundKeepAliveTimeout accepts a number in milliseconds—we advise using a value of at least a few minutes, to avoid unnecessary disconnections.

resolveUsers

Comments stores user IDs in its system, but no other user information. To display user information in Comments 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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider resolveUsers={async ({ userIds }) => { const usersData = await (userIds);
return usersData.map((userData) => ({ name: userData.name, avatar: userData.avatar.src, })); }}
// Other props // ... > {/* children */} </LiveblocksProvider> );}

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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider resolveUsers={async ({ userIds }) => { // ["marc@example.com", "nimesh@example.com"]; console.log(userIds);
return [ { name: "Marc", avatar: "https://example.com/marc.png" }, { name: "Nimesh", avatar: "https://example.com/nimesh.png" }, ]; }}
// Other props // ... > {/* children */} </LiveblocksProvider> );}

You can also return custom information, for example, a user’s color:

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider resolveUsers={async ({ userIds }) => { // ["marc@example.com"]; console.log(userIds);
return [ { name: "Marc", avatar: "https://example.com/marc.png", color: "purple", }, ]; }}
// Other props // ... > {/* children */} </LiveblocksProvider> );}
Accessing user data

You can access any values set within resolveUsers with the useUser hook.

import { useUser } from "@liveblocks/react/suspense";
function Component() { const user = useUser("marc@example.com");
// { name: "Marc", avatar: "https://...", ... } console.log(user);}

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 LiveblocksProvider.

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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider resolveRoomsInfo={async ({ roomIds }) => { const documentsData = await (roomIds);
return documentsData.map((documentData) => ({ name: documentData.name, // url: documentData.url, })); }}
// Other props // ... > {/* children */} </LiveblocksProvider> );}

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.

resolveMentionSuggestions

To enable creating mentions in Comments, you can provide a resolver function to the resolveMentionSuggestions option in LiveblocksProvider. These mentions will be displayed in the Composer component.

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.

import { LiveblocksProvider } from "@liveblocks/react/suspense";
function App() { return ( <LiveblocksProvider resolveMentionSuggestions={async ({ text, roomId }) => { const workspaceUsers = await (roomId);
if (!text) { // Show all workspace users by default return (workspaceUsers); } else { const matchingUsers = (workspaceUsers, text); return (matchingUsers); } }}
// Other props // ... > {/* children */} </LiveblocksProvider> );}

LiveblocksProvider 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.

$npm install ws node-fetch

Then, pass them to the LiveblocksProvider polyfill option as below.

import { LiveblocksProvider } from "@liveblocks/react/suspense";import fetch from "node-fetch";import WebSocket from "ws";
function App() { return ( <LiveblocksProvider polyfills={{ fetch, WebSocket, }}
// Other props // ... > {/* children */} </LiveblocksProvider> );}

Note that node-fetch v3+ does not support CommonJS. If you are using CommonJS, downgrade node-fetch to v2.

LiveblocksProvider 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.

$npm install base-64

Then you can pass the decode function to our atob polyfill option when you create the client.

import { LiveblocksProvider } from "@liveblocks/react/suspense";import { decode } from "base-64";
function App() { return ( <LiveblocksProvider polyfills={{ atob: decode, }}
// Other props // ... > {/* children */} </LiveblocksProvider> );}

createLiveblocksContext

Creates a LiveblocksProvider and a set of typed hooks. Note that any LiveblocksProvider created in this way takes no props, because it uses settings from the client instead. We recommend using it in liveblocks.config.ts and re-exporting your typed hooks like below.

While createRoomContext offers APIs for interacting with rooms (e.g. Presence, Storage, and Comments), createLiveblocksContext offers APIs for interacting with Liveblocks features that are not tied to a specific room (e.g. Notifications).

liveblocks.config.ts
import { createClient } from "@liveblocks/client";import { createRoomContext, createLiveblocksContext } from "@liveblocks/react";
const client = createClient({ // publicApiKey: "", // authEndpoint: "/api/liveblocks-auth", // throttle: 100,});
// ...
export const { RoomProvider } = createRoomContext(client);
export const { LiveblocksProvider, useInboxNotifications,
// Other hooks // ...} = createLiveblocksContext(client);

useClient

Returns the client of the nearest LiveblocksProvider above in the React component tree.

import { useClient } from "@liveblocks/react/suspense";
const client = useClient();

Room

RoomProvider

Makes a Room available in the component hierarchy below. Joins the room when the component is mounted, and automatically leaves the room when the component is unmounted. When using Sync Datastore, initial Presence values for each user, and Storage values for the room can be set.

import { RoomProvider } from "@liveblocks/react/suspense";
function App() { return <RoomProvider id="my-room-id">{/* children */}</RoomProvider>;}
Props
  • idstringRequired

    The unique ID for the current room. RoomProvider will join this room when it loads. If the room doesn’t exist already it will automatically create the room first then join. After setting up authentication for your app, it can helpful to decide on a naming pattern for your room IDs.

  • 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. This value is ignored after the first render. Learn more.

  • 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. This value is ignored after the first render, and if Storage for the current room has already been created. Learn more.

  • autoConnectbooleanDefault is true

    Whether the room immediately connects to Liveblocks servers. This value is ignored after the first render.

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.

import { RoomProvider } from "@liveblocks/react/suspense";
function App() { return ( <RoomProvider id="my-room" initialPresence={{ cursor: null, colors: ["red", "purple"], selection: { id: 72426, }, }} > {/* children */} </RoomProvider> );}

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. Note that this value is only read a single time.

import { LiveList, LiveObject, LiveMap } from "@liveblocks/client";import { RoomProvider } from "@liveblocks/react/suspense";
function App() { return ( <RoomProvider id="my-room" initialPresence={} initialStorage={{ title: "Untitled", names: new LiveList(["Steven", "Guillaume"]), shapes: new LiveMap([ ["g9shu0", new LiveObject({ type: "rectangle", color: "red" })], ["djs3g5", new LiveObject({ type: "circle", color: "yellow" })], ]), }} > {/* children */} </RoomProvider> );}

Any conflict-free live structures and JSON-serializable objects are allowed (the LsonObject type).

createRoomContext

Creates a RoomProvider and a set of typed hooks to use in your app. Note that any RoomProvider created in this way does not need to be nested in LiveblocksProvider, as it already has access to the client. We generally recommend typing your app using the newer method instead. When using createRoomContext it can be helpful to use it in liveblocks.config.ts and re-export your typed hooks as below.

liveblocks.config.ts
import { createClient } from "@liveblocks/client";import { createRoomContext } from "@liveblocks/react";
const client = createClient({ // publicApiKey: "", // authEndpoint: "/api/liveblocks-auth",});
type Presence = {};type Storage = {};type UserMeta = {};type RoomEvent = {};type ThreadMetadata = {};
export const { RoomProvider, useMyPresence,
// Other hooks // ...} = createRoomContext<Presence, Storage, UserMeta, RoomEvent, ThreadMetadata>( client);

Suspense with createRoomContext

To use the React suspense version of our hooks with createRoomContext, you can export from the suspense property instead.

liveblocks.config.ts
import { createClient } from "@liveblocks/client";import { createRoomContext } from "@liveblocks/react";
const client = createClient({ // publicApiKey: "", // authEndpoint: "/api/liveblocks-auth",});
type Presence = {};type Storage = {};type UserMeta = {};type RoomEvent = {};type ThreadMetadata = {};
export const { suspense: { RoomProvider, useMyPresence,
// Other suspense hooks // ... },} = createRoomContext<Presence, Storage, UserMeta, RoomEvent, ThreadMetadata>( client);

Typing createRoomContext

To type your hooks, you can pass multiple different types to createRoomContext. A full explanation is in the code snippet below.

useRoom

Returns the Room of the nearest RoomProvider above in the React component tree.

import { useRoom } from "@liveblocks/react/suspense";
const room = useRoom();

useIsInsideRoom

Returns a boolean, true if the hook was called inside a RoomProvider context, and false otherwise.

import { useIsInsideRoom } from "@liveblocks/react/suspense";
const isInsideRoom = useIsInsideRoom();

Displaying different components inside rooms

useIsInsideRoom is helpful for rendering different components depending on whether they’re inside a room, or not. One example is a header component that only displays a live avatar stack when users are connected to the room.

import { useIsInsideRoom, useOthers } from "@liveblocks/react/suspense";
function Header() { const isInsideRoom = useIsInsideRoom();
return ( <div> {isInsideRoom ? <LiveAvatars /> : null} <MyAvatar /> </div> );}
function LiveAvatars() { const others = useOthers(); return others.map((other) => <img src={other.info.picture} />);}

Here’s how the example above would render in three different LiveblocksProvider and RoomProvider contexts.

// 👥👤 Live avatar stack and your avatar<LiveblocksProvider /* ... */>  <RoomProvider /* ... */>    <Header />  </RoomProvider></LiveblocksProvider>

// 👤 Just your avatar<LiveblocksProvider /* ... */> <Header /></LiveblocksProvider>
// 👤 Just your avatar<Header />

useErrorListener

Listen to potential room connection errors.

import { useErrorListener } from "@liveblocks/react/suspense";
useErrorListener((error) => { switch (error.code) { case -1: // Authentication error break;
case 4001: // Could not connect because you don't have access to this room break;
case 4005: // Could not connect because room was full break;
case 4006: // The room ID has changed, get the new room ID (use this for redirecting) const newRoomId = error.message; break;
default: // Unexpected error break; }});

useStatus

Returns the current WebSocket connection status of the room, and will re-render your component whenever it changes.

import { useStatus } from "@liveblocks/react/suspense";
const status = useStatus();

The possible value are: initial, connecting, connected, reconnecting, or disconnected.

useStorageStatus

Returns the current storage status of the room, and will re-render your component whenever it changes. A { smooth: true } option is also available, which prevents quick changes between states, making it ideal for rendering a synchronization badge in your app.

import { useStorageStatus } from "@liveblocks/react";
const storageStatus = useStorageStatus();
// "not-loaded" | "loading" | "synchronizing" | "synchronized"console.log(storageStatus);

👉 A Suspense version of this hook is also available.

import { useStorageStatus } from "@liveblocks/react/suspense";
const storageStatus = useStorageStatus();
// "synchronizing" | "synchronized"console.log(storageStatus);

useSyncStatus

Returns the current synchronization status of Liveblocks, and will re-render your component whenever it changes. This includes any part of Liveblocks that may be synchronizing local changes to the server, including (any room’s) Storage, text editors, threads, or notifications.

A { smooth: true } option is also available, which prevents quick changes between states, making it ideal for rendering a synchronization badge in your app.

import { useSyncStatus } from "@liveblocks/react";
const syncStatus = useSyncStatus();// "not-loaded" | "loading" | "synchronizing" | "synchronized"

👉 A Suspense version of this hook is also available.

import { useSyncStatus } from "@liveblocks/react/suspense";
const syncStatus = useSyncStatus();// "synchronizing" | "synchronized"

Display a synchronization badge

Passing { smooth: true } prevents the status changing from "synchronizing" to "synchronized" until 1 second has passed after the final change. This means it’s ideal for rendering a synchronization status badge, as it won’t flicker in a distracting manner when changes are made in quick succession.

import { useSyncStatus } from "@liveblocks/react/suspense";
function StorageStatusBadge() { const syncStatus = useSyncStatus({ smooth: true });
return <div>{syncStatus === "synchronized" ? "✅ Saved" : "🔄 Saving"}</div>;}

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.

function Page() {  return (    <LiveblocksProvider      preventUnsavedChanges
// Other options // ... > ... </LiveblocksProvider> );}

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.

useOthersListener

Calls the given callback when an “others” event occurs. Possible event types are:

  • enter – A user has entered the room.
  • leave – A user has left the room.
  • reset – The others list has been emptied. This is the first event that occurs when the room is entered. It also occurs when you’ve lost connection to the room.
  • update – A user’s presence data has been updated.
function App() {  useOthersListener(({ type, user, others }) => {    switch (type) {      case "enter":        // `user` has entered the room        break;
case "leave": // `user` has left the room break;
case "update": // Presence for `user` has updated break;
case "reset": // Others list has been emptied break; } });}

useLostConnectionListener

Calls the given callback in the exceptional situation that a connection is lost and reconnecting does not happen quickly enough.

This event allows you to build high-quality UIs by warning your users that the app 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.

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).

The lostConnectionTimeout client option will determine how quickly this event will fire after a connection loss (default: 5 seconds).

import { toast } from "my-preferred-toast-library";
function App() { useLostConnectionListener((event) => { switch (event) { case "lost": toast.warn("Still trying to reconnect..."); break;
case "restored": toast.success("Successfully reconnected again!"); break;
case "failed": toast.error("Could not restore the connection"); break; } });}

Automatically unsubscribes when the component is unmounted.

For a demonstration of this behavior, see our connection status example.

Presence

useMyPresence

Return the presence of the current user, and a function to update it. Automatically subscribes to updates to the current user’s presence.

Note that the updateMyPresence setter function is different to the setter function returned by React’s useState hook. Instead, you can pass a partial presence object to updateMyPresence, and any changes will be merged into the current presence. It will not replace the entire presence object.

import { useMyPresence } from "@liveblocks/react/suspense";
const [myPresence, updateMyPresence] = useMyPresence();updateMyPresence({ x: 0 });updateMyPresence({ y: 0 });
// At the next render, "myPresence" will be equal to "{ x: 0, y: 0 }"

This is roughly equal to:

const myPresence = useSelf((me) => me.presence);const updateMyPresence = useUpdateMyPresence();

updateMyPresence accepts an optional argument to add a new item to the undo/redo stack. See room.history for more information.

updateMyPresence({ selectedId: "xxx" }, { addToHistory: true });

useUpdateMyPresence

Returns a setter function to update the current user’s presence.

Use this if you don’t need the current user’s presence in your component, but you need to update it (e.g. live cursor). It’s better to use useUpdateMyPresence because it won’t subscribe your component to get rerendered when the presence updates.

Note that the updateMyPresence setter function is different to the setter function returned by React’s useState hook. Instead, you can pass a partial presence object to updateMyPresence, and any changes will be merged into the current presence. It will not replace the entire presence object.

import { useUpdateMyPresence } from "@liveblocks/react/suspense";
const updateMyPresence = useUpdateMyPresence();
updateMyPresence({ y: 0 });

updateMyPresence accepts an optional argument to add a new item to the undo/redo stack. See room.history for more information.

updateMyPresence({ selectedId: "xxx" }, { addToHistory: true });

useSelf

Returns the current user once it is connected to the room, and automatically subscribes to updates to the current user.

import { useSelf } from "@liveblocks/react/suspense";
const currentUser = useSelf();// {// connectionId: 1,// presence: { cursor: { x: 27, y: -8 } },// }
const currentUser = useSelf((me) => me.presence.cursor);// { x: 27, y: -8 }

The benefit of using a selector is that it will only update your component if that particular selection changes. For full details, see how selectors work.

👉 A Suspense version of this hook is also available, which will never return null.

useOthers

Extracts data from the list of other users currently in the same Room, and automatically subscribes to updates on the selected data. For full details, see how selectors work.

The others argument to the useOthers selector function is an immutable array of Users.

// ✅ Rerenders only if the number of users changesconst numOthers = useOthers((others) => others.length);
// ✅ Rerenders only if someone starts or stops typingconst isSomeoneTyping = useOthers((others) => others.some((other) => other.presence.isTyping));
// ✅ Rerenders only if actively typing users are updatedconst typingUsers = useOthers( (others) => others.filter((other) => other.presence.isTyping), shallow // 👈);

👉 A Suspense version of this hook is also available, which will never return null.

One caveat with this API is that selecting a subset of data for each user quickly becomes tricky. When you want to select and get updates for only a particular subset of each user’s data, we recommend using the useOthersMapped hook instead, which is optimized for this use case.

// ❌ Mapping is hard to get right with this hookconst cursors = useOthers(  (others) => others.map((other) => other.presence.cursor),  shallow);
// ✅ Better to use useOthersMappedconst cursors = useOthersMapped((other) => other.presence.cursor);

When called without arguments, returns the user list and updates your component whenever anything in it changes. This might be way more often than you want!

const others = useOthers(); // ⚠️ Caution, might rerender often!// [//   { connectionId: 2, presence: { cursor: { x: 27, y: -8 } } },//   { connectionId: 3, presence: { cursor: { x: 0, y: 19 } } },// ]

useOthersMapped

Extract data using a selector for every user in the room, and subscribe to all changes to the selected data. A Suspense version of this hook is also available.

The key difference with useOthers is that the selector (and the optional comparison function) work at the item level, like doing a .map() over the others array.

// Example 1const others = useOthersMapped((other) => other.presence.cursor);// [//   [2, { x: 27, y: -8 }],//   [3, { x: 0, y: 19 }],// ]
// Example 2const others = useOthersMapped( (other) => ({ avatar: other.info.avatar, isTyping: other.presence.isTyping, }), shallow // 👈);
// [// [2, { avatar: 'https://...', isTyping: true }],// [3, { avatar: null, isTyping: false }],// ]

Returns an array where each item is a pair of [connectionId, data]. For pragmatic reasons, the results are keyed by the connectionId, because in most cases you’ll want to iterate over the results and draw some UI for each, which in React requires you to use a key={connectionId} prop.

const others = useOthersMapped((other) => other.presence.cursor);
// In JSXreturn ( <> {others.map(([connectionId, cursor]) => ( <Cursor key={connectionId} x={cursor.x} y={cursor.y} /> ))} </>);

useOthersConnectionIds

Returns an array of connection IDs (numbers), and rerenders automatically when users join or leave. This hook is useful in particular in combination with the useOther (singular) hook, to implement high-frequency rerendering of components for each user in the room, e.g. cursors. See the useOther (singular) documentation below for a full usage example.

useOthersConnectionIds(); // [2, 4, 7]

Roughly equivalent to:

useOthers((others) => others.map((other) => other.connectionId), shallow);

👉 A Suspense version of this hook is also available.

useOther

Extract data using a selector for one specific user in the room, and subscribe to all changes to the selected data. A Suspense version of this hook is also available.

// ✅ Rerenders when this specific user’s isTyping changes (but not when their cursor changes)const isTyping = useOther(  3, // User with connectionId 3  (user) => user.presence.isTyping);

The reason this hook exists is to enable the most efficient rerendering model for high-frequency updates to other’s presences, which is the following structure:

Cursors.tsx
const Cursors =  // (1) Wrap parent component in a memo and make sure it takes no props  React.memo(function () {    const othersConnectionIds = useOthersConnectionIds(); // (2)    return (      <>        {othersConnectionIds.map((connectionId) => (          <Cursor            key={connectionId} // (3)            connectionId={connectionId}          />        ))}      </>    );  });
Cursor.tsx
function Cursor({ connectionId }) {  const { x, y } = useOther(connectionId, (other) => other.presence.cursor); // (4)  return <Cursor x={x} y={y} />;}
  1. Makes sure this whole component tree will never rerender beyond the first time.
  2. Makes sure the parent component only rerenders when users join/leave.
  3. Makes sure each cursor remains associated to the same connection.
  4. Makes sure each cursor rerenders whenever its data changes only.

👉 A Suspense version of this hook is also available, which will never return null.

Broadcast

useBroadcastEvent

Returns a callback that lets you broadcast custom events to other users in the room.

import { useBroadcastEvent } from "@liveblocks/react/suspense";
// On client Aconst broadcast = useBroadcastEvent();broadcast({ type: "EMOJI", emoji: "🔥" });
// On client BuseEventListener(({ event, user, connectionId }) => { // ^^^^ Will be Client A if (event.type === "EMOJI") { // Do something }});

useEventListener

Listen to custom events sent by other people in the room via useBroadcastEvent. Provides the event along with the connectionId of the user that sent the message. If an event was sent from the Broadcast to a room REST API, connectionId will be -1.

import { useEventListener } from "@liveblocks/react/suspense";
// On client Aconst broadcast = useBroadcastEvent();broadcast({ type: "EMOJI", emoji: "🔥" });
// On client BuseEventListener(({ event, user, connectionId }) => { // ^^^^ Will be Client A if (event.type === "EMOJI") { // Do something }});

The user property will indicate which User instance sent the message. This will typically be equal to one of the others in the room, but it can also be null in case this event was broadcasted from the server, using the Broadcast Event API.

Automatically unsubscribes when the component is unmounted.

Storage

Each room contains Storage, a conflict-free data store that multiple users can edit at the same time. When users make edits simultaneously, conflicts are resolved automatically, and each user will see the same state. Storage is ideal for storing permanent document state, such as shapes on a canvas, notes on a whiteboard, or cells in a spreadsheet.

Data structures

Storage provides three different conflict-free data structures, which you can use to build your application. All structures are permanent and persist when all users have left the room, unlike Presence which is temporary.

  • LiveObject - Similar to JavaScript object. Use this for storing records with fixed key names and where the values don’t necessarily have the same types. For example, a Person with a name: string and an age: number field. If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.

  • LiveList - An ordered collection of items synchronized across clients. Even if multiple users add/remove/move elements simultaneously, LiveList will solve the conflicts to ensure everyone sees the same collection of items.

  • LiveMap - Similar to a JavaScript Map. Use this for indexing values that all have the same structure. For example, to store an index of Person values by their name. If multiple users update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.

Typing Storage

To type the Storage values you receive, make sure to set your Storage type.

import { LiveList } from "@liveblocks/client";
declare global { interface Liveblocks { Storage: { animals: LiveList<string>; }; }}

You can then set an initial value in RoomProvider.

import { LiveList } from "@liveblocks/client";import { RoomProvider } from "@liveblocks/react/suspense";
function App() { return ( <RoomProvider id="my-room-name" initialStorage={{ animals: new LiveList(["Fido"]) }} > {/* children */} </RoomProvider> );}

The type received in your Storage will match the type passed. Learn more under typing your data.

import { useMutation } from "@liveblocks/react/suspense";
function App() { const addAnimal = useMutation(({ storage }) => { const animals = storage.get("animals");
// LiveList<["Fido"]> console.log(animals);
animals.push("Felix");
// LiveList<["Fido", "Felix"]> console.log(animals); });
return <button onClick={addAnimal}>Add animal</button>;}

useStorage will return an immutable copy of the data, for example a LiveList is converted to an array, which makes it easy to render.

import { useStorage } from "@liveblocks/react/suspense";
function App() { const animals = useStorage((root) => root.animals);
// ["Fido", "Felix"] console.log(animals);
return ( <ul> {animals.map((animal) => ( <li key={animal}>{animal}</li> ))} </ul> );}

Nesting data structures

All Storage data structures can be nested, allowing you to create complex trees of conflict-free data.

import { LiveObject, LiveList, LiveMap } from "@liveblocks/client";
type Person = LiveObject<{ name: string; pets: LiveList<string>;}>;
declare global { interface Liveblocks { Storage: { people: LiveMap<string, Person>; }; }}

Here’s an example of setting initialStorage for this type.

import { LiveObject, LiveList, LiveMap } from "@liveblocks/client";import { RoomProvider } from "@liveblocks/react/suspense";
function App() { return ( <RoomProvider id="my-room-name" initialStorage={{ people: new LiveMap([ [ "alicia", new LiveObject({ name: "Alicia", pets: new LiveList(["Fido", "Felix"]), }), ], ]), }} > {/* children */} </RoomProvider> );}

useStorage

Extracts data from Liveblocks Storage state and automatically subscribes to updates to that selected data. For full details, see how selectors work.

// ✅ Rerenders if todos (or their children) changeconst items = useStorage((root) => root.todos);
// ✅ Rerenders when todos are added or deletedconst numTodos = useStorage((root) => root.todos.length);
// ✅ Rerenders when the value of allDone changesconst allDone = useStorage((root) => root.todos.every((item) => item.done));
// ✅ Rerenders if any _unchecked_ todo items changeconst uncheckedItems = useStorage( (root) => root.todos.filter((item) => !item.done), shallow // 👈);

The root argument to the useStorage selector function is an immutable copy of your entire Liveblocks Storage tree. Think of it as the value you provided in the initialStorage prop at the RoomProvider level, but then (recursively) converted to their “normal” JavaScript equivalents (objects, arrays, maps) that are read-only.

From that immutable root, you can select or compute any value you like. Your component will automatically get rerendered if the value you return differs from the last rendered value.

This hook returns null while storage is still loading. To avoid that, use the Suspense version.

useBatch

Returns a function that 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. Every modification made during the batch is merged into a single history item (undo/redo).

import { useBatch } from "@liveblocks/react/suspense";
const batch = useBatch();batch(() => { // All modifications made in this callback are batched});

Note that batch cannot take an async function.

// ❌ Won't workbatch(async () => /* ... */);
// ✅ Will workbatch(() => /*... */);

useHistory

Returns the room’s history. See Room.history for more information.

import { useHistory } from "@liveblocks/react/suspense";
const { undo, redo, pause, resume } = useHistory();

useUndo

Returns a function that undoes the last operation executed by the current client. It does not impact operations made by other clients.

import { useUndo } from "@liveblocks/react/suspense";
const undo = useUndo();

useRedo

Returns a function that redoes the last operation executed by the current client. It does not impact operations made by other clients.

import { useRedo } from "@liveblocks/react/suspense";
const redo = useRedo();

useCanUndo

Returns whether there are any operations to undo.

import { useCanUndo, useUpdateMyPresence } from "@liveblocks/react/suspense";
const updateMyPresence = useUpdateMyPresence();const canUndo = useCanUndo();
updateMyPresence({ y: 0 });
// At the next render, "canUndo" will be true

useCanRedo

Returns whether there are any operations to redo.

import {  useCanRedo,  useUndo,  useUpdateMyPresence,} from "@liveblocks/react/suspense";
const updateMyPresence = useUpdateMyPresence();const undo = useUndo();const canRedo = useCanRedo();
updateMyPresence({ y: 0 });undo();
// At the next render, "canRedo" will be true

useMutation

Creates a callback function that lets you mutate Liveblocks state.

import { useMutation } from "@liveblocks/react/suspense";
const fillWithRed = useMutation( // Mutation context is passed as the first argument ({ storage, setMyPresence }) => { // Mutate Storage storage.get("shapes").get("circle1").set("fill", "red"); // ^^^
// ...or Presence setMyPresence({ lastUsedColor: "red" }); }, []);
// JSXreturn <button onClick={fillWithRed} />;

To make the example above more flexible and work with any color, you have two options:

  1. Close over a local variable and adding it to the dependency array, or
  2. Have it take an extra callback parameter.

Both are equally fine, just a matter of preference.

With dependency arrays

// Local state maintained outside Liveblocksconst [currentColor, setCurrentColor] = useState("red");
const fillWithCurrentColor = useMutation( ({ storage, setMyPresence }) => { storage.get("shapes").get("circle1").set("fill", currentColor); setMyPresence({ lastUsedColor: currentColor }); }, [currentColor] // Works just like it would in useCallback);
// JSXreturn <button onClick={fillWithCurrentColor} />;

With extra callback parameters

Alternatively, you can add extra parameters to your callback function:

const fill = useMutation(  // Note the second argument  ({ storage, setMyPresence }, color: string) => {    storage.get("shapes").get("circle1").set("fill", color);    setMyPresence({ lastUsedColor: color });  },  []);
// JSXreturn <button onClick={() => fill("red")} />;// ^^^^^^^^^^^ Now fill takes a color argument

Depending on current presence

For convenience, the mutation context also receives self and others arguments, which are immutable values reflecting the current Presence state, in case your mutation depends on it.

For example, here’s a mutation that will delete all the shapes selected by the current user.

const deleteSelectedShapes = useMutation(  // You can use current "self" or "others" state in the mutation  ({ storage, self, others, setMyPresence }) => {    // Delete the selected shapes    const shapes = storage.get("shapes");    for (const shapeId of self.presence.selectedShapeIds) {      shapes.delete(shapeId);    }
// Clear the current selection setMyPresence({ selectedShapeIds: [] }); }, []);
// JSXreturn <button onClick={deleteSelectedShapes} />;

Mutations are automatically batched, so when using useMutation there’s no need to use useBatch, or call room.batch() manually.

ESLint rule

If you are using ESLint in your project, and are using the React hooks plugin, we recommend to add a check for "additional hooks", so that it will also check the dependency arrays of your useMutation calls:

{  "rules": {    // ...    "react-hooks/exhaustive-deps": ["warn", {      "additionalHooks": "useMutation"    }]  }}

Comments

useThreads

Returns the threads within the current room.

import { useThreads } from "@liveblocks/react/suspense";
const { threads, error, isLoading } = useThreads();

useThreads supports a few options as its first argument, one is query and it can be used to filter threads based on their metadata or resolved status.

// Returns threads that match the entire `query`, e.g. { color: "blue", pinned: true, ... }const { threads } = useThreads({  query: {    // Filter for unresolved threads    resolved: false,
metadata: { // Filter for threads that contain specific string, boolean, and number data color: "blue", pinned: true, priority: 3,
// Filter for threads with string metadata that starts with a certain value organization: { startsWith: "liveblocks:", }, }, },});

Another option is scrollOnLoad, which is enabled by default. When enabled, if the URL’s hash is set to a comment ID (e.g. https://example.com/my-room#cm_xxx), the page will scroll to that comment once the threads are loaded.

By default, the useThreads hook returns up to 50 threads. To fetch more, the hook (along with useInboxNotifications) provides additional fields for pagination.

import { useThreads } from "@liveblocks/react";
const { isLoading, error, threads,
// Available in Liveblocks 2.9 fetchMore, isFetchingMore, hasFetchedAll, fetchMoreError,} = useThreads({ query });

The hasFetchedAll field indicates whether all available threads have been fetched since the initial load. To load more threads, you can call the fetchMore function. This function is safe to call even if no more threads are available or a fetch is already in progress—it simply won't do anything in that case. Therefore, you don't need to check hasFetchedAll or isFetchingMore before calling fetchMore.

The following example demonstrates how to use the fetchMore function to implement a "Load More" button, which fetches additional threads when clicked. The button is disabled while fetching is in progress.

<div>  {threads.map((thread) => (    <Thread key={thread.id} />  ))}
{hasFetchedAll ? ( <div>🎉 You're all caught up!</div> ) : ( <button disabled={isFetchingMore} onClick={fetchMore}> Load more </button> )}</div>

Error handling is another important aspect to consider when using the useThreads hook. The error and fetchMoreError fields provide information about any errors that occurred during the initial fetch or subsequent fetch operations, respectively. You can use these fields to display appropriate error messages to the user and implement retry mechanisms if needed.

The following example shows how to display error messages for both initial loading errors and errors that occur when fetching more threads.

import { useThreads } from "@liveblocks/react";
const { threads, error, fetchMore, fetchMoreError } = useThreads();
// Handle error if the initial load failed.// The `error` field is not returned by the Suspense hook as the error is thrown to nearest ErrorBoundaryif (error) { return ( <div> <p>Error loading threads: {error.message}</p> </div> );}
return ( <div> {threads.map((thread) => ( <Thread key={thread.id}>...</Thread> ))}
{fetchMoreError && ( <div> <p>Error loading more threads: {fetchMoreError.message}</p> <button onClick={fetchMore}>Retry</button> </div> )} </div>);

👉 A Suspense version of this hook is also available, which will never return a loading state and will throw error to the nearest ErrorBoundary if initial load failed.

useThreadSubscription

Returns the subscription status of a thread.

import { useThreadSubscription } from "@liveblocks/react/suspense";
const { status, unreadSince } = useThreadSubscription("th_xxx");

useCreateThread

Returns a function that creates a thread with an initial comment, and optionally some metadata.

import { useCreateThread } from "@liveblocks/react/suspense";
const createThread = useCreateThread();const thread = createThread({ body: {}, attachments: [], metadata: {} });

useDeleteThread

Returns a function that deletes a thread and all its associated comments by ID. Only the thread creator can delete the thread.

import { useDeleteThread } from "@liveblocks/react/suspense";
const deleteThread = useDeleteThread();deleteThread("th_xxx");

useEditThreadMetadata

Returns a function that edits a thread’s metadata.

import { useEditThreadMetadata } from "@liveblocks/react/suspense";
const editThreadMetadata = useEditThreadMetadata();editThreadMetadata({ threadId: "th_xxx", metadata: {} });

useMarkThreadAsResolved

Returns a function that marks a thread as resolved.

import { useMarkThreadAsResolved } from "@liveblocks/react/suspense";
const markThreadAsResolved = useMarkThreadAsResolved();markThreadAsResolved("th_xxx");

useMarkThreadAsUnresolved

Returns a function that marks a thread as unresolved.

import { useMarkThreadAsUnresolved } from "@liveblocks/react/suspense";
const markThreadAsUnresolved = useMarkThreadAsUnresolved();markThreadAsUnresolved("th_xxx");

useMarkThreadAsRead

Returns a function that marks a thread as read.

import { useMarkThreadAsRead } from "@liveblocks/react/suspense";
const markThreadAsRead = useMarkThreadAsRead();markThreadAsRead("th_xxx");

useCreateComment

Returns a function that adds a comment to a thread.

import { useCreateComment } from "@liveblocks/react/suspense";
const createComment = useCreateComment();const comment = createComment({ threadId: "th_xxx", body: {}, attachments: [],});

useEditComment

Returns a function that edits a comment’s body.

import { useEditComment } from "@liveblocks/react/suspense";
const editComment = useEditComment();editComment({ threadId: "th_xxx", commentId: "cm_xxx", body: {}, attachments: [],});

useDeleteComment

Returns a function that deletes a comment. If it is the last non-deleted comment, the thread also gets deleted.

import { useDeleteComment } from "@liveblocks/react/suspense";
const deleteComment = useDeleteComment();deleteComment({ threadId: "th_xxx", commentId: "cm_xxx" });

useAddReaction

Returns a function that adds a reaction to a comment.

import { useAddReaction } from "@liveblocks/react/suspense";
const addReaction = useAddReaction();addReaction({ threadId: "th_xxx", commentId: "cm_xxx", emoji: "👍" });

useRemoveReaction

Returns a function that removes a reaction from a comment.

import { useRemoveReaction } from "@liveblocks/react/suspense";
const removeReaction = useRemoveReaction();removeReaction({ threadId: "th_xxx", commentId: "cm_xxx", emoji: "👍" });

useAttachmentUrl

Returns a presigned URL for an attachment by its ID.

import { useAttachmentUrl } from "@liveblocks/react/suspense";
const { url, error, isLoading } = useAttachmentUrl("at_xxx");

👉 A Suspense version of this hook is also available, which will never return a loading state and will throw when there’s an error.

Notifications

useInboxNotifications

Returns the inbox notifications for the current user.

import { useInboxNotifications } from "@liveblocks/react/suspense";
const { inboxNotifications, error, isLoading } = useInboxNotifications();

Inbox notifications are project-based, meaning notifications from outside the current room are received. By default, the useInboxNotifications hook returns up to 50 notifications. To fetch more, the hook (along with useThreads) provides additional fields for pagination.

import { useInboxNotifications } from "@liveblocks/react";
const { inboxNotifications, isLoading, error,
// Available in Liveblocks 2.9 fetchMore, isFetchingMore, hasFetchedAll, fetchMoreError,} = useInboxNotifications();

The hasFetchedAll field indicates whether all available inbox notifications have been fetched since the initial load. To load more inbox notifications, you can call the fetchMore function. This function is safe to call even if no more inbox notifications are available or a fetch is already in progress—it simply won't do anything in that case. Therefore, you don't need to check hasFetchedAll or isFetchingMore before calling fetchMore.

The following example demonstrates how to use the fetchMore function to implement a "Load More" button, which fetches additional inbox notifications when clicked. The button is disabled while fetching is in progress.

<div>  {inboxNotifications.map((notification) => (    <InboxNotification key={notification.id}>...</InboxNotification>  ))}
{hasFetchedAll ? ( <div>🎉 You're all caught up!</div> ) : ( <button disabled={isFetchingMore} onClick={fetchMore}> Load more </button> )}</div>

Error handling is another important aspect to consider when using the useInboxNotifications hook. The error and fetchMoreError fields provide information about any errors that occurred during the initial fetch or subsequent fetch operations, respectively. You can use these fields to display appropriate error messages to the user and implement retry mechanisms if needed.

The following example shows how to display error messages for both initial loading errors and errors that occur when fetching more inbox notifications.

import { useInboxNotifications } from "@liveblocks/react";
const { inboxNotifications, error, fetchMore, fetchMoreError } = useInboxNotifications();
// Handle error if the initial load failed.// The `error` field is not returned by the Suspense hook as the error is thrown to nearest ErrorBoundaryif (error) { return ( <div> <p>Error loading inbox notifications: {error.message}</p> </div> );}
return ( <div> {inboxNotifications.map((notification) => ( <InboxNotification key={notification.id}>...</InboxNotification> ))}
{fetchMoreError && ( <div> <p>Error loading more inbox notifications: {fetchMoreError.message}</p> <button onClick={fetchMore}>Retry</button> </div> )} </div>);

👉 A Suspense version of this hook is also available, which will never return a loading state and will throw error to the nearest ErrorBoundary if initial load failed.

useUnreadInboxNotificationsCount

Returns the number of unread inbox notifications for the current user.

import { useUnreadInboxNotificationsCount } from "@liveblocks/react/suspense";
const { count, error, isLoading } = useUnreadInboxNotificationsCount();

👉 A Suspense version of this hook is also available, which will never return a loading state and will throw when there’s an error.

useMarkInboxNotificationAsRead

Returns a function that marks an inbox notification as read for the current user.

import { useMarkInboxNotificationAsRead } from "@liveblocks/react/suspense";
const markInboxNotificationAsRead = useMarkInboxNotificationAsRead();markInboxNotificationAsRead("in_xxx");

useMarkAllInboxNotificationsAsRead

Returns a function that marks all of the current user‘s inbox notifications as read.

import { useMarkAllInboxNotificationsAsRead } from "@liveblocks/react/suspense";
const markAllInboxNotificationsAsRead = useMarkAllInboxNotificationsAsRead();markAllInboxNotificationsAsRead();

useDeleteInboxNotification

Returns a function that deletes an inbox notification for the current user.

import { useDeleteInboxNotification } from "@liveblocks/react/suspense";
const deleteInboxNotification = useDeleteInboxNotification();deleteInboxNotification("in_xxx");

useDeleteAllInboxNotifications

Returns a function that deletes all of the current user‘s inbox notifications.

import { useDeleteAllInboxNotifications } from "@liveblocks/react/suspense";
const deleteAllInboxNotifications = useDeleteAllInboxNotifications();deleteAllInboxNotifications();

useInboxNotificationThread

Returns the thread associated with a "thread" inbox notification.

import { useInboxNotificationThread } from "@liveblocks/react/suspense";
const thread = useInboxNotificationThread("in_xxx");

It can only be called with IDs of "thread" inbox notifications, so we recommend only using it when customizing the rendering or in other situations where you can guarantee the kind of the notification.

useRoomNotificationSettings

Returns the user’s notification settings for the current room and a function to update them.

import { useRoomNotificationSettings } from "@liveblocks/react/suspense";
const [{ settings }, updateSettings] = useRoomNotificationSettings();

useUpdateRoomNotificationSettings

Returns a function that updates the user’s notification settings for the current room.

Use this if you don’t need the current user’s notification settings in your component, but you need to update them (e.g. an unsubscribe button).

import { useUpdateRoomNotificationSettings } from "@liveblocks/react/suspense";
const updateRoomNotificationSettings = useUpdateRoomNotificationSettings();updateRoomNotificationSettings({ threads: "all" });

Version History

useHistoryVersions

Returns the versions of the room. See Version History Components for more information on how to display versions.

import { useHistoryVersions } from "@liveblocks/react";
const { versions, error, isLoading } = useHistoryVersions();

Miscellaneous

useUser

Returns user info from a given user ID.

import { useUser } from "@liveblocks/react/suspense";
const { user, error, isLoading } = useUser("user-id");

👉 A Suspense version of this hook is also available, which will never return a loading state and will throw when there’s an error.

useRoomInfo

Returns room info from a given room ID.

import { useRoomInfo } from "@liveblocks/react/suspense";
const { info, error, isLoading } = useRoomInfo("room-id");

👉 A Suspense version of this hook is also available, which will never return a loading state and will throw when there’s an error.

TypeScript

Typing your data

It’s possible to have automatic types flow through your application by defining a global Liveblocks interface. We recommend doing this in a liveblocks.config.ts file in the root of your app, so it’s easy to keep track of your types. Each type (Presence, Storage, etc.), is optional, but it’s recommended to make use of them.

liveblocks.config.ts
declare global {  interface Liveblocks {    // Each user's Presence, for useMyPresence, useOthers, etc.    Presence: {};
// The Storage tree for the room, for useMutation, useStorage, etc. Storage: {};
UserMeta: { id: string; // Custom user info set when authenticating with a secret key info: {}; };
// Custom events, for useBroadcastEvent, useEventListener RoomEvent: {};
// Custom metadata set on threads, for useThreads, useCreateThread, etc. ThreadMetadata: {};
// Custom room info set with resolveRoomsInfo, for useRoomInfo RoomInfo: {};
// Custom activities data for custom notification kinds ActivitiesData: {}; }}
// Necessary if you have no imports/exportsexport {};

Here are some example values that might be used.

liveblocks.config.ts
import { LiveList } from "@liveblocks/client";
declare global { interface Liveblocks { // Each user's Presence, for useMyPresence, useOthers, etc. Presence: { // Example, real-time cursor coordinates cursor: { x: number; y: number }; };
// The Storage tree for the room, for useMutation, useStorage, etc. Storage: { // Example, a conflict-free list animals: LiveList<string>; };
UserMeta: { id: string; // Custom user info set when authenticating with a secret key info: { // Example properties, for useSelf, useUser, useOthers, etc. name: string; avatar: string; }; };
// Custom events, for useBroadcastEvent, useEventListener // Example has two events, using a union RoomEvent: { type: "PLAY" } | { type: "REACTION"; emoji: "🔥" };
// Custom metadata set on threads, for useThreads, useCreateThread, etc. ThreadMetadata: { // Example, attaching coordinates to a thread x: number; y: number; };
// Custom room info set with resolveRoomsInfo, for useRoomInfo RoomInfo: { // Example, rooms with a title and url title: string; url: string; };
// Custom activities data for custom notification kinds ActivitiesData: { // Example, a custom $alert kind $alert: { title: string; message: string; }; }; }}
// Necessary if you have no imports/exportsexport {};

Typing with createRoomContext

Before Liveblocks 2.0, it was recommended to create your hooks using createRoomContext, and manually pass your types to this function. This is no longer the recommended method for setting up Liveblocks, but it can still be helpful, for example you can use createRoomContext multiple times to create different room types, each with their own correctly typed hooks.

To upgrade to Liveblocks 2.0 and the new typing system, follow the 2.0 migration guide.

Helpers

shallow

Compares two values shallowly. This can be used as the second argument to selector based functions to loosen the equality check:

const redShapes = useStorage(  (root) => root.shapes.filter((shape) => shape.color === "red"),  shallow // 👈 here);

The default way selector results are compared is by checking referential equality (===). If your selector returns computed arrays (like in the example above) or objects, this will not work.

By passing shallow as the second argument, you can “loosen” this check. This is because shallow will shallowly compare the members of an array (or values in an object):

// Comparing arraysshallow([1, 2, 3], [1, 2, 3]); // true
// Comparison objectsshallow({ a: 1 }, { a: 1 }); // true

Please note that this will only do a shallow (one level deep) check. Hence the name. If you need to do an arbitrarily deep equality check, you’ll have to write a custom equality function or use a library like Lodash for that.

How selectors work

The concepts and behaviors described in this section apply to all of our selector hooks: useStorage , useSelf , useOthers , useOthersMapped, and useOther (singular).

Component.tsx
const child = useStorage((root) => root.child);const nested = useStorage((root) => root.child.nested);const total = useStorage((root) => root.x + root.y);const merged = useStorage((root) => [...root.items, ...root.more], shallow);

In a nutshell, the key behaviors for all selector APIs are:

Let’s go over these traits and responsibilities in the next few sections.

Selectors receive immutable data

The received input to all selector functions is a read-only and immutable top level context value that differs for each hook:

  • useStorage((root) => ...) receives the Storage root
  • useSelf((me) => ...) receives the current user
  • useOthers((others) => ...) receives a list of other users in the room
  • useOthersMapped((other) => ...) receives each individual other user in the room
  • useOther(connectionId, (other) => ...) receives a specific user in the room

For example, suppose you have set up Storage in the typical way by setting initialStorage in your RoomProvider to a tree that describes your app’s data model using LiveList, LiveObject, and LiveMap. The "root" argument for your selector function, however, will receive an immutable and read-only representation of that Storage tree, consisting of "normal" JavaScript datastructures. This makes consumption much easier.

Component.tsx
function Component() {  useStorage((root) => ...);  //          ^^^^  //          Read-only. No mutable Live structures in here.  //  //          {  //            animals: ["🦁", "🦊", "🐵"],  //            mathematician: { firstName: "Ada", lastName: "Lovelace" },  //            fruitsByName: new Map([  //              ["apple", "🍎"],  //              ["banana", "🍌"],  //              ["cherry", "🍒"],  //            ])  //          }  //}

Internally, these read-only trees use a technique called structural sharing. This means that between rerenders, if nodes in the tree did not change, they will guarantee to return the same memory instance. Selecting and returning these nodes directly is therefore safe and considered a good practice, because they are stable references by design.

Selectors return arbitrary values

Component.tsx
const animals = useStorage((root) => root.animals);// ["🦁", "🦊", "🐵"]
const ada = useStorage((root) => root.mathematician);// { firstName: "Ada", lastName: "Lovelace" }
const fullname = useStorage( (root) => `${root.mathematician.firstName} ${root.mathematician.lastName}`);// "Ada Lovelace"
const fruits = useStorage((root) => [...root.fruitsByName.values()], shallow);// ["🍎", "🍌", "🍒"]

Selectors you write can return any value. You can use it to “just” select nodes from the root tree (first two examples above), but you can also return computed values, like in the last two examples.

Selector functions must return a stable result

One important rule is that selector functions must return a stable result to be efficient. This means calling the same selector twice with the same argument should return two results that are referentially equal. Special care needs to be taken when filtering or mapping over arrays, or when returning object literals, because those operations create new array or object instances on every call (the reason why is detailed in the next section).

Examples of stable results

(root) => root.animals is stable

Liveblocks guarantees this. All nodes in the Storage tree are stable references as long as their contents don’t change.

️️⚠️ (root) => root.animals.map(...) is not stable

Because .map() creates a new array instance every time. You’ll need to use shallow here.

(root) => root.animals.map(...).join(", ") is stable

Because .join() ultimately returns a string and all primitive values are always stable.

Use a shallow comparison if the result isn’t stable

If your selector function doesn’t return a stable result, it will lead to an explosion of unnecessary rerenders. In most cases, you can use a shallow comparison function to loosen the check:

import { shallow } from "@liveblocks/react";
// ❌ Bad - many unnecessary rerendersconst uncheckedItems = useStorage((root) => root.todos.filter((item) => !item.done));
// ✅ Greatconst uncheckedItems = useStorage( (root) => root.todos.filter((item) => !item.done), shallow // 👈 The fix!);

If your selector function constructs complex objects, then a shallow comparison may not suffice. In those advanced cases, you can provide your own custom comparison function, or use _.isEqual from Lodash.

Selectors auto-subscribe to updates

Selectors effectively automatically subscribe your components to updates to the selected or computed values. This means that your component will automatically rerender when the selected value changes.

Using multiple selector hooks within a single React component is perfectly fine. Each such hook will individually listen for data changes. The component will rerender if at least one of the hooks requires it. If more than one selector returns a new value, the component still only rerenders once.

Technically, deciding if a rerender is needed works by re-running your selector function (root) => root.child every time something changes inside Liveblocks storage. Anywhere. That happens often in a busy multiplayer app! The reason why this is still no problem is that even though root will be a different value on every change, root.child will not be if it didn’t change (due to how Liveblocks internally uses structural sharing).

Only once the returned value is different from the previously returned value, the component will get rerendered. Otherwise, your component will just remain idle.

Consider the case:

function Component() {  const animals = useStorage((root) => root.animals);}

And the following timeline:

  • First render, root.animals initially is ["🦁", "🦊", "🐵"].
  • Then, something unrelated elsewhere in Storage is changed. In response to the change, root.animals gets re-evaluated, but it still returns the same (unchanged) array instance.
  • Since the value didn’t change, no rerender is needed.
  • Then, someone removes an animal from the list. In response to the change, root.animals gets re-evaluated, and now it returns ["🦁", "🦊"].
  • Because the previous value and this value are different, the component will rerender, seeing the updated value.

We use cookies to collect data to improve your experience on our site. Read our Privacy Policy to learn more.