Comments - Email notifications

Using Liveblocks webhooks, it’s possible to trigger your API endpoints when certain events occur, such as a thread being created, or a comment being modified. One use for these events is sending new comment notifications, for example via email or Slack.

Sending notifications to participating users

Using Liveblocks webhooks you can listen to a range of events such as comments being deleted, or reactions being added. On the dashboard you can create a webhook for a project, and select which events you’d like to listen to.

The endpoint URL will be sent a request with relevant data when the event occurs. Here’s an example of an event object that’s sent when a thread is created.

const event = {  type: "threadCreated",  data: {    projectId: "my-project-id",    roomId: "my-room-id",    threadId: "th_sf8s6sh...",    createdAt: "2021-10-06T01:45:56.558Z",    createdBy: "my-user-id",  },};

In your endpoint, you can use this event object with functions such as liveblocks.getThread and stringifyCommentBody, which will return the thread that’s just been created, and the text from the first comment.

const thread = await liveblocks.getThread({  roomId: event.data.roomId,  threadId: event.data.threadId,});
// { type: "thread", id: "th_d75sF3...", ... }console.log(thread);
const commentText = await stringifyCommentBody(thread.comments[0].body);
// "Hello world!"console.log(commentText);

You could then call liveblocks.getParticipants to find a list of user IDs mentioned in the thread, and send an email to each user.

const { participantIds } = await liveblocks.getThreadParticipants({  roomId: event.data.roomId,  threadId: event.data.threadId,});
// ["adri@example.com", "florent@example.com"]console.log(participantIds);
// Send email notificationsfor (const userId of participantIds) { const emailAddress = (userId);
// Send email to the user using your setup ({ from: "hello@my-company.com", to: emailAddress, title: "New thread", text: `New thread created: ${commentText}`, });}

Here’s an example with every step linked together, along with the code necessary to verify a webhook request is valid.

Learn more about building this in our how to send email notifications guide.

Webhook events

There are more webhook events than just the ThreadCreatedEvent event used above—a number related to Comments are available to use.

There are also more events, for example you can trigger events when users enter or leave rooms. We recommend reading our guide on testing webhooks locally to get started.

Retrieving and modifying Comments data

Here’s every Comments-related @liveblocks/node function. Each also has a corresponding REST API, you can find more info by following the links.