During a real-time engagement session, the connection state between an app client and Agora SD-RTNTM 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, to help you better manage users and troubleshoot when network interruption occurs.
When the connection state changes, Agora sends the connectionChangedToState
callback. The following diagram illustrates the various 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, lises connection, and rejoins the channal.
In the above diagram:
joinChannel
request from UID 1.joinChannel
, UID 1 joins the channel. In the process, UID 1 also receives the connectionChangedToState(Connecting, Connecting)
callback. When successfully joining the channel, UID 1 receives the connectionChangedToState(Connected, JoinSuccess)
and didJoinSuccess
callbacks.didJoinedOfUid
callback.connectionChangedToState(Reconnecting, Interrupted)
; meanwhile the SDK continues to try rejoining the channel.connectionChangedToState(Reconnecting, Interrupted)
, UID 1 receives the rtcEngineConnectionDidLost
callback; meanwhile the SDK continue to try rejoining the channel.didOfflineOfUid
.connectionChangedToState(Reconnecting, Interrupted)
, the SDK stops trying. UID 1 receives the connectionChangedToState(Failed, JoinFailed)
callback. UID 1 needs to leave the channel and call the joinChannel
method to join the channel.Before proceeding, ensure that you have a project that has implemented the basic real-time engagement functionality.
This section introduces how to use the connectionChangedToState
callback to monitor channel connection state change.
In your Agora project, open the file used to manage AgoraRtcEngineDelegate
and copy the following code to the AgoraRtcEngineDelegate
initialization process.
func rtcEngine(_engine: AgoraRtcEngineKit, connectionChangedToState
state: AgoraConnectionStateType
reason: AgoraConnectionChangedReadon) {
LogUtils.log(message: "Connection state changed: \(state) \(reason)")
}
You can read the current connection state and why the state changes from the log.
To help you troubleshoot network interruption, connectionChangedToState
also has the reason
parameter that explains why the connection state changes. For the reasons for the change and how to troubleshoot, refer to Connection state change reasons.
This section provides reference information you may need when managing the channel connection state.
As shown in the diagram above, an app can have the following five connection states before and after the app joins and leaves the channel:
Connection state | Description |
---|---|
Disconnected | The initial connection state. It usually occurs under the following circumstances:
|
Connecting | This is the instantaneous state after calling the didJoinChannel method. |
Connected | This state occurs after the app successfully joins the channel. The SDK also triggers didJoinChannel to report that the local client has joined the channel. Users can now publish or subcribe to audio and video in the channel. |
Reconnecting | This state occurs when the connection is interrupted. The SDK automatically tries to reconnect after the 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 joinChannelByToken to join the channel again. |
The following table lists the mapping relationship between the connection state, reasons for the state change, and how to troubleshoot when network interruption occurs.
Connection state | Reasons for the state change and how to troubleshoot |
---|---|
Disconnected |
|
Connecting |
|
Connected |
|
Reconnecting |
|
Failed |
|