Skip to main content
Android
iOS
macOS
Web
Windows
Electron
Flutter
React Native
Python
React JS
Unity
Unreal Engine
Unreal (Blueprint)

Connection status management

In real-time audio and video scenarios, the connection state between the app and Agora SD-RTN™ changes as the client joins or leaves the channel. The connection may also be interrupted due to network or authentication issues.

This page describes the various channel connection states, reasons for state changes, and how to handle these changes to better manage your users and troubleshoot network faults.

Understand the tech

The following figure shows the various connection states and how the connection state changes between a user joining and leaving a channel:

Connection State

Disconnection and reconnection

During the communication process, if the user is disconnected due to network problems, the SDK automatically enables the disconnection and reconnection mechanism. The following figure shows the callbacks received by the local user UID1 and the remote user UID2 when:

  • A local user joins the channel
  • A network exception occurs
  • The connection is interrupted
  • The user rejoins the channel

Disconnection Connection

In the diagram:

  • T0: The Voice SDK receives a joinChannelByToken request from UID1.

  • T1: After 200 ms of calling joinChannelByToken, UID1 joins the channel. At the same time, UID1 receives connectionChangedToState(Connecting, Connecting) callback. After successfully joining the channel, UID1 receives connectionChangedToState(Connected, JoinSuccess) and didJoinChannel callbacks.

  • T2: Due to the transmission delay between networks, UID2 observes a delay of about 100 milliseconds for UID1 to join the channel, and at this point UID2 receives a didJoinedOfUid callback.

  • T3: When UID1 client's connection deteriorates due to a network issue or some other reason, the Voice SDK automatically tries to rejoin the channel.

  • T4: If UID1 does not receive any data from the server for 4 consecutive seconds, UID1 receives the connectionChangedToState(Reconnecting, Interrupted) callback. Meanwhile, the Voice SDK continues to try to rejoin the channel.

  • T5: If UID1 does not receive any data from the server for 10 consecutive seconds after receiving connectionChangedToState(Reconnecting, Interrupted), UID1 receives the rtcEngineConnectionDidLost callback. Meanwhile, the Voice SDK continues to try to rejoin the channel.

  • T6: If UID2 does not receive any data from UID1 for 20 consecutive seconds, the Voice SDK determines that UID1 is offline. So UID2 receives the didOfflineOfUid callback.

  • T7: If UID1 fails to rejoin the channel for 20 consecutive minutes after receiving connectionChangedToState(Reconnecting, Interrupted), the SDK stops retrying. UID1 receives the connectionChangedToState(Failed, JoinFailed) callback and the user must exit and then rejoin the channel.

Prerequisites

Ensure that you have implemented the SDK quickstart project.

Implement connection status management

When the connection status changes, Voice SDK sends a connectionChangedToState callback. This section describes how to use the connectionChangedToState callback to monitor changes in a channel's connection state.

In your project, open the file with AgoraRtcEngineDelegate and copy the following code into the AgoraRtcEngineDelegate initialization process:

func rtcEngine(_ engine: AgoraRtcEngineKit, connectionChangedTo state: AgoraConnectionState, reason: AgoraConnectionChangedReason) {
LogUtils.log(message: "Connection state changed: \(state) \(reason)", level: .info)
}
Copy

The code logs the connection state changes and the reason for the state change. The reason parameter in connectionChangedToState explains why the connection state changed and helps you troubleshoot your network.

Reference

This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.

Connection states

The app may have the following connection states before joining, during a session, and after leaving a channel:

Connection stateDescription
DisconnectedInitial connection state. Usually occurs:
  • Before calling joinChannelByToken.
  • After calling leaveChannel.
ConnectingThe transient state after calling joinChannelByToken.
ConnectedOccurs after the app successfully joins a channel. The SDK also triggers the didJoinChannel callback to report that the local client has joined the channel. At this point, the user can publish or subscribe to the audio and video in the channel.
ReconnectingOccurs when the connection is interrupted. The SDK automatically tries to reconnect after an interruption.
  • If the client successfully rejoins the channel, the SDK triggers the didRejoinChannel callback.
  • If the channel is not rejoined within 10 seconds, the SDK triggers connectionChangedToState(Reconnecting, Lost), and continues trying to rejoin the channel.
FailedConnection failed. Occurs when the SDK is unable to join a channel for 20 minutes and stops attempting to reconnect to the channel. In this case, call leaveChannel to leave the current channel, and then call joinChannelByToken to join the channel again.

State description and troubleshooting

The reason parameter in connectionChangedToState describes the reason for the connection state change.

The following table maps the relationships between different connection states and the causes of state change, as well as how to handle the situation when network outages occur:

Connection stateDescription and troubleshooting guide
Disconnected
  • LeaveChannel (5): The user leaves the channel.
  • InvalidToken (8):The token is invalid. Please use a valid token to join the channel.
ConnectingConnecting (0): The app is trying to join the Agora channel.
ConnectedJoinSuccess (1): The app has successfully joined the channel.
Reconnecting
  • Interrupted (2): When the network connection is interrupted, the SDK automatically reconnects to the channel and the connection state continues to change. For details on how the connection state changes during automatic reconnection, see Disconnection and reconnection.
  • Lost (16): The SDK lost connection with the server.
  • SettingProxyServer (11): The SDK attempts to reconnect because a proxy server is configured.
  • ClientIpAddressChanged (13): The client IP address has changed. If this status code occurs multiple times, prompt the user to rejoin the channel after changing networks.
  • KeepAliveTimeout (14): The keep-alive connection between the SDK and the server has timed out, causing the SDK to enter an automatic reconnection state.
  • RenewToken (12): Updating the token has caused a change in the network connection status.
Failed
  • BannedByServer (3): The user is banned by the server.
  • JoinFailed (4): The SDK stopped trying to reconnect after continued failed attempts to join the channel for 20 minutes. Call leaveChannel to leave the current channel and then call joinChannelByToken to rejoin the channel.
  • InvalidAppId (6): The app ID is invalid. Use a valid app ID to join the channel.
  • InvalidChannelName (7): The channel name is invalid. Please check if the channel name contains illegal characters and use a valid channel name to join the channel.
  • TokenExpired (9): The token has expired. Obtain a new token from the app server, and then call joinChannelByToken to rejoin the channel.
  • RejectedByServer (10): The user is banned by the server. May also occurs under the following circumstances:
    • The app calls joinChannelByToken again after the local user has joined the channel.
    • The app called startEchoTest, but did not call stopEchoTest to end the echo test.

API reference

Voice Calling