Get started - Get started with JavaScript

In this guide, you’ll learn how to use integrate Liveblocks into your JavaScript application. The @liveblocks/client package provides the Liveblocks Presence and Storage APIs you’ll be integrating with. If you’re using a UI framework such as React or Vue.js, we recommend reading with our dedicated guides below:

This guide assumes that you’re already familiar with JavaScript and that you already have a way to bundle your JavaScript code for the client.

Install Liveblocks into your project

Install the Liveblocks client package

Run the following command to install the Liveblocks client package:

npm install @liveblocks/client

@liveblocks/client lets you connect to Liveblocks servers.

Connect to Liveblocks

To connect to Liveblocks, you need to create a Liveblocks client with createClient from the front-end.

import { createClient } from "@liveblocks/client";
// Create a Liveblocks client// Replace this key with your public key provided at// https://liveblocks.io/dashboard/projects/{projectId}/apikeysconst client = createClient({ publicApiKey: "pk_prod_xxxxxxxxxxxxxxxxxxxxxxxx",});

Liveblocks should now be installed into your project!

Connect to a Liveblocks room

A room is the virtual space where people collaborate. To create a multiplayer experience, you’ll need to connect your users to a Liveblocks room following the instructions below.

Using Client.enter to connect to a room

You can easily connect to a room by using client.enter as shown below.

const room = client.enter("your-room-id", { initialPresence: {} });

Using Liveblocks Client methods

Now that we have our room setup, we can start using Liveblocks Client methods to share any kind of data between users. For instance, Room.subscribe.others allows us to subscribe to the other users updates.

const room = client.enter("your-room-id", { initialPresence: {} });
const container = document.getElementById("root-container");
room.subscribe("others", (others) => { if (others.count === 0) { container.innerHTML = "You're the only one here."; } else if (others.count === 1) { container.innerHTML = "There is one other person here."; } else { container.innerHTML = "There are " + others.count + " other people here."; }});

Bonus: setup authentication endpoint

Using the public key allows you to use Liveblocks without implementing your own authentication endpoint. It’s great for prototyping and marketing websites.

If you want to implement your own security and define if the current user has access to a specific room, you can follow the Authentication guide.

If you are using Express, you can also check the Express Authentication guide.

© 2023 Liveblocks Inc.Edit this page