Room class is the primary interface for connecting to a LiveKit server, managing participants, and handling real-time audio, video, and data communication.
Overview
Room manages the WebRTC connection to a LiveKit server and provides access to local and remote participants, tracks, and room state. It implements ObservableObject for SwiftUI integration.
Properties
Server-assigned unique identifier for the room
Name of the room
Custom metadata associated with the room
Version of the LiveKit server
Region code where the room is hosted
Specific node ID within the LiveKit cluster
The local participant representing the current user
Dictionary of remote participants in the room, keyed by their identity
Array of participants who are currently speaking
Current connection state (
.disconnected, .connecting, .connected, .reconnecting, .disconnecting)Whether the room is currently being recorded
Maximum number of participants allowed in the room
Current number of participants in the room
Number of participants currently publishing tracks
User-provided connection URL
Actual server URL used for the connection (may include regional URL)
JWT token used for authentication
Multicast delegate for room events
Initialization
init(delegate:connectOptions:roomOptions:)
Creates a new Room instance.Optional delegate to receive room events
Options for connection behavior (auto-subscribe, protocols, etc.)
Options for room behavior (capture settings, adaptive stream, etc.)
Methods
connect(url:token:connectOptions:roomOptions:)
Connects to a LiveKit room.WebSocket URL of the LiveKit server (e.g.,
"wss://your-host.livekit.cloud")JWT token for authentication and room access
Connection options (overrides options from init)
Room options (overrides options from init)
Throws an error if connection fails
disconnect()
Disconnects from the room and cleans up all resources.sid()
Async version that waits for the room SID to be assigned by the server. Returns:Sid - The server-assigned room identifier
Throws: Error if SID is not received within timeout
Usage Example
Thread Safety
Room can be accessed from any thread. Delegate callbacks are invoked on the SDK’s internal thread, so UI updates should be dispatched to the main thread: