During a real-time engagement session, the connection state between an app client and Agora SD-RTN™TM changes as the client joins and leaves an Agora channel, or goes offline due to network or authentication issues.
This page explains the connection states, how they change, and why they change, in order to help you better manage users and troubleshoot when network interruptions occur.
When the connection state changes, Agora sends the onConnectionStateChanged
callback. The following diagram illustrates the various connection states and how the states change as a client app joins and leaves a channel:
When the network connection is interrupted, the SDK automatically tries to reconnect to the server. The following diagram shows the callbacks received by the local user (UID1) and the remote user (UID2) when the local user joins the channel, gets a network exception, loses connection, and rejoins the channel.
As shown in the preceding diagram:
joinChannel
request from UID 1.joinChannel
, UID 1 joins the channel. In the process, UID 1 also receives the onConnectionStateChanged(CONNECTING, CONNECTING)
callback. When successfully joining the channel, UID 1 receives the onConnectionStateChanged(CONNECTED, JOIN_SUCCESS)
and the onJoinChannelSuccess
callbacks.onUserJoined
callback.onConnectionStateChanged(RECONNECTING, INTERRUPTED)
callback; meanwhile the SDK continues to try rejoining the channel.onConnectionStateChanged(RECONNCTING, INTERRUPTED)
, UID 1 receives the onConnectionLost
callback; meanwhile the SDK continue to try rejoining the channel.onUserOffline
callback.onConnectionStateChanged(RECONNCTING, INTERRUPTED)
, the SDK stops trying. UID 1 receives the onConnectionStateChanged(FAILED, JOIN_FAILED)
callback. UID 1 needs to leave the channel and call the joinChannel
method to join the channel.Before proceeding, ensure that you have implemented basic real-time functions in your project. See Start a Call.
This section introduces how to use the onConnectionStateChanged
callback to monitor the channel connection state change.
In your Agora project, open the file used to manage the IRtcEngineEventHandler
file and add onConnectionStateChanged
to the IRtcEngineEventHandler
initialization process.
You can read the current connection state and why the state changes from the log.
To help you troubleshoot network interruptions, connectionChangedToState
also has the reason
parameter to explain why the connection state changes. For more information on these reasons and troubleshooting , refer to Reasons for the state change and how to troubleshoot.
This section provides reference information for managing the channel connection state.
As shown in the preceding diagram, an app might have the following five connection states before and after the app joins and leaves the channel:
Connection states | Description |
---|---|
Disconnected | The initial connection state. It usually occurs under the following circumstances:
|
Connecting | This is the instantaneous state after calling the joinChannel method. |
Connected | This state occurs after the app successfully joins the channel. The SDK also triggers the onJoinChannelSuccess callback to report that the local client has joined the channel. Users can now publish or subscribe to the audio and video in the channel. |
Reconnecting | This state occurs when the connection is interrupted. The SDK automatically tries to reconnect after an interruption.
|
Failed | The connection fails. This state occurs when the SDK cannot join the channel in 20 minutes and the SDK stops reconnecting to the channel. When this state occurs, call leaveChannel to leave the current channel, and call joinChannel to join the channel again. |
To help you troubleshoot network interruptions, onConnectionStateChanged
also has the reason
parameter that explains why the connection state changes.
The following table lists the mapping relationship between the connection state, reasons for the state change, and how to troubleshoot when an network interruption occurs.
Connection states | Reasons for the state change and how to troubleshoot |
---|---|
Disconnected |
|
Connecting |
|
Connected |
|
Reconnecting |
|
Failed |
|