Get started with a Chat SDK bot using Liveblocks and Next.js

Liveblocks is a realtime collaboration infrastructure for building performant collaborative experiences. Follow the following steps to start building a bot that reads and responds to Liveblocks comment threads using the Chat SDK and the @liveblocks/chat-sdk-adapter in your Next.js /app directory application.

Live example

See the finished result in the Chat SDK Bot example.

Quickstart

  1. Install Liveblocks and Chat SDK

    Install the Liveblocks Chat SDK adapter, the Chat SDK, and a state adapter.

    Terminal
  2. Add your environment variables

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

    .env.local

    You'll create the webhook and get the secret in the final step.

  3. Create a user database

    Create a file to store your bot's user ID and a function to resolve users. The resolveUsers function converts user IDs from mentions into display names.

    app/database.ts
  4. Create the bot instance

    Create a bot instance using the Chat SDK with the Liveblocks adapter. The adapter connects your bot to Liveblocks comment threads.

    app/bot.ts
  5. Handle mentions and reactions

    Add event handlers to respond when users mention the bot or react to messages. Add these handlers to your app/bot.ts file after the bot instance.

    app/bot.ts
  6. Create the webhook endpoint

    Create an API route to receive Liveblocks webhooks. The bot processes incoming comments and reactions through this endpoint.

    app/api/webhooks/liveblocks/route.ts

    For production deployments on Vercel, use waitUntil from @vercel/functions for background processing:

    app/api/webhooks/liveblocks/route.ts
  7. Set up Liveblocks webhooks

    The final step is to configure Liveblocks webhooks to send events to your bot.

    1. Follow the guide on testing webhooks locally
    2. When creating the webhook endpoint in the dashboard, enable these events:
      • commentCreated
      • commentReactionAdded
      • commentReactionRemoved
    3. Copy your webhook secret (whsec_...) and add it to .env.local

    Now when users @-mention your bot or react to messages in comment threads, your bot will respond automatically.

    Set up webhooks

What to read next

Congratulations! You've set up a bot that responds to Liveblocks comment threads using the Chat SDK.


Examples using Chat SDK