Skip to content

Instantly share code, notes, and snippets.

@srflp
Last active August 13, 2025 12:37
Show Gist options
  • Save srflp/931d3e435656b31c9d15258e2d91f0b5 to your computer and use it in GitHub Desktop.
Save srflp/931d3e435656b31c9d15258e2d91f0b5 to your computer and use it in GitHub Desktop.
Liveblocks: A script that pushes 150 list items simultaneously using 150 simultaneous mutateStorage calls. The expected result is 150 items being pushed to the array, since we don't override the LiveList, we only mutate it by pushing items. The real result is not all items being pushed.
import { Liveblocks, LiveList } from "@liveblocks/node";
/**
* This script is used to push a list of items to a Liveblocks room.
* We're mutating the list, by pushing 150 items, so the final list should have 150 items,
* but we never get 150 items back, it's always less.
*
* For running the script use Node 24 (it allows to run .ts directly using node only):
*
* ```node
* LB_SECRET=<your-secret> node pushLiveListItems.ts
* ```
*/
const numberOfItemsToInsert = 150;
const roomId = "live-list-insertion-test" + crypto.randomUUID();
const secret = process.env.LB_SECRET;
if (!secret) {
console.error("LB_SECRET environment variable is not set.");
process.exit(1);
}
const liveblocks = new Liveblocks({ secret });
async function pushOne(index: number): Promise<void> {
await liveblocks.mutateStorage(roomId, async ({ root }) => {
const list = root.get("list") as LiveList<string>;
list.push(index.toString());
});
}
async function main() {
// Initialize the room
await liveblocks.getOrCreateRoom(roomId, {
defaultAccesses: [],
});
await liveblocks.mutateStorage(roomId, async ({ root }) => {
root.set("list", new LiveList<string>([]));
});
// Push the items
console.log(
`Pushing ${numberOfItemsToInsert} item(s) concurrently to room "${roomId}" at path "list"…`
);
const ops = Array.from({ length: numberOfItemsToInsert }, (_, i) =>
pushOne(i)
);
const results = await Promise.allSettled(ops);
// Fetch the storage again and check if all items have been inserted
const { list } = await liveblocks.getStorageDocument(roomId, "json");
const numberOfItemsInserted = (list as string[])?.length ?? 0;
console.log(
`Items count in the list after inserting ${numberOfItemsToInsert} items: ${numberOfItemsInserted}`
);
const succeeded = results.filter((r) => r.status === "fulfilled").length;
const failed = results.length - succeeded;
console.log(
`Succeeded: ${succeeded}, Failed: ${failed}, Missing: ${
numberOfItemsToInsert - numberOfItemsInserted
}`
);
if (failed > 0) {
// Surface at least one failure
for (const r of results) {
if (r.status === "rejected") {
console.error("Example error:", r.reason);
break;
}
}
process.exitCode = 1;
}
}
await main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment