How to use Liveblocks Presence with Redux
In this guide, we’ll be learning how to use Liveblocks Presence with Redux using
the APIs from the [@liveblocks/redux][] package.
This guide assumes you already have Liveblocks set up into your Redux 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
state.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 enhancer to specify which
part of the state maps to the current user’s presence.
Then you can dispatch an action like in any redux app and we will broadcast this cursor to everyone in the room.
Get other users’ presence
Get people’s cursor positions with
liveblocks.others.map(user => user.presence?.cursor). It’s worth noting that a
user presence can be undefined.