Start or Join Room - iOS
After the successful installation of VideoSDK, the next step is to integrate VideoSDK features with your webApp/MobileApp.
To talk with other participants by audio or video, you will need to join the room.
This guide will provide an overview of how to configure, initialize and join a VideoSDK room.

1. Configuration
To configure a room, you will need a generated token and a roomId, as described in Server Setup. This code snippet calls the API on your local server.
Scenario 1 - Suppose you don't have a roomId. You can generate one by calling the create-meeting API.
Scenario 2 - Suppose you have roomId, now you don't have to call create-meeting API to generate roomId, instead you can call validate-meeting API to validate roomId.
The token generation API is necessary for both scenarios.
import Foundation
import VideoSDKRTC
// Update server url here.
let LOCAL_SERVER_URL = "http://192.168.0.101:9000"
class APIService {
class func getToken(completion: @escaping (Result<String, Error>) -> Void) {
var url = URL(string: LOCAL_SERVER_URL)!
url = url.appendingPathComponent("get-token")
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data, let token = data.toJSON()["token"] as? String {
completion(.success(token))
} else if let err = error {
completion(.failure(err))
}
}
.resume()
}
class func createRoom(token: String, completion: @escaping (Result<String, Error>) -> Void) {
var url = URL(string: LOCAL_SERVER_URL)!
url = url.appendingPathComponent("create-meeting")
let params = ["token": token]
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data, let roomId = data.toJSON()["meetingId"] as? String {
completion(.success(roomId))
} else if let err = error {
completion(.failure(err))
}
}
.resume()
}
}
2. Initialization
After configuration, you will have to initialize the
room by providing roomId, token, name, micEnabled, camEnabled & maxResolution.

VideoSDK.createRoom(roomId:token:...) takes the token as a parameter and returns a Room. It throws ERROR_INVALID_PARAMETER for an empty room ID or token, so call it with try.
// import sdk
import VideoSDKRTC
class RoomViewController: UIViewController {
// room
private var room: Room?
override func viewDidLoad() {
super.viewDidLoad()
do {
// create a new room instance
room = try VideoSDK.createRoom(
roomId: "<roomId>", // required
token: "<Authentication-token>", // required, the token you got earlier
participantName: "<participantName>", // optional, default: a random name
micEnabled: true, // optional, default: true
camEnabled: true // optional, default: true
)
} catch {
print("Room setup failed: \(error)")
}
}
}
3. Join
After configuration & initialization, the third step is to call join() to join a room.
After joining, you will be able to manage participants in a room.

join() is async throws. Call it with try await inside a Task.
// join
Task {
do {
try await room?.join()
} catch {
print("Join failed: \(error)")
}
}
- A second
join()throwsERROR_OPERATION_IN_PROGRESSwhile a join is running, andERROR_ALREADY_IN_REQUESTED_STATEonce you are in the room. If the join fails,roomStatechanges toFAILEDand the error is thrown. - Treat
onRoomJoined(), not the return ofjoin(), as the sign that you are in the room. For example, ifleave()cancels a pending join,join()returns without throwing. join()returns before the mic and camera frommicEnabledandcamEnabledstart. If one of them fails to start, for example because access was denied, the error arrives inonError(error:).
API Reference
The API references for all the methods and events utilised in this guide are provided below.
Got a Question? Ask us on discord

