The Agora Web SDK uses the AgoraRTCClient object to manage the behavior of a local user in the target channel. Create an AgoraRTCClient
object before joining the target channel.
Call AgoraRTC.createClient to create an AgoraRTCClient
object. Specify the video codec type (codec
) and the channel profile (mode
) when creating AgoraRTCClient
.
const client = AgoraRTC.createClient({
codec: "vp8",
mode: "rtc",
});
The Agora Web SDK supports two video codec types: "vp8"
(VP8) and "h264"
(H.264). The codec
setting only affects the video encoding type of the published video. For the subscriber, as long as it supports decoding the video codec type, the subscription can be completed successfully. For example, the desktop version of Chrome 58 or later supports both VP8 and H.264, while while Safari versions before 12.1 do not support VP8; if two hosts in a channel publish VP8 and H.264 videos, respectively, the audience using desktop Chrome 58 can decode the videos of both hosts, but the audience using Safari versions before 12.1 can only decode the H.264 video.
Different browsers and devices support the two codec types differently. The following table lists the codec support of different browsers:
Browser | VP8 | H.264 |
---|---|---|
Desktop Chrome 58+ | ✔ | ✔ |
Firefox 56+ | ✔ | ✔* |
Safari 12.1+ | ✔ | ✔ |
Safari < 12.1 | ✘ | ✔ |
Android Chrome 58+ | ✔ | ?* |
The channel profile is a configuration that Agora uses to apply optimized algorithms for different real-time scenarios. Agora Web SDK supports two channel profiles: "rtc"
(communication profile) and "live"
(live streaming profile).
Communication profile
The communication profile applies to scenarios where all users in the channel often need to communicate with each other, but the total number of users is not too large, such as team meetings and online chats.
Live streaming profile
The live streaming profile applies to scenarios where the channel has few publishers but many subscribers. In this profile, the SDK defines two user roles: the audience and the host. The host can both send and receive audio and video, but the audience can only receive. You can set the role parameter of createClient
to specify the user role, and call setClientRole to dynamically change the user role.
After creating the AgoraRTCClient
object, call AgoraRTCClient.join to join a channel.
Promise
or async/await
.Note the setting of the fourth parameter uid
of the join
method. If no value is assigned, Agora automatically assigns a unique number ID for the local user. You can also specify a number user ID.
// Automatically assign a number user ID.
const uid = await client.join("APPID", "CHANNEL", "TOKEN");
// Specify a number user ID.
await client.join("APPID", "CHANNEL", "TOKEN", 393939);
Call AgoraRTCClient.leave to leave the channel. You can call this method at any time, including when the user is joining a channel or reconnecting. After calling leave
, the SDK immediately destroys objects related to the current channel, including subscribed remote user objects, remote track objects, and objects that save connection states. To join the channel again, call join
after you call leave
.
Promise
or async/await
.await client.leave();
See Channel Connection.
When joining a channel, the SDK may throw the following errors due to improper use of the SDK or network abnormalities:
INVALID_PARAMS
: The parameters are incorrect, for example, an invalid token is provided.INVALID_OPERATION
: An incorrect operation. This error is usually caused by joining a channel repeatedly. Ensure that you call leave
before joining a channel again.OPERATION_ABORTED
: The joining is aborted, which means that leave
is called before the method call of join
succeeds.UNEXPECTED_RESPONSE
: The Agora server returns an unexpected response, usually because the App ID or token authentication fails. For example, you have enabled the App Certificate but do not pass a token in join
.UID_CONFLICT
: Multiple AgoraRTCClient
objects use the same user ID.