Get started with AI presence using Liveblocks and Next.js

Liveblocks is a realtime collaboration infrastructure for building performant collaborative experiences. Follow the following steps to show an AI agent as a real user inside your Next.js /app directory application: in the avatar stack, and as a highlighted box around the form field or cell it’s currently editing, using setPresence from @liveblocks/node and the useOthers hook from @liveblocks/react.

Quickstart

  1. Have a presence app ready

    To add AI presence on top of your existing app, you first need a Liveblocks presence setup with AvatarStack and authenticated users. Open up your app, or set up presence if you haven’t already.

    Get started with presence

  2. Install @liveblocks/node

    We’ll set the AI agent’s presence from the server, so install @liveblocks/node.

    Terminal
  3. Add your secret key

    Create a .env.local file and add your Liveblocks secret key from the dashboard.

    .env.local
  4. Add an AI user to your database

    The AI agent appears in the room like any other user, so it needs an ID, name, and avatar. Add a dedicated user for your agent alongside your real users, and return it from resolveUsers so it can be rendered in AvatarStack.

    Find where you fetch data in your resolver functions…
    …and modify the return values to include an AI user
  5. Show the AI in your avatar stack

    To make the AI appear in AvatarStack and useOthers from the server, call setPresence. Use this to signify your agent is running a task in the room.

    app/agent-presence.ts

    The agent will now appear in presence alongside real users.

  6. Show AI presence in custom UI

    With useOthers you can create custom AI presence within any UI. An example of this is showing a highlighted box around the part of the interface that the AI is editing.

    To set this up, first set presence types in your app. An editingId string can represent an element that the AI is currently editing, such as a form field, and a value null can signify that the AI is not editing anything.

    liveblocks.config.ts

    Next, when using setPresence on the server, set the editingId property to the ID of the element that the AI is currently editing.

    app/agent-presence.ts

    On the client, use useOthers to create a small component that draws a highlighted box around the targeted element when the AI is editing it.

    app/AiPresenceEditFrame.tsx

    You can then wrap any part of your your UI with AiPresenceEditFrame, give each one a unique editingId, and if the AI is editing that ID, a highlighted box will appear around it.

    app/Form.tsx
  7. Hide the AI when it’s done

    You can optionally choose to hide the AI's presence when it's completed editing. To do this, set the minimum ttl value to 2 seconds.

    You have AI presence set up! In some apps, it may be useful to use multiple different IDs for AI, as this way you can individually control when each AI appears and disappears.

What to read next

Congratulations! You’ve set up the foundation for AI presence in your Next.js application.


Examples using AI presence