Quickstart - Get started with creating a realtime feed using Liveblocks and Next.js
Liveblocks is a realtime collaboration infrastructure for building performant
collaborative experiences. Follow the following steps to start adding a realtime
feed to your Next.js /app directory application using hooks from
@liveblocks/react and methods from
@liveblocks/node.
Quickstart
Install Liveblocks
Every package should use the same version.
TerminalInitialize the
liveblocks.config.tsfileWe can use this file later to define types for our application.
TerminalDefine feed message shape
Inside the new
liveblocks.config.tsfile, define the data shape for messages in feeds. Any JSON data is supported, and can be used to render UI in your application. In this this guide, we'll defineroleandcontentproperties, like you’d find in an AI chat.liveblocks.config.tsCreate a feed in your back end
Create a feed in your back end using the
createFeedmethod and send messages with thecreateFeedMessagemethod. This snippet simualtes a slow running process then adds a second message. Here we’ve put this in a server action.Picture this file as the AI back end of your application. It’s responsible for generating AI responses and adding them into the feed.
app/actions.tsRender the feed messages
Now that we’ve defined the data shape, we can start using it. Add
useFeedMessagesto get a list of feed messages, then loop through them to render theirdata. Import thenewFeedserver action into a button to demo creating a new feed.app/RealtimeFeed.tsxCreate a Liveblocks room
Liveblocks uses the concept of rooms, separate virtual spaces where people collaborate, and to create a realtime experience, multiple users must be connected to the same room. When using Next.js’
/approuter, we recommend creating your room in aRoom.tsxfile in the same directory as your current route.Set up a Liveblocks client with
LiveblocksProvider, join a room withRoomProvider, and useClientSideSuspenseto add a loading spinner to your app.app/Room.tsxAdd the room and feed to your page
After creating your room file, it’s time to join it. Import your room into your
page.tsxfile, and place your feed component inside it.app/page.tsxNext: authenticate your users and add presence
Feeds is now set up and working! Each user in your app is still anonymous—the next step is to authenticate each user as they connect, and optionally share their live presence in an avatar stack, cursors, or custom UI.
Add your users to Comments
Optional: read about creating agentic workflows
Liveblocks can be used to create agentic workflows, where AI agents can collaborate with humans in realtime. This is a powerful way to build collaborative applications with AI, and we’ve detailed various ways to create this.
Enabling agentic workflows with Liveblocks
What to read next
Congratulations! You’ve set up the foundation to start building a realtime feeds experience for your Next.js application.