Liveblocks 1.8: Typed REST APIs and easy Comments notifications
Today we're introducing new typed REST API methods, making integrating Liveblocks into your back end easier than ever. We're also adding functions to help you transform comments, and create email notifications more easily.
With Liveblocks 1.8, we’re introducing an improved way to consume our REST APIs
with types, making it quicker to integrate Liveblocks into the back end of your
application. We’re also adding a way to transform comments into HTML, Markdown,
and more, meaning sending formatted email notifications is now simpler.
It’s now possible to access our REST APIs using the
@liveblocks/node package. Previously,
the only way to access our REST APIs in Node.js was via a fetch call.
// ❌ Before, using fetchconst response =awaitfetch("https://api.liveblocks.io/v2/rooms",{ method:"POST", headers:{ Authorization:"sk_prod_xxxxxxxxxxxxxxxxxxxxxxxx",}, body:JSON.stringify({ id:"my-room-name", defaultAccesses:["room:write"],}),}); // ❌ No typesconst room =await response.json();
From now, you can set up a Liveblocks client, call a corresponding method, and
get type hints on the returned value.
// ✅ After, using `Liveblocks` and typed methodsconst liveblocks =newLiveblocks({ secret:"sk_prod_xxxxxxxxxxxxxxxxxxxxxxxx",}); // ✅ Fully-typed roomconst room =await liveblocks.createRoom("my-room-name",{ defaultAccesses:["room:write"],});
Every existing REST API is supported, and we’ll continue to add them in the
future. Here’s an example of some more methods.
// Get users currently in a roomconst activeUsers =await liveblocks.getActiveUsers("my-room-id"); // Get Storage data as JSONconst storage =await liveblocks.getStorageDocument("my-room-id","json"); // Update Yjs dataawait liveblocks.sendYjsBinaryUpdate("my-room-id",{ update:Y.encodeStateAsUpdate(yDoc),}); // Get the last 20 rooms that contain your custom metadataconst canvasRooms =await liveblocks.getRooms({ limit:20, metadata:{ myRoomType:"canvas"},});
Find all the methods in the API reference under
@liveblocks/node.
Using Liveblocks 1.8, you can transform a comment’s text into different formats,
for example HTML, Markdown, or a plain string of text. Here’s an example of a
comment converted into two different formats.
Thank you **so much** @Emil Joyce!
<p>Thank you <b>so much</b><spandata-mention>@Emil Joyce</span>!</p>
As a bonus, we’ve also added a utility that makes it easy to find each user
mentioned in a comment’s body. It’s called
getMentionedIdsFromCommentBody, and
here’s how it works.
// Retrieve a commentconst comment =await liveblocks.getComment({ roomId:"my-room-id", threadId:"my-thread-id", commentId:"my-comment-id",}); // Get IDs for each user mentionedconst mentionedIds =getMentionedIdsFromCommentBody(comment.body); // ["jody.hekla@example.com", "tatum.paolo@example.com"]console.log(mentionedIds);
Using the new REST API and comment transformation functions, it’s easier than
ever to send formatted email notifications. You can trigger events when comments
are created using our webhooks, and in particular the
CommentCreatedEvent.
Here’s an example of a Next.js route handler that listens for new comments, then
sends a formatted HTML email with Resend.
Huge thanks to everyone who contributed and specifically to
Nimesh Nayaju and
Marc Bouchenoire for their work on the
REST API methods and the Comments functions respectively. Keep checking out the
changelog for
the full release notes–see you next time!