|
| 1 | +--- |
| 2 | +name: "liveblocks-best-practices" |
| 3 | +description: "Use this skill when building, debugging, or answering questions |
| 4 | + about Liveblocks. Liveblocks gives you the building blocks and infrastructure |
| 5 | + to enable people and AI to work together inside your app, powering realtime |
| 6 | + collaboration. |
| 7 | +
|
| 8 | + Liveblocks features include collaboration, rooms, organizations, workspaces, |
| 9 | + comments, composer, threads, notifications, multiplayer, conflict resolution, |
| 10 | + realtime presence, avatar stacks, AI collaborators, AI agents, text editors, |
| 11 | + Tiptap, BlockNote, Lexical, React Flow, Chat SDK. |
| 12 | +
|
| 13 | + Common components include AiChat, Thread, InboxNotification, Composer, Toolbar |
| 14 | + (for Lexical Tiptap), FloatingToolbar, FloatingComposer, FloatingThreads, |
| 15 | + AnchoredThreads. |
| 16 | +
|
| 17 | + Common hooks include useThreads, useStorage, useMutation, useOthers, |
| 18 | + useInboxNotifications, useAiChats. |
| 19 | +
|
| 20 | + Common issues are related to authentication (ID tokens vs access tokens), |
| 21 | + permissions, room limits, connection errors, user info." |
| 22 | +license: "Apache License 2.0" |
| 23 | +metadata: |
| 24 | + author: "liveblocks" |
| 25 | + version: "1.0.0" |
| 26 | +--- |
| 27 | + |
| 28 | +# Liveblocks best practices |
| 29 | + |
| 30 | +## When to use this skill |
| 31 | + |
| 32 | +Use this skill when implementing features using any Liveblocks packages, for |
| 33 | +example [`@liveblocks/react`](https://www.npmjs.com/package/@liveblocks/react) |
| 34 | +or [`@liveblocks/node`](https://www.npmjs.com/package/@liveblocks/node). This |
| 35 | +could be related to features like realtime multiplayer, commenting, |
| 36 | +notifications, cursors, avatar stacks, AI, and more. This also includes |
| 37 | +technologies like Liveblocks Storage, Yjs, Tiptap, BlockNote, Lexical, React |
| 38 | +Flow, tldraw, and more. |
| 39 | + |
| 40 | +## Quick reference |
| 41 | + |
| 42 | +A number of reference files are available in the `references/` directory. You |
| 43 | +must read the list below, and load relevant files as needed. For example, |
| 44 | +`add-user-information` is available in the `references/add-user-information.md` |
| 45 | +file. |
| 46 | + |
| 47 | +## References |
| 48 | + |
| 49 | +- `add-user-information`: Add user information to your application, such as |
| 50 | + name, avatar, color, and more. Users will no longer be anonymous. Useful for |
| 51 | + displaying user info in comments, comments mention suggestions, notifications, |
| 52 | + cursors, avatar stacks. |
| 53 | +- `ai-as-a-collaborator`: Use agentic workflows to allow AI to collaborate like |
| 54 | + a human inside your Liveblocks application. |
| 55 | +- `ai-chats-with-tools-knowledge-components`: Set up and troubleshoot chats with |
| 56 | + RAG, knowledge, custom components, tool calling, custom models. Different to |
| 57 | + AI as a collaborator, as it is NOT workflow based. |
| 58 | +- `auth-endpoint-callback`: Use a callback function to authenticate users, |
| 59 | + instead of passing a string to `authEndpoint`. Useful for passing custom |
| 60 | + headers to your back end, and for preventing automatic reconnection when the |
| 61 | + token is invalid. |
| 62 | +- `authenticating-with-access-tokens`: Alternative method for authenticating |
| 63 | + users with their ID and info, best for very simple permissions. |
| 64 | +- `authenticating-with-id-tokens`: Recommended method for authenticating users |
| 65 | + with their ID and info, best for complex permissions. |
| 66 | +- `avoid-hitting-user-limit-in-rooms`: How to avoid rooms filling up with users. |
| 67 | +- `compartmentalize-resources-with-organizations`: Use organizations to create |
| 68 | + workspaces/tenants and compartmentalize resources, such as rooms, |
| 69 | + notifications, comment threads, and more. You can filter by organization when |
| 70 | + listing rooms and more. |
| 71 | +- `create-custom-comment-composer`: Build your own commenting composer (advanced |
| 72 | + use cases only). |
| 73 | +- `create-custom-realtime-multiplayer`: Use Liveblocks Storage to build fully |
| 74 | + custom conflict-resolved multiplayer apps, like Figma, Pitch, Spline. Helpful |
| 75 | + for one-off realtime features too, like simple properties in a document. |
| 76 | +- `create-custom-text-editor-toolbar`: Set up a styled toolbar, static or |
| 77 | + floating next to your selection, in Tiptap and Lexical. |
| 78 | +- `create-rooms-manually`: Always create rooms yourself in production |
| 79 | + applications, setting permissions, organization, metadata. |
| 80 | +- `customize-thread-components`: Use slots to customize comments inside of |
| 81 | + threads, and their different parts. |
| 82 | +- `dark-mode-styles`: You can import CSS styling for dark mode themes. |
| 83 | +- `develop-and-test-locally`: Set up a local dev server, and use it for |
| 84 | + Continuous Integration (CI) and End-to-End (E2E) testing. Connect to your app |
| 85 | + not online. |
| 86 | +- `devtools-extension`: Inspect Liveblocks Storage, Yjs, presence, events, and |
| 87 | + connected users with DevTools extensions for Chrome, Firefox, Edge. |
| 88 | +- `edit-component-text-strings`: Override strings in default components, such as |
| 89 | + button values, tooltip text, error text, more. Also helpful for setting other |
| 90 | + languages. |
| 91 | +- `exhaustive-deps-with-usemutation`: Prevent and fix stale-closure bugs with |
| 92 | + `useMutation` by configuring the `react-hooks/exhaustive-deps` ESLint rule. |
| 93 | +- `handling-connection-errors`: Handle problems joining rooms because of |
| 94 | + permissions, authentication, changed room IDs. |
| 95 | +- `handling-full-rooms`: Handle problems caused by joining full rooms. |
| 96 | +- `handling-hook-and-component-errors`: Handle errors caused by hooks and |
| 97 | + pre-built components. |
| 98 | +- `handling-unstable-connections`: Implement fallbacks and error messages when |
| 99 | + users have poor network conditions. |
| 100 | +- `log-out-of-liveblocks`: Rarely useful, but helpful in specific SPA situations |
| 101 | + where you cannot navigate the page to log out. |
| 102 | +- `multiplayer-react-flow`: Use the Liveblocks React Flow package to create |
| 103 | + multiplayer diagrams with cursors and more. |
| 104 | +- `multiple-text-editors`: Add multiple Tiptap or BlockNote editors to the same |
| 105 | + page. Optionally use Storage to hold their field IDs. |
| 106 | +- `offline-support-in-text-editors`: Give your text editors the illusion of a |
| 107 | + quicker load time. |
| 108 | +- `override-css-variables`: Add custom styles to the default components by |
| 109 | + overriding Liveblocks CSS variables. |
| 110 | +- `performant-others-and-presence`: Prevent unnecessary presence renders by |
| 111 | + using `useOtherConnectionIds`, `useOthersMapped`, `useOther`, and `shallow` to |
| 112 | + update components only when necessary. |
| 113 | +- `performant-storage-with-selectors`: Prevent unnecessary storage renders by |
| 114 | + using `useStorage` selectors and `shallow`. |
| 115 | +- `prevent-unsaved-changes-being-lost`: Stop losing unsynched or unsaved |
| 116 | + changes. |
| 117 | +- `primitive-component-parts`: Use primitives to create custom components or to |
| 118 | + merge components from your design system into Liveblocks UI. |
| 119 | +- `remove-liveblocks-branding`: A Liveblocks logo badge appears in the bottom |
| 120 | + right of the screen, this is how to remove it. |
| 121 | +- `rendering-error-components`: Use `ErrorBoundary` to structure your app with |
| 122 | + error fallbacks. |
| 123 | +- `rendering-loading-components`: Use `ClientSideSuspense` to performantly |
| 124 | + structure your app with loading skeletons and spinners. |
| 125 | +- `send-comment-notification-emails`: Send email notifications to users when |
| 126 | + they have unread comments. These comments are sent via webhooks, and are |
| 127 | + triggered when a user is mentioned, or has participated in a thread. |
| 128 | +- `smoother-realtime-updates`: Make presence and storage run at a higher frame |
| 129 | + rate, appearing more smooth. Using this option, updates can be received more |
| 130 | + frequently. |
| 131 | +- `suspense-vs-regular-hooks`: You must read this when using any Liveblocks |
| 132 | + hooks. React suspense uses `ErrorBoundary` and `ClientSideSuspense` |
| 133 | + components. Regular hooks use `{ error, isLoading }` values. |
| 134 | +- `tiptap-best-practices`: Server-side rendering, starter kit extensions, |
| 135 | + initial content, unsaved changes, more. |
| 136 | +- `trigger-custom-notifications`: Trigger any kind of notification in your |
| 137 | + inbox, even those unrelated to commenting. |
| 138 | +- `type-liveblocks-correctly`: Always use TypeScript to type Liveblocks where |
| 139 | + available. Presence, others, user info, storage, metadata, room info, |
| 140 | + notifications activities, can all be automatically typed. |
| 141 | +- `url-params-in-room-id`: Use URL params in room IDs to create rooms, and |
| 142 | + incorporate document titles in the URL. |
| 143 | +- `utility-components`: Import a ready-made emoji picker, or human-readable |
| 144 | + timestamps, durations, file sizes. |
| 145 | +- `yjs-best-practices`: YKeyValue, subdocuments, V2 encoding, double imports, |
| 146 | + getYjsProviderForRoom. NOT relevant if you're using Tiptap, BlockNote, or |
| 147 | + Lexical. |
| 148 | +- `z-index-issues`: Fix z-index problems by targeting portaled elements. |
| 149 | + |
| 150 | +## Note |
| 151 | + |
| 152 | +Some files link to markdown files in the Liveblocks docs, for example: |
| 153 | + |
| 154 | +``` |
| 155 | +https://liveblocks.io/docs/concepts.md |
| 156 | +``` |
| 157 | + |
| 158 | +When linking users to these pages, remove `.md` from the link, so they can view |
| 159 | +the full content: |
| 160 | + |
| 161 | +``` |
| 162 | +https://liveblocks.io/docs/concepts |
| 163 | +``` |
| 164 | + |
| 165 | +## Liveblocks packages |
| 166 | + |
| 167 | +Always check if we provide a package for your technology. For example, if you're |
| 168 | +using React Flow, you should use **`@liveblocks/react-flow`**. |
| 169 | + |
| 170 | +- **`@liveblocks/client`**: JavaScript client. Can use with any framework, e.g. |
| 171 | + Vue, Svelte, vanilla JS. |
| 172 | +- **`@liveblocks/react`**: React client. Contains hooks and components.. |
| 173 | +- **`@liveblocks/react-ui`**: Pre-built React UI components and primitives. |
| 174 | + `@liveblocks/react` for data and hooks. |
| 175 | +- **`@liveblocks/react-tiptap`**: Collaborative Tiptap integration for React. |
| 176 | +- **`@liveblocks/react-blocknote`**: Collaborative BlockNote integration for |
| 177 | + React. |
| 178 | +- **`@liveblocks/react-lexical`**: Collaborative Lexical integration for React. |
| 179 | +- **`@liveblocks/node-lexical`**: Server-side Lexical utilities for Node.js. |
| 180 | +- **`@liveblocks/node-prosemirror`**: Server-side ProseMirror utilities For |
| 181 | + Node.js. Also works with Tiptap and BlockNote. |
| 182 | +- **`@liveblocks/react-flow`**: Multiplayer React Flow. |
| 183 | +- **`@liveblocks/yjs`**: Yjs provider backed by Liveblocks rooms. |
| 184 | +- **`@liveblocks/redux`**: Sync Liveblocks room state with a Redux store. |
| 185 | +- **`@liveblocks/zustand`**: Sync Liveblocks room state with Zustand. |
| 186 | +- **`@liveblocks/node`**: Node.js server SDK. Use for auth and lots more. |
| 187 | +- **`@liveblocks/emails`**: Build notification emails more easily. |
| 188 | +- **`@liveblocks/chat-sdk-adapter`**: Make multi-platform (Liveblocks, Slack, |
| 189 | + Discord, etc.) chat bots. |
| 190 | +- **Python SDK**: Python server SDK. Use for auth and lots more. |
| 191 | +- **REST API**: HTTP API. Use for auth and lots more. |
| 192 | + |
| 193 | +### If a technology is not listed here |
| 194 | + |
| 195 | +If the users want to set up a new technology, first check the following |
| 196 | +resources to see if a package exists: |
| 197 | + |
| 198 | +- [llms.txt](https://liveblocks.io/llms.txt) |
| 199 | +- [Documentation](https://liveblocks.io/docs) |
| 200 | +- [Examples](https://liveblocks.io/examples) |
| 201 | + |
| 202 | +## Learn more |
| 203 | + |
| 204 | +Learn more in [Liveblocks docs](https://liveblocks.io/docs). |
0 commit comments