Sign in

Quickstart - Get started with an in-app custom notification inbox using Liveblocks and Next.js

Liveblocks is a realtime collaboration infrastructure for building performant collaborative experiences. Follow this guide to start adding custom notifications to your Next.js /app directory application using the hooks from @liveblocks/react and the components from @liveblocks/react-ui.

Quickstart

  1. Install Liveblocks

    Every package should use the same version.

    Terminal
    npm install @liveblocks/client @liveblocks/react @liveblocks/react-ui @liveblocks/node
  2. Initialize the liveblocks.config.ts file

    We can use this file later to define types for our application.

    Terminal
    npx create-liveblocks-app@latest --init --framework react
  3. Create a Liveblocks provider

    Liveblocks Notifications uses the concept of projects, which relate to projects in your dashboard. Notifications are sent between users in the same project. To connect and receive notifications, you must add LiveblocksProvider to a client component in your app.

    app/Providers.tsx
    "use client";
    import { ReactNode } from "react";import { LiveblocksProvider } from "@liveblocks/react";
    export function Providers({ children }: { children: ReactNode }) { return ( <LiveblocksProvider publicApiKey={""}> {children} </LiveblocksProvider> );}
  4. Add the provider to your layout

    After creating your provider file, it’s time to use it. Import your room into your layout.tsx file.

    app/layout.tsx
    import { Providers } from "./Providers";
    export default function Layout({ children }) { return ( <html> <body> <Providers> {children} </Providers> </body> </html> );}
  5. Use the Liveblocks hooks and components

    Now that we’ve set up the provider, we can start using the Liveblocks hooks and components. We’ll add useInboxNotifications to get the current project’s notifications, then we’ll use InboxNotification and InboxNotificationList to render them.

    app/page.tsx
    "use client";
    import { useInboxNotifications } from "@liveblocks/react/suspense";import { InboxNotification, InboxNotificationList,} from "@liveblocks/react-ui";
    export default function Page() { const { inboxNotifications } = useInboxNotifications();
    return ( <InboxNotificationList> {inboxNotifications.map((inboxNotification) => ( <InboxNotification key={inboxNotification.id} inboxNotification={inboxNotification} /> ))} </InboxNotificationList> );}
  6. Import default styles

    The default components come with default styles, you can import them into the root layout of your app or directly into a CSS file with @import.

    app/layout.tsx
    import "@liveblocks/react-ui/styles.css";
  7. Trigger a custom notification

    Trigger a custom notification using triggerInboxNotification from a server action or route handler. In this example, a custom $fileUploaded notification is sent.

    "use server";
    import { Liveblocks } from "@liveblocks/node";
    const liveblocks = new Liveblocks({ secret: "",});
    // Call this in your app to create a custom notificationexport async function triggerCustomNotification() { await liveblocks.triggerInboxNotification({ // The user that will receive the notification userId: "steven@example.com", kind: "$fileUploaded", subjectId: "my-file",
    // Custom data for this notification activityData: { file: "https://example.com/my-file.zip", status: "pending", }, });}
  8. Render the custom notification

    After triggering the custom notification, modify InboxNotification to render it with custom UI.

    app/page.tsx
    "use client";
    import { useInboxNotifications } from "@liveblocks/react/suspense";import { InboxNotification, InboxNotificationList,} from "@liveblocks/react-ui";
    export default function Page() { const { inboxNotifications } = useInboxNotifications();
    return ( <InboxNotificationList> {inboxNotifications.map((inboxNotification) => ( <InboxNotification key={inboxNotification.id} inboxNotification={inboxNotification} kinds={{ $fileUploaded: (props) => ( <InboxNotification.Custom {...props} title="File uploaded!" aside={<InboxNotification.Icon>📎</InboxNotification.Icon>} > A new file has been uploaded: <b>{props.inboxNotification.activities[0].file}</b> </InboxNotification.Custom> ), }} /> ))} </InboxNotificationList> );}
  9. Next: authenticate and add your users

    Notifications is set up and working now, but each user is anonymous—the next step is to authenticate each user as they connect, and attach their name and avatar to their notifications.

    Add your users to Notifications

What to read next

Congratulations! You’ve set up the foundation to start building a notifications experience for your React application.


Examples using Notifications