How to use Liveblocks presence with Zustand

In this guide, we’ll be learning how to use Liveblocks presence with Zustand using the APIs from the @liveblocks/zustand package.

Install Liveblocks

This guide assumes you already have Liveblocks set up into your Zustand store. If you don’t make sure to follow these quick steps to get started first.

Get other users in the room

Need help troubleshooting presence?

Try the Liveblocks DevTools extension to inspect and debug your collaborative experiences as you build them, in realtime.

If you want to list all the people connected to the room, you can use liveblocks.others to get an array of the other users in the room.

Update user presence

To create immersive multiplayer experiences, it’s helpful for each person in the room to share their real‑time state with other connected users. That real‑time state often corresponds to a cursor position or even the item a user has currently selected. We call this concept “presence”.

For instance, to share the cursor’s position in real‑time with others, we’re going to add a new presenceMapping option to our liveblocks middleware configuration to specify which part of the state maps to the current user’s presence. In this case, we’re updating the cursor position in our store in the onPointerMove event listener in our React component.

src/store.ts
src/App.tsx

Get other users’ presence

Get people’s cursor positions with liveblocks.others.map(user => user.presence.cursor).

src/App.tsx