How to notify users about unread comments outside of your app
Liveblocks Comments allows you to build a
commenting experience. With our webhooks and REST API, it’s possible to
aggregate a list of unread comments, and use them to trigger notifications in
Slack, Microsoft Teams, or any other external service with an API, using
webhooks. Notifications can also be displayed in your app using
useInboxNotifications
and the
InboxNotification
component.
What we’re building
In this guide we’ll be learning how to notify users about unread comments outside of your app, and more specifically, we’ll be looking at how to:
- Trigger events based on unread comments using the
NotificationEventwebhook event. - Fetch unread comments using the
@liveblocks/nodepackage.
What are inbox notifications?
Liveblocks uses the concept of inbox notifications, which differ to external notifications. Inbox notifications are displayed within in-app inboxes, group multiple activities together, and can change over time, like when a new comment is added to a thread.
External notifications, such as Slack, and Microsoft Teams, are different, and Liveblocks is set up to send them in a way that won’t overload your users with notifications. This means that Liveblocks will wait to trigger these notifications until a certain amount of time has passed, and will only trigger them if your users has not read the notification on the front-end, which we automatically keep track of. We then aggregate multiple unread comments into a single notification, so your users just get a single ping per thread.
Learn more about Notifications for Comments in the overview page.
Using webhooks
Liveblocks provides a number of webhooks that can
send requests to your API endpoint when certain events occurs. One webhook we
provide is the NotificationEvent
webhook, which is triggered for each participating user in a thread, and can be
used to send external notifications to your users.
The information it returns allows you to retrieve comments that have not yet been read by the user, making it possible to aggregate multiple unread comments into a single external notification. Let’s take a look at how to set this up.
Notification channels
You can send notifications via different channels, such as email, Slack,
Microsoft Teams, and Web Push. In our dashboard, you can enable notifications on
certain channels, and in this guide, we’ll be using the Slack channel. You must
always enable the correct channel to ensure your
NotificationEvent webhook events
are triggered, and this guide will take you through setting it up.
Create an endpoint in your project
When a webhook event is triggered, it can send a POST request to the back end in your project. In this guide, we’ll be using a Next.js route handler (API endpoint) as an example, but other frameworks work similarly.
In order to use webhooks, we’ll need to retrieve the headers and body from
the request. Here’s the basic endpoint we’ll be starting from:
Create this endpoint in your project, and make it available on localhost at
the following URL:
Make a note of this endpoint URL, as you’ll be using it later.
Testing webhooks locally
Running webhooks locally can be difficult, but one way to do this is to use a
tool such as localtunnel or
ngrok which allow you to temporarily
put your localhost server online.
If your project is running on localhost:3000, you can run the following
command to generate a temporary URL that’s available while your localhost server
is running:
localtunnel generates a base URL that can be placed into the Liveblocks
webhooks dashboard for quick testing. To use this, take the full address of your
webhook endpoint, and replace the domain in your localhost address with the
generated URL.
You now have a URL that can be used in the webhooks dashboard.
Set up webhooks on the Liveblocks dashboard
To use webhooks, you need to pass your endpoint URL to the webhooks dashboard inside your Liveblocks project, and tell the webhook to trigger when a comment has been created.
Select your project
From the Liveblocks dashboard, navigate to the project you’d like to use with webhooks, or create a new project.

Go to the notifications dashboard
Click on the “Notifications” tab on the menu at the left.

Enable the thread notification type
Click on “Edit” at the top right, enable
threadnotifications on the Slack channel, and publish your changes.
Go to the webhooks dashboard
Click on the “Webhooks” tab on the menu at the left.

Create an endpoint
Click the “Add endpoint…” button on the webhooks dashboard to start setting up your webhook.

Add your endpoint URL
Enter the URL of the endpoint. In a production app this will be the real endpoint, but for now enter your
localtunnelURL from earlier.
Enable notification webhook events
Check the “notification” event in the dialog to enable the correct webhooks events.

Get your webhook secret key
Click “Add endpoint” at the bottom, then find your “Secret key” on the next page, and copy it.

Webhooks dashboard is set up!
Notification webhooks are set up! Let’s go back to the code.
Verify the webhook request
The @liveblocks/node package provides
you with a function that verifies whether the current request is a real webhook
request from Liveblocks. You can set this up by setting up a
WebhookHandler and
running verifyRequest.
Make sure to add your webhook secret key from earlier—in a real project we’d recommend using an environment variable for this.
Check the event and notification permissions
After verifying the request, we can then check we’re receiving the correct type
of event, on the correct channel. There are different notification events, and
in this case we’d like to check for
thread notification, as we’re
specifically listening for new comments. We can do this using
ThreadNotificationEvent,
making sure to check for the slack channel.
Note that we’re also checking if the user should receive a notification—Liveblocks doesn’t have knowledge of your permissions system on the back end, so it’s your responsibility to check if this user should have access to the room.
Get comment and thread data
The next step is to use the
Liveblocks client from
@liveblocks/node to retrieve the inbox notification, and the corresponding
thread’s data. To do this we’ll need to add our project’s secret key from
the dashboard to the Liveblocks client (not the webhook
secret key we used earlier), before awaiting the following functions:
getInboxNotification
and getThread.
Get the unread comments
The next step is to get each unread comment by comparing the readAt time in
the inbox notification with the createAt time on each comment. We’re also
filtering out each comment with no body, which represents a deleted comment.
If there are no unread notifications, then we’re choosing not to send an email.
Generating comment text for the external notification
Now that we have the comment data, we have one more step before sending the
notifications—formatting each comment’s text, found inside comment.body, and
generating the text for our external notification.
By using
await stringifyCommentBody,
we can convert each comment into plain text, markdown, or HTML. In this code
snippet, you can see we’re looping through each comment, and replacing
comment.body with a markdown string.
This snippet outputs simple formatting, for example it renders a user IDs (e.g.
@jory.quispe) instead of a names (e.g. @Jory Quispe), but you can create
more complex formatting easily by using more complex
stringifyCommentBody
options.
Send external notifications
Now that the comment’s body has been formatted, we can send the external notifications. In this example, we’re sending our markdown to an incoming webhook in Slack.
We’ve now successfully sent an external notification to a user!
Allow users to toggle notifications
Using Liveblocks hooks and methods, it’s possible to create a notifications settings interface, allowing end users to choose which notifications they’d like to receive, and on which channels, saving their preferences.

Learn more in our guide on creating a notification settings panel.
Recap
Great, we’re successfully sending external notifications to users when comments are left unread! In this guide we’ve learned:
- How to use webhooks and the
NotificationEvent. - How to use the
@liveblocks/nodepackage to fetch and render unread thread data. - How to hook it up to an external service, such as Slack.