Another key feature of Liveblocks is the ability to
broadcast custom events to other users. Events are ephemeral, and
are sent to all users currently connected to the room, which makes them useful
Creating state controls, for example “next/previous slide” buttons in
Letting other users know when to revalidate data after a change
The first step in broadcasting events is setting up your types. Let’s navigate
to liveblocks.config.ts and create a RoomEvent type for our toasts.
RoomEvent represents the object that will be broadcast to other users
currently connected to the room. We’ll give it a type along with a message
that can appear in the toast notification.
We’re now sending an event, but we’re not listening for any. Import
useEventListener and add it to the component. In the callback, we’re
checking if the received event is a toast notification, and if it is, we’re
calling a function that sends a toast notification with the message.