This page shows how the Agora Web SDK determines the connection state of a user and transitions between the states.
To help you get the connection state of each user in the channel, the Web SDK provides the AgoraRTCClient.connectionState
property and the Client.on("connection-state-change")
callback. This callback occurs when the connection state of the local user changes.
The connection between the SDK and the Agora server has the following states:
"DISCONNECTED"
: The SDK is disconnected from the server. This is the initial state before you call join
.
leave
."CONNECTING"
: The SDK is connecting to the server. The SDK enters this state when you call join
.
"CONNECTED"
: The SDK is connected to the server and joins a channel. The user can now publish streams or subscribe to streams in the channel.
"RECONNECTING"
: The SDK is reconnecting to the server. If the connection is lost because the network is down or switched, the SDK enters this state.
"DISCONNECTING"
: The SDK is disconnecting from the server. The SDK enters this state when you call leave
.
The following diagram shows how each connection state is defined:
When network interruption occurs, the SDK automatically tries reconnecting to the server.
The following diagram shows the callbacks received by UID 1 and UID 2, where UID 1 joins the channel, gets a network exception, loses connection, and rejoins the channel.
Where:
Client.join
request from UID 1.Client.on("connection-state-change", CONNECTING)
callback. When successfully joining the channel, UID 1 receives the Client.on("connection-state-change", CONNECTED)
callback.AgoraRTCClient.on("user-joined")
callback.Stream.publish
to publish the local stream.AgoraRTCClient.on("user-published")
callback, indicating UID 1 has published a stream. UID 2 can call Stream.subscribe
to subscribe to UID 1's stream.Client.on("connection-state-change", RECONNECTING)
callback.AgoraRTCClient.on("user-unpublished")
.AgoraRTCClient.on("user-left")
callback.Client.on("connection-state-change", CONNECTED)
.AgoraRTCClient.on("user-joined")
callback, indicating that UID 1 is back in the channel.For how connection states change when the process gets killed, see FAQ: Does Agora have reconnection mechanisms?