Overview
The LiveKit SDKs use events to communicate with the application changes that are taking place in the room.
There are two kinds of events, room events and participant events. Room events are emitted from the main Room
object, reflecting any change in the room. Participant events are emitted from each Participant
, when that specific participant has changed.
Room events are generally a superset of participant events. As you can see, some events are fired on both Room
and Participant
; this is intentional. This duplication is designed to make it easier to componentize your application. For example, if you have a UI component that renders a participant, it should only listen to events scoped to that participant.
Declarative UI
Event handling can be quite complicated in a realtime, multi-user system. Participants could be joining and leaving, each publishing tracks or muting them. To simplify this, LiveKit offers built-in support for declarative UI for most platforms.
With declarative UI you specify the how the UI should look given a particular state, without having to worry about the sequence of transformations to apply. Modern frameworks are highly efficient at detecting changes and rendering only what's changed.
We offer a few hooks and components that makes working with React much simpler.
- useParticipant - maps participant events to state
- useTracks - returns the current state of the specified audio or video track
- VideoTrack - React component that renders a video track
- RoomAudioRenderer - React component that renders the sound of all audio tracks
const Stage = () => {const tracks = useTracks([Track.Source.Camera, Track.Source.ScreenShare]);return (<LiveKitRoom{/* ... */}>// Render all video{tracks.map((track) => {<VideoTrack trackRef={track} />;})}// ...and all audio tracks.<RoomAudioRenderer /></LiveKitRoom>);};function ParticipantList() {// Render a list of all participants in the room.const participants = useParticipants();<ParticipantLoop participants={participants}><ParticipantName /></ParticipantLoop>;}
Events
This table captures a consistent set of events that are available across platform SDKs. In addition to what's listed here, there may be platform-specific events on certain platforms.
Event | Description | Room Event | Participant Event |
---|---|---|---|
ParticipantConnected | A RemoteParticipant joins after the local participant. | ✔️ | |
ParticipantDisconnected | A RemoteParticipant leaves | ✔️ | |
Reconnecting | The connection to the server has been interrupted and it's attempting to reconnect. | ✔️ | |
Reconnected | Reconnection has been successful | ✔️ | |
Disconnected | Disconnected from room due to the room closing or unrecoverable failure | ✔️ | |
TrackPublished | A new track is published to room after the local participant has joined | ✔️ | ✔️ |
TrackUnpublished | A RemoteParticipant has unpublished a track | ✔️ | ✔️ |
TrackSubscribed | The LocalParticipant has subscribed to a track | ✔️ | ✔️ |
TrackUnsubscribed | A previously subscribed track has been unsubscribed | ✔️ | ✔️ |
TrackMuted | A track was muted, fires for both local tracks and remote tracks | ✔️ | ✔️ |
TrackUnmuted | A track was unmuted, fires for both local tracks and remote tracks | ✔️ | ✔️ |
LocalTrackPublished | A local track was published successfully | ✔️ | ✔️ |
LocalTrackUnpublished | A local track was unpublished | ✔️ | ✔️ |
ActiveSpeakersChanged | Current active speakers has changed | ✔️ | |
IsSpeakingChanged | The current participant has changed speaking status | ✔️ | |
ConnectionQualityChanged | Connection quality was changed for a Participant | ✔️ | ✔️ |
ParticipantAttributesChanged | A participant's attributes were updated | ✔️ | ✔️ |
ParticipantMetadataChanged | A participant's metadata was updated | ✔️ | ✔️ |
RoomMetadataChanged | Metadata associated with the room has changed | ✔️ | |
DataReceived | Data received from another participant or server | ✔️ | ✔️ |
TrackStreamStateChanged | Indicates if a subscribed track has been paused due to bandwidth | ✔️ | ✔️ |
TrackSubscriptionPermissionChanged | One of subscribed tracks have changed track-level permissions for the current participant | ✔️ | ✔️ |
ParticipantPermissionsChanged | When the current participant's permissions have changed | ✔️ | ✔️ |