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.
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
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.
Get other users’ presence
Get people’s cursor positions with
liveblocks.others.map(user => user.presence.cursor).