How to Use Liveblocks to Add Real-Time Collaboration to Your App

Liveblocks provides a managed infrastructure for real‑time collaboration features—think multiplayer cursors, comments, and live data sync. With just a few lines of code, you can turn any React app into a collaborative workspace. This tutorial walks you through the essential steps to get started.

First, sign up at liveblocks.io to get your public API key. Then install the client library: npm install @liveblocks/client @liveblocks/react. Wrap your app with and pass your API key to enable the context for all collaborative components.

Article illustration

1. Create and Join a Room

A room is a shared environment where users collaborate. Use the createRoom method or the useRoom hook to enter a room by its ID. For example:

  • const room = client.enterRoom("my-room", { initialPresence: { cursor: null } });
  • In React: const { room } = useRoom(); inside a RoomProvider component.

2. Add Real‑Time Presence and Cursors

Presence shows each user’s cursor position, selection, or any custom data. Use useMyPresence to update the current user’s state and useOthers to render remote cursors.

  • const [myPresence, updateMyPresence] = useMyPresence();
  • const others = useOthers(); — iterate over others to draw cursors.

3. Sync Document State with Storage

For persistent collaborative data (like text or canvas shapes), use Liveblocks Storage. Define a storage schema and use useStorage to read and useMutation to write changes safely.

  • const data = useStorage(root => root.doc);
  • const update = useMutation(({ storage }, newValue) => { storage.root.set("doc", newValue); }, []);

4. Handle Comments and Threads

Liveblocks also includes a comments API. Use useThreads and createThread to allow users to leave inline feedback. Perfect for design tools or document editors.

Start simple: enable comments by passing threads in your room configuration and use the useThreads hook to display them.

That’s it! With Liveblocks you skip the WebSocket and CRDT complexity. In a few hours you can ship a fully collaborative experience that feels like Google Docs or Figma. Dive into their free tier today.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *