Skip to main content
The 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

sid
Sid?
Server-assigned unique identifier for the room
name
String?
Name of the room
metadata
String?
Custom metadata associated with the room
serverVersion
String?
Version of the LiveKit server
serverRegion
String?
Region code where the room is hosted
serverNodeId
String?
Specific node ID within the LiveKit cluster
localParticipant
LocalParticipant
The local participant representing the current user
remoteParticipants
[Participant.Identity: RemoteParticipant]
Dictionary of remote participants in the room, keyed by their identity
activeSpeakers
[Participant]
Array of participants who are currently speaking
connectionState
ConnectionState
Current connection state (.disconnected, .connecting, .connected, .reconnecting, .disconnecting)
isRecording
Bool
Whether the room is currently being recorded
maxParticipants
Int
Maximum number of participants allowed in the room
participantCount
Int
Current number of participants in the room
publishersCount
Int
Number of participants currently publishing tracks
url
String?
User-provided connection URL
connectedUrl
String?
Actual server URL used for the connection (may include regional URL)
token
String?
JWT token used for authentication
delegates
MulticastDelegate<RoomDelegate>
Multicast delegate for room events

Initialization

init(delegate:connectOptions:roomOptions:)

Creates a new Room instance.
delegate
RoomDelegate?
Optional delegate to receive room events
connectOptions
ConnectOptions?
Options for connection behavior (auto-subscribe, protocols, etc.)
roomOptions
RoomOptions?
Options for room behavior (capture settings, adaptive stream, etc.)
let room = Room(delegate: self)

Methods

connect(url:token:connectOptions:roomOptions:)

Connects to a LiveKit room.
url
String
required
WebSocket URL of the LiveKit server (e.g., "wss://your-host.livekit.cloud")
token
String
required
JWT token for authentication and room access
connectOptions
ConnectOptions?
Connection options (overrides options from init)
roomOptions
RoomOptions?
Room options (overrides options from init)
Returns: Throws an error if connection fails
Task {
    do {
        try await room.connect(
            url: "wss://your-host.livekit.cloud",
            token: "your_jwt_token"
        )
        print("Connected to room: \(room.name ?? "unknown")")
    } catch {
        print("Failed to connect: \(error)")
    }
}

disconnect()

Disconnects from the room and cleans up all resources.
Task {
    await room.disconnect()
}

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
Task {
    let roomSid = try await room.sid()
    print("Room SID: \(roomSid)")
}

Usage Example

import LiveKit
import UIKit

class RoomViewController: UIViewController {
    lazy var room = Room(delegate: self)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        Task {
            do {
                try await room.connect(
                    url: "wss://your-host.livekit.cloud",
                    token: "your_jwt_token"
                )
                
                // Enable camera and microphone
                try await room.localParticipant.setCamera(enabled: true)
                try await room.localParticipant.setMicrophone(enabled: true)
            } catch {
                print("Connection failed: \(error)")
            }
        }
    }
}

extension RoomViewController: RoomDelegate {
    func room(_ room: Room, participant: RemoteParticipant, didSubscribeTrack publication: RemoteTrackPublication) {
        print("Subscribed to track: \(publication.name)")
    }
    
    func room(_ room: Room, didUpdateMetadata metadata: String) {
        print("Room metadata updated: \(metadata)")
    }
    
    func room(_ room: Room, didUpdateIsRecording isRecording: Bool) {
        print("Recording state: \(isRecording)")
    }
}

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:
func room(_ room: Room, participant: RemoteParticipant, didSubscribeTrack publication: RemoteTrackPublication) {
    DispatchQueue.main.async {
        // Update UI here
    }
}

See Also

Build docs developers (and LLMs) love