In real-time communications, a user can be in various channel connection states. This page shows how the 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 Agora Web SDK added the Client.on("connection-state-change")
callback in v2.5.1. This callback occurs when the connection state of the local user changes.
With this callback, the SDK provides the following connection states of the users:
Client.leave
The following diagram shows how each connection state is defined:
During communications, you can also call the getConnectionState
method to get the current connection state.
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)
and Client.on("connected")
callbacks.Client.on("peer-online")
callback.Stream.publish
method to publish the local stream.Client.on("stream-added")
, indicating the UID 1 has published a stream. UID 2 can call the Stream.subscribe
method to subscribe to UID 1's stream.Client.on("stream-removed")
. Meanwhile the SDK continues to try rejoining the channel.Client.on("connection-state-change", CONNECTING)
.Client.on("peer-leave")
callback. Meanwhile the SDK continues to try rejoining the channel.Client.on("connection-state-change", CONNECTED)
.Client.on("peer-online")
callback of UID 1, 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?.