iOS

Updated

API reference for the Agora Signaling iOS SDK for Swift.

This is the Swift API reference. For Objective-C, see the macOS SDK API reference.

The Signaling SDK API reference is divided into the following sections:

Setup

The API reference for the Signaling SDK documents interface descriptions, methods, basic usage, and return values of the Signaling APIs.

AgoraRtmClientConfig

Description

Use the AgoraRtmClientConfig to set additional properties for Signaling initialization. These configuration properties take effect throughout the lifecycle of the Signaling client and affect the behavior of the Signaling client.

Method

You can create an AgoraRtmClientConfig instance as follows:

AgoraRtmClientConfig(
    appId: String,
    userId: String,
    useStringUserId: Bool,
    ispPolicyEnabled: Bool,
    multipath: Bool,
    presenceTimeout: UInt32,
    heartbeatInterval: UInt32,
    reconnectTimeout: UInt32,
    areaCode: AgoraRtmAreaCode,
    proxyConfig: AgoraRtmProxyConfig?,
    protocolType: AgoraRtmProtocolType,
    logConfig: AgoraRtmLogConfig?,
    encryptionConfig: AgoraRtmEncryptionConfig?,
    privateConfig: AgoraRtmPrivateConfig?
)
PropertiesTypeRequiredDefaultDescription
appIdStringRequired-App ID obtained when creating a project in the Agora Console.
userIdStringRequired-User ID for identify a user or a device. To distinguish each user or device, you need to ensure that the userId parameter is globally unique and remains unchanged throughout the user or device's lifecycle.
multipathBoolRequiredfalseWhether to enable dual-network transmission.
areaCodeAgoraRtmAreaCodeOptionalGLOBService area code, you can choose according to the region where your business is deployed. See AgoraRtmAreaCode.
protocolTypeAgoraRtmProtocolTypeOptionaltcpUdpProtocol types for message transmission. Signaling by default utilizes one-way TCP and one-way UDP protocols for transmission, but you have the flexibility to modify the protocol types based on your requirements. See AgoraRtmProtocolType.
presenceTimeoutunsigned intOptional300Presence timeout in seconds, and the value range is [5,300]. This parameter refers the delay imposed by the Signaling server before sending a remoteConnectionTimeout event notification to other users once it determines that a client has timed out. If the client reconnects and returns to the channel within the specified time, the Signaling server does not send the remoteConnectionTimeout event notification to other participants or delete the temporary user data associated with the user.
heartbeatIntervalunsigned intOptional5Heartbeat interval in seconds, and the value range is [5,1800]. This parameter refers to the time interval at which the client sends heartbeat packets to the Signaling server. If the client fails to send heartbeat packets to the Signaling server within the specified time, the Signaling server determines that the client has timed out. Please note that this parameter affects the PCU count, which in turn affects billing.
reconnectTimeoutuint32_tOptional0SDK connection timeout in seconds. The value range is [15,3600]. The default value is 0 which means no timeout; the SDK keeps retrying until successful. This parameter applies to both the initial login to the Signaling service and reconnection after disconnection:
- If the initial login times out, the SDK triggers the completion callback with errorInfo as AgoraRtmErrorLoginTimeout, and also triggers the didReceiveLinkStateEvent callback with currentState as AgoraRtmLinkStateFailed and reasonCode as AgoraRtmLinkStateChangeReasonLoginTimeout.
- If reconnection times out, the SDK triggers the didReceiveLinkStateEvent callback with currentState as AgoraRtmLinkStateFailed and reasonCode as AgoraRtmLinkStateChangeReasonReconnectTimeout.
useStringUserIdBOOLOptionaltrueWhether to use string-type user IDs:
- true: Use string-type user IDs.
- false: Use number-type user IDs. The SDK automatically converts string-type user IDs to number-type ones. ones. In this case, the userId parameter must be a numeric string (for example, "123456"), otherwise initialization fails.When using Agora RTC and Signaling products at the same time, it is necessary to ensure that the userId parameter is consistent.
ispPolicyEnabledBoolOptionalfalseWhether to enable the ISP domain policy restriction. In IoT scenarios, devices may be restricted by Internet Service Providers (ISPs). Use this field to set the SDK's connection mode to connect to servers with domains registered with the operator or those in the IP whitelist:
- true: Enable the ISP domain policy restriction. This setting applies to scenarios where IoT devices use IoT SIM cards for network access. The SDK will only connect to servers with domains registered with the operator or those in the IP whitelist.
- false: (Default) Disable the ISP domain policy restriction. This setting applies to most common scenarios.
logConfigAgoraRtmLogConfigOptional-Log configuration properties such as the log storage size, storage path, and level.
proxyConfigAgoraRtmProxyConfigOptional-When using the Proxy feature of Signaling, you need to configure this parameter.
encryptionConfigAgoraRtmEncryptionConfigOptional-When using the client-side encryption feature of Signaling, you need to configure this parameter.
privateConfigAgoraRtmPrivateConfigOptional-When using the private deployment feature of Signaling, you need to configure this parameter.
AgoraRtmLogConfig

Use the AgoraRtmLogConfig instance to configure and store local log files named agora.log. During the debugging phase, you can greatly improve efficiency by storing and tracking the running status of the app through logs. If you encounter complex problems and need Agora technical support to assist with the investigation, you need to provide the log information. AgoraRtmLogConfig contains the following properties:

PropertiesTypeRequiredDefaultDescription
filePathStringOptional-Log file storage paths.
fileSizeInKBintOptional1024Log file size in KB, with a value range of [128,1024].
- If the value you enter is less than 128, the SDK sets the value to 128.
- If the value you enter is greater than 1024, the SDK sets the value to 1024.
levelAgoraRtmLogLevelOptionalinfoOutput level of log information. See AgoraRtmLogLevel.
AgoraRtmProxyConfig

Use the AgoraRtmProxyConfig instance to set properties related to the client Proxy service. In some restricted network environments, you might need to use this feature.

Information

Keep your Proxy username and password safe. The Signaling SDK does not parse, store, or forward your username and password in any way. If you modify the proxy settings while the app is running, the settings take effect only after you rest the Signaling client.

AgoraRtmProxyConfig contains the following properties:

PropertiesTypeRequiredDefaultDescription
proxyTypeAgoraRtmProxyTypeOptionalnoneProxy protocol type. See AgoraRtmProxyType.
serverStringRequired-Proxy server domain name or IP address.
portunsigned shortOptional-Proxy listening port.
accountStringOptional-Proxy login account.
passwordStringOptional-Proxy login password.
AgoraRtmEncryptionConfig

Use the AgoraRtmEncryptionConfig instance to set the properties required for client-side encryption. After successfully setting the encryption mode, encryption key, and other related properties, the SDK automatically encrypts and decrypts all messages sent or all statuses set by the user on the client side.

Caution

Once you set the encryption feature, all users must use the same encryption mode and key, otherwise users cannot communicate with each other.

AgoraRtmEncryptionConfig contains the following properties:

PropertiesTypeRequiredDefaultDescription
encryptionModeAgoraRtmEncryptionModeOptionalnoneEncryption mode. See AgoraRtmEncryptionMode.
encryptionKeyStringOptional-User-defined encryption key, unlimited length. Agora recommends using a 32-byte key.
encryptionKdfSaltDataOptional-User-defined encryption salt, length is 32 bytes. Agora recommends using OpenSSL to generate salt on the server side.
AgoraRtmPrivateConfig

Use the AgoraRtmPrivateConfig instance to set the properties required for private deployment.

AgoraRtmPrivateConfig contains the following properties:

PropertiesTypeRequiredDefaultDescription
serviceTypeEnumSet<RtmServiceType>Optional-Service type. See AgoraRtmServiceType.
accessPointHosts[String]Optional-An array of server addresses, where you can fill in domain names or IP addresses.

Basic usage

let config = AgoraRtmClientConfig(appId: "yourAppId", userId: "yourUserId")
config.areaCode = [.CN, .NA]
config.presenceTimeout = 30
config.heartbeatInterval = 10
config.useStringUserId = true

Create an instance

Description

Call the AgoraRtmClientKit initializer to create and initialize the Signaling Client instance.

Information


- Create and initialize a client instance before calling other Signaling APIs.
- To distinguish each user or device, ensure that the userId parameter is globally unique and remains unchanged throughout the user or device's lifecycle.

Method

You can create and initialize an instance as follows:

AgoraRtmClientKit(_ config: AgoraRtmClientConfig, delegate: (any AgoraRtmClientDelegate)?) throws
ParametersTypeRequiredDefaultDescription
configAgoraRtmClientConfigRequired-Initialize the configuration parameters of the Signaling Client. See AgoraRtmClientConfig.
delegateAgoraRtmClientDelegateOptional-Signaling event notification listener settings. See Event listeners.

Basic usage

do {
    let config = AgoraRtmClientConfig(appId: "yourAppId", userId: "uniqueUserId")
    config.areaCode = [.CN, .NA]
    config.presenceTimeout = 30
    config.heartbeatInterval = 10
    config.useStringUserId = true
 // Initializing the RTM client
    let rtmClient = try AgoraRtmClientKit(config, delegate: nil)
    if rtmClient != nil {
        print("RTM Client initialized successfully!")
    }
} catch let error {
    print("Failed to initialize RTM client. Error: \(error)")
}

Return Value

  • Success: Creates a Signaling client instance for subsequent calls to other Signaling APIs.
  • Failure: nil.

Event Listeners

Description

Signaling event notifications.

AgoraRtmClientDelegate

Signaling provides the following event notifications types:

Event TypeDescription
didReceiveMessageEventReceive message event notifications in subscribed message channels and subscribed topics.
didReceivePresenceEventReceive presence event notifications in subscribed message channels and joined stream channels.
didReceiveTopicEventReceive all topic event notifications in joined stream channels.
didReceiveStorageEventReceive channel metadata event notifications in subscribed message channels and joined stream channels, and the user metadata event notification of the subscribed users.
didReceiveLockEventReceive lock event notifications in subscribed message channels and joined stream channels.
didReceiveLinkStateEventReceive event notifications when client connection status changes. For details, see AgoraRtmLinkStateEvent.
tokenPrivilegeWillExpireReceive event notifications when the client tokens are about to expire.

Add event listeners

You can add an event listener object in the following ways:

  • Add an event listener object during initialization.
  • Add one or more event listener objects at any point during the app's lifecycle by calling the addDelegate method.
Adding a delegate during initialization

When initializing the Signaling client instance, add an event listener object as follows:

class EventHandler: NSObject, AgoraRtmClientDelegate {
 // message event handler
  func rtmKit(_ rtmKit: AgoraRtmClientKit, didReceiveMessageEvent event: AgoraRtmMessageEvent) {
    print("Message received from \(event.channelName): \(event.message)")
  }
 // presence event handler
  func rtmKit(_ rtmKit: AgoraRtmClientKit, didReceivePresenceEvent event: AgoraRtmPresenceEvent) {
    print("User \(event.publisher) is now \(event.type)")
  }
}
let eventDelegate = EventHandler()
let rtmClient = try AgoraRtmClientKit(config, delegate: eventDelegate)
Add a delegate any time after initialization

At any point during the app's lifecycle, you can add multiple event listener objects, by calling the addDelegate method multiple times.

// message event handler
class MessageHandler: NSObject, AgoraRtmClientDelegate {
  func rtmKit(_ rtmKit: AgoraRtmClientKit, didReceiveMessageEvent event: AgoraRtmMessageEvent) {
    print("Message received from \(event.channelName): \(event.message)")
  }
}
 // presence event handler
class PresenceHandler: NSObject, AgoraRtmClientDelegate {
  func rtmKit(_ rtmKit: AgoraRtmClientKit, didReceivePresenceEvent event: AgoraRtmPresenceEvent) {
    print("User \(event.publisher) is now \(event.type)")
  }
}
let messageDelegate = MessageHandler()
let presenceDelegate = PresenceHandler()
rtmClient.addDelegate(messageDelegate)
rtmClient.addDelegate(presenceDelegate)

removeDelegate

If you no longer need to listen to a specific event listener, but still need to listen to other event listener objects, you can call the removeDelegate method to remove the specified event listener.

rtmClient.removeDelegate(messageDelegate)
AgoraRtmMessageEvent

Message event.

AgoraRtmMessageEvent contains the following properties:

PropertiesTypeDescription
channelTypeAgoraRtmChannelTypeChannel types. See AgoraRtmChannelType.
channelNameStringChannel name.
channelTopicStringTopic name.
messageAgoraRtmMessageMessage.
publisherStringUser ID of the message publisher.
customTypeStringA user-defined field. Only supports String type.
timestampUInt64The timestamp when the event occurs.

AgoraRtmMessage contains the following properties:

PropertiesTypeDescription
rawDataDataBinary message.
stringDataStringString message.
AgoraRtmPresenceEvent

User presence event.

AgoraRtmPresenceEvent contains the following properties:

PropertiesTypeDescription
typeAgoraRtmPresenceEventTypePresence event type. See AgoraRtmPresenceEventType.
channelTypeAgoraRtmChannelTypeChannel types. See AgoraRtmChannelType.
channelNameStringChannel name.
publisherStringUser ID of the message publisher.
states[String : String]Key-value pair that identifies the user's presence state.
intervalAgoraRtmPresenceIntervalInfoIn the Interval state, the aggregated incremental information of event notifications such as user joining, leaving, timeout, and status change in the previous period of the current channel.
snapshot[AgoraRtmUserState]When the user first joins the channel, the server pushes the snapshot data of all users in the current channel and their statuses to the user.
timestampUInt64The timestamp when the event occurs.

The AgoraRtmPresenceIntervalInfo data type contains the following properties:

PropertiesTypeDescription
joinUserList[String]List of users who joined the channel in the previous cycle.
leaveUserList[String]List of users who left the channel in the previous cycle.
timeoutUserList[String]List of users who timed out joining the channel in the previous cycle.
userStateList[UserState]List of users whose status has changed in the previous cycle. Contains user ID and status key-value pairs.

The AgoraRtmUserState data type contains the following properties:

PropertiesTypeDescription
userIdStringUser ID.
states[String : String]List of online users and their temporary state information in a specified channel.
AgoraRtmTopicEvent

Topic event.

AgoraRtmTopicEvent contains the following properties:

PropertiesTypeDescription
typeAgoraRtmTopicEventTypeTopic event type. See AgoraRtmTopicEventType.
channelNameStringChannel name.
publisherStringUser ID.
topicInfos[AgoraRtmTopicInfo]Topic information.
timestampUInt64The timestamp when the event occurs.

The AgoraRtmTopicInfo data type contains the following properties:

PropertiesTypeDescription
topicStringTopic name.
publishers[AgoraRtmPublisherInfo]Message publisher array.

The AgoraRtmPublisherInfo data type contains the following properties:

PropertiesTypeDescription
publisherUserIdStringUser ID of the message publisher.
publisherMetaStringMetadata of the message publisher.
AgoraRtmStorageEvent

Storage event.

AgoraRtmStorageEvent contains the following properties:

PropertiesTypeDescription
channelTypeAgoraRtmChannelTypeChannel types. See AgoraRtmChannelType.
storageTypeAgoraRtmStorageTypeStorage type. See AgoraRtmStorageType.
eventTypeAgoraRtmStorageEventTypeStorage event type. See AgoraRtmStorageEventType.
targetStringUser ID or channel name.
dataAgoraRtmMetadataMetadata item. See AgoraRtmMetadata.
timestampUInt64The timestamp when the event occurs.
AgoraRtmLockEvent

Lock event.

AgoraRtmLockEvent contains the following properties:

PropertiesTypeDescription
channelTypeAgoraRtmChannelTypeChannel types. See AgoraRtmChannelType.
eventTypeAgoraRtmLockEventTypeLock event type. See AgoraRtmLockEventType.
channelNameStringChannel name.
lockDetailListArrayList<LockDetail>Details of lock.
timestampUInt64The timestamp when the event occurs.

The AgoraRtmLockDetail data type contains the following properties:

PropertiesTypeDescription
lockNameStringLock name.
ownerStringThe ID of the user who has a lock.
ttlintThe expiration time of the lock. The value is in seconds, ranging from [10 to 300]. When the user who owns the lock goes offline, if the user returns to the channel within the time they can still use the lock; otherwise, the lock is released and the users who listen for the didReceiveLockEvent event receives the lockReleased event.
AgoraRtmLinkStateEvent

SDK link state event.

AgoraRtmLinkStateEvent data type contains the following properties:

ParametersTypeDescription
currentStateAgoraRtmLinkStateThe current link state. See AgoraRtmLinkState.
previousStateAgoraRtmLinkStateThe previous link state. See AgoraRtmLinkState.
serviceTypeAgoraRtmServiceTypeThe network connection type. See AgoraRtmServiceType.
operationAgoraRtmLinkOperationThe operation that triggered the current state transition. See AgoraRtmLinkOperation.
reasonCodeAgoraRtmLinkStateChangeReasonThe reason for this state transition. See AgoraRtmLinkStateChangeReason.
reasonStringThe reason for the current state transition. This parameter will be deprecated in the future, please use the reasonCode parameter instead.
affectedChannels[String]The channels affected by the current state transition.
unrestoredChannels[String]The information about the channels to which subscription or joining has not been restored, including the channel name, channel type, and temporary state data in the channel. Typically, this information is empty.
isResumedBoolWithin 2 minutes of the disconnection, whether the state transitions from AgoraRtmLinkStateDisconnected to AgoraRtmLinkStateConnected. true refers to the state has transitioned.
timestampUInt64The timestamp when the event occurs.

AgoraRtmClientKit

Signaling client instance

login

Description

After creating and initializing the Signaling instance, you need to perform the login operation to log in to the Signaling service. After successful login, the client establishes a long connection with the Signaling server, and then the SDK allows the client to access Signaling resources.

Information

After the user successfully logs in to the Signaling service, the PCU of the application increases, which affects your billing data.

Method

You can log in to the Signaling system as follows:

login(_ token: String?) async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
login(_ token: String?, completion completionBlock: AgoraRtmOperationBlock? = nil)
ParametersTypeRequiredDefaultDescription
tokenStringOptional-The token used for logining to the Signaling system.
- If your project enables token authentication, you can provide either the Signaling temporary token or the Signaling token generated by your token server. See User authentication.
- If your project does not enable token authentication, you can enter an empty string or the App ID of a project that enables Signaling services.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

After calling most of the APIs of the Signaling Objective-C SDK, the SDK executes a completion callback, which includes response and errorInfo parameters. Based on different calling results, the SDK returns different parameter values:

  • Success: Returns the corresponding data in the response parameter, and the nil in the errorInfo parameter.
  • Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The errorInfo of AgoraRtmErrorInfo data type contains the error code, error reason, and API operation name for this call, as follows:

The return value of the type AgoraRtmErrorInfo includes the error code, error reason, and API operation name of the call, as follows:

class AgoraRtmErrorInfo : NSError {
    var errorCode: AgoraRtmErrorCode  // Error code
    var operation: String  // API name
    var reason: String  // Brief description of the error reason
}

You can understand the error reason and find the corresponding solution by looking up the error codes in the error codes table.

Basic usage

// async-await
let (response, error) = await rtmClient.login("your_token_here")
if let error = error {
    print("Login failed with error: \(error.reason)")
} else if let response = response {
    print("Login successful!")
} else {
    print("Unknown error")
}
// callback
rtmClient.login(token) { response, error in
    if let errorInfo = error {
        print("Login failed with error: \(errorInfo.reason)")
    } else let loginResponse = response {
        print("Login successful: \(loginResponse)")
    }
}

logout

Description

When you no longer need to operate, you can log out of the system. This operation affects the PCU item in your billing data.

Method

You can log out as follows:

logout() async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
logout(_ completionBlock: AgoraRtmOperationBlock? = nil)
ParametersTypeRequiredDefaultDescription
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

 // async-await
let (response, error) = await rtmClient.logout()
if let error = error {
  print("Logout failed with error: \(error.reason)")
} else if let response = response {
  print("Logout successful!")
} else {
  print("Unknown error")
}
 // callback
rtmClient.logout { response, error in
  if let errorInfo = error {
    print("Logout failed with error: \(errorInfo.reason)")
  } else let logoutResponse = response {
    print("Logout successful: \(logoutResponse)")
  }
}

releaseLock

Description

When you no longer need the Signaling service, it is best to destroy the AgoraRtmClientKit instance. Doing so protects you from the performance degradation caused by memory leaks, errors, and exceptions.

Method

You can destroy the AgoraRtmClientKit instance as follows:

destroy() -> AgoraRtmErrorCode

Basic usage

let errorCode = rtmClient.destroy()
if errorCode == .ok {
    print("RTM instance destroyed successfully.")
} else {
    print("Failed to destroy RTM instance: \(errorCode)")
}

Return Value

The releaseLock method returns an AgoraRtmErrorCode data structure. See the Error codes table.

User authentication

Authentication is the process of validating the identity of each user before they access a system. Agora uses digital tokens to authenticate users and their privileges.

Signaling provides 3 types of channels: message channels, user channels and stream channels. Different types of channels require different types of tokens:

  • For message channels and user channels: When logging in to the Signaling system using the login method, you only need to pass the token that enables the Signaling service.
  • For stream channels: When joining a stream channel using the joinTopic method, in addition to the Signaling token you also need to pass the token that enables the RTC service (RTC Token).

The token is valid for up to 24 hours. Agora recommends that you update the token before it expires. This article describes how to renew the token.

AgoraRtmClientKit.renewToken

Description

Call the AgoraRtmClientKit.renewToken method to renew the Signaling token.

To ensure timely token updates, Agora recommends listening for the tokenPrivilegeWillExpire callback. See Event listeners for details. Once you successfully add the event listener, when the Signaling token is about to expire within 30 seconds, the SDK triggers the tokenPrivilegeWillExpire callback to notify the user about the impending token expiration.

Upon receiving this callback, retrieve a new Signaling token from the token server and call the renewToken method to provide the SDK with the newly generated Signaling token.

Method

Call the AgoraRtmClientKit.renewToken method as follows:

renewToken(_ token: String, completion completionBlock: AgoraRtmOperationBlock? = nil)
renewToken(_ token: String) async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
ParametersTypeRequiredDefaultDescription
tokenStringRequired-The newly generated Signaling token.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let (response, error) = await rtmClient.renewToken(token: "your_token_here")
if let error = error {
    print("Login failed with error: \(error.reason)")
} else if let response = response {
    print("renew token successful!")
} else {
    print("Unknown error")
}
// callback
rtmClient.renewToken(token: "your_token_here") { response, error in
    if let errorInfo = error {
        print("renew token failed with error: \(errorInfo.reason)")
    } else let loginResponse = response {
        print("renew token successful: \(loginResponse)")
    }
}

AgoraRtmStreamChannel.renewToken

Description

Call the AgoraRtmStreamChannel.renewToken method to renew the RTC token.

To ensure timely token updates, Agora recommends listening for the tokenPrivilegeWillExpire callback. See Event listeners for details. Once you successfully add the event listener, when the RTC token is about to expire within 30 seconds, the SDK triggers the tokenPrivilegeWillExpire callback to notify the user about the impending token expiration.

Upon receiving this callback, retrieve a new RTC token from the token server and call the renewToken method to provide the SDK with the newly generated RTC token.

Method

Call the AgoraRtmStreamChannel.renewToken method as follows:

renewToken(_ token: String, completion completionBlock: AgoraRtmOperationBlock? = nil)
renewToken(_ token: String) async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
ParametersTypeRequiredDefaultDescription
tokenStringRequired-The newly generated RTC token.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let (response, error) = await streamChannel.renewToken(token: "your_token_here")
if let error = error {
    print("Login failed with error: \(error.reason)")
} else if let response = response {
    print("renew token successful!")
} else {
    print("Unknown error")
}
// callback
streamChannel.renewToken(token: "your_token_here") { response, error in
    if let errorInfo = error {
        print("renew token failed with error: \(errorInfo.reason)")
    } else let loginResponse = response {
        print("renew token successful: \(loginResponse)")
    }
}

Channels

Signaling provides a highly efficient channel management mechanism for data transmission. Any user who subscribes or joins a channel can receive messages and events transmitted within 100 milliseconds. Signaling allows clients to subscribe to hundreds or even thousands of channels. Most Signaling APIs perform actions such as sending, receiving, and encrypting based on channels.

Signaling provides the following channel types for different application use-cases:

  • Message Channel: Follows the industry-standard Pub/Sub (publish/subscribe) mode. You can send and receive messages within the channel by subscribing to a channel, and do not need to create the channel in advance. There is no limit to the number of publishers and subscribers in a channel.

  • User Channel: Point-to-point message sending and receiving based on the Pub/Sub (publish/subscribe) model. Users do not need to subscribe to the channel, they can directly specify the user ID to send messages. To receive messages, they only need to listen to didReceiveMessageEvent events.

  • Stream Channel: Follows a concept similar to the observer pattern in the industry, where users need to create and join a channel before sending and receiving messages. You can create different topics in the channel, and messages are organized and managed through topics.

AgoraRtmClientKit

subscribeTopic

Description

Signaling provides event notification capabilities for messages and states. By listening for callbacks, you can receive messages and events within subscribed channels. For information on how to add and set event listeners, see Event Listeners.

By calling the subscribeTopic method, the client can subscribe to a message channel and start receiving messages and event notifications within the channel. After successfully calling this method, users who subscribe to the channel and enable the presence event listener can receive a remoteJoinChannel type of didReceivePresenceEvent event. See Event Listeners.

Information

This method only applies to the message channel.

Method

You can call the subscribeTopic method as follows:

subscribe(
    channelName: String,
    option subscribeOption: AgoraRtmSubscribeOptions?
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
subscribe(
    channelName: String,
    option subscribeOption: AgoraRtmSubscribeOptions?,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
subscribeOptionAgoraRtmSubscribeOptionsOptional-Options for subscribing to a channel.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

AgoraRtmSubscribeOptions contains the following properties:

PropertiesTypeRequiredDefaultDescription
featuresAgoraRtmSubscribeChannelFeatureOptional[.message, .presence]Sets event notification types when subscribing to a channel. You can use bitwise operations to set multiple event notifications simultaneously. The SDK enables the message and presence event notifications by default.

Basic usage

// async-await
let subscribeOptions = AgoraRtmSubscribeOptions()
subscribeOptions.features = [.presence, .message, .metadata]

let (response, error) = await rtmClient.subscribe(channelName: "exampleChannel", option: subscribeOptions)
if let error = error {
    print("Failed to subscribe with error: \(error.reason)")
} else {
    print("Successfully subscribed to the channel.")
}
// callback
let subscribeOptions = AgoraRtmSubscribeOptions()
subscribeOptions.features = [.presence, .message, .metadata]

rtmClient.subscribe(channelName: "exampleChannel", option: subscribeOptions) { response, error in
    if let errorInfo = error {
        print("Subscription failed with error: \(errorInfo.reason)")
    } else let SubscribeResponse = response {
        print("Subscription to \(SubscribeResponse.channelName) successful!")
    }
}

unsubscribeTopic

Description

When you no longer need to subscribe to a channel, call unsubscribeTopic to unsubscribe from the channel. After successfully calling this method, users who subscribe to the channel and enable event listeners receive the remoteLeaveChannel type of didReceivePresenceEvent event notification. See Event Listeners.

Information

This method only applies to the message channel.

Method

Call the unsubscribeTopic method as follows:

unsubscribe(_ channelName: String) async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
unsubscribe(_ channelName: String, completion completionBlock: AgoraRtmOperationBlock? = nil)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let (response, error) = await rtmClient.unsubscribe("exampleChannel")
if let error = error {
    // Handle error
    print("Failed to unsubscribe with error: \(error.reason)")
} else if let response = response {
    // Handle success
    print("Unsubscribed from channel successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while unsubscribing.")
}

// callback
rtmClient.unsubscribe("exampleChannel") { response, error in
    if let errorInfo = error {
        print("Unsubscribe failed with error: \(errorInfo.localizedDescription)")
    } else let unsubscribeResponse = response {
        print("Successfully unsubscribed from \(unsubscribeResponse.channelName).")
    }
}

Description

Before using a stream channel, call the `` method to create the AgoraRtmStreamChannel instance. After successfully creating the instance, you can call its relevant methods to implement functions, such as joining the channel, leaving the channel, sending messages in a topic, and subscribing to messages in a topic.

Information

This method only applies to the stream channel.

Method

Call the `` method as follows:

createStreamChannel(_ channelName: String) throws -> AgoraRtmStreamChannel
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
errorNSErrorRequired-(Output) Error description.

Basic usage

do {
    let streamChannel = try rtmClient.createStreamChannel("exampleChannel")
    print("Stream channel created successfully: \(streamChannel)")
    // You can now use the streamChannel instance
} catch {
    print("Failed to create stream channel: \(error)")
}

Return Value

  • Success: Returns an instance of AgoraRtmStreamChannel you can use for subsequent calls to other stream channel APIs.
  • Failure: nil.

joinTopic

Description

After successfully creating a stream channel, you can call the joinTopic method to join the stream channel. Once you join the channel, you can implement channel-related functions. At this point, users who subscribe to the channel and have added event listeners receive the following event notifications:

  • For the local user:
    • The snapshot type of the didReceivePresenceEvent event.
    • The snapshot type of the didReceiveTopicEvent event.
  • For remote users:
    • The remoteJoinChannel type of the didReceivePresenceEvent event.

Information

This method only applies to the stream channel.

Method

Call the joinTopic method as follows:

join(_ option: AgoraRtmJoinChannelOption) async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
join(_ option: AgoraRtmJoinChannelOption, completion completionBlock: AgoraRtmOperationBlock? = nil)
ParametersTypeRequiredDefaultDescription
optionAgoraRtmJoinChannelOptionRequired-Options for joining a channel.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmJoinChannelOption data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
tokenStringOptional-The token used for joining a stream channel.
- If your project enables token authentication, you can provide either the RTC temporary token or obtain an RTC token generated by your token server.
- If your project does not enable token authentication, you can enter an empty string or the App ID of a project that enables RTC and Signaling services.
featuresAgoraRtmJoinChannelFeatureOptional[.message, .presence]Sets event notification types when joining a channel. You can use bitwise operations to set multiple event notifications simultaneously. The SDK enables the presence event notification by default.

Basic usage

// async-await
let joinOptions = AgoraRtmJoinChannelOption()
joinOptions.token = 'your_token'
joinOptions.features = [.message, .presence]

let (response, error) = await streamChannel.join(joinOptions)
if let errorInfo = error {
    print("Failed to join the channel with error: \(errorInfo.reason)")
} else if let joinResponse = response {
    print("Successfully joined the channel. Response: \(joinResponse)")
} else {
    print("Join operation completed with no response or error.")
}
// callback
let joinOptions = AgoraRtmJoinChannelOption()
joinOptions.token = "your_token"
joinOptions.features = [.presence, .presence]

streamChannel.join(joinOptions, completion: { (res, error) in
    if error != nil {
        print("\(error?.operation ?? "Operation") failed! Error reason is \(error?.reason ?? "Unknown reason")")
    } else {
        print("Success")
    }
})

leaveTopic

Description

When you no longer need to stay in a channel, call the leaveTopic method to leave the channel. After leaving the channel, you no longer receive any messages, states, or event notifications from this channel. Also, you are no longer the publisher or subscriber of all topics. To restore your previous publisher role and subscriber relationship, call joinTopic, joinTopic and subscribeTopic methods in the specified order.

After successfully leaving the channel, remote users in the channel receive the remoteLeaveChannel type of didReceivePresenceEvent event notification. For details, see Event Listeners.

Information

This method only applies to the stream channel.

Method

Call the leaveTopic method as follows:

leave() async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
leave(_ completionBlock: AgoraRtmOperationBlock? = nil)
ParametersTypeRequiredDefaultDescription
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let (response, error) = await streamChannel.leave()
if let errorInfo = error {
    print("Failed to leave the channel with error: \(errorInfo.localizedDescription)")
} else if let commonResponse = response {
    print("Successfully left the channel. Response: \(commonResponse)")
} else {
    print("Leave operation completed with no response or error.")
}
// callback
streamChannel.leave({ res, error in
    if error != nil {
        print("\(error?.operation) failed! error reason is \(error?.reason)")
    } else {
        print("success")
    }
})

releaseLock

Description

When you no longer need a channel, call the releaseLock method to destroy the corresponding stream channel instance and release resources. Calling the releaseLock method does not destroy the stream channel. It can be re-joined later by calling `` and joinTopic again.

Caution

This method only applies to the stream channel. If you don't call leaveTopic to leave the channel before directly calling releaseLock to destroy the stream channel instance, the SDK automatically calls the leaveTopic and triggers the corresponding event.

Method

Call the releaseLock method as follows:

destroy() -> AgoraRtmErrorCode

Basic usage

streamChannel.destroy()

Return Value

The releaseLock method returns an AgoraRtmErrorCode data structure. See Error code.

Topics

Topic is a data stream management mechanism in stream channels. Users use topics to subscribe to and distribute data streams, as well as notify events in data streams in stream channels.

Information

Topics exist in stream channels only. Before using the relevant features, create an AgoraRtmStreamChannel instance.

joinTopic

Description

The purpose of joining a topic is to register as a message publisher for the topic, so that the user can send messages in the topic. This operation does not affect whether or not the user becomes a subscriber to the topic.

Information

  • Currently, Signaling supports a single client joining up to 8 topics in the same stream channel at a time.
  • Before joining a topic, create an AgoraRtmStreamChannel instance and call the joinTopic method to join the channel. See Channels.

After successfully joining a topic, users who subscribe to that topic and add event listeners receive the remoteJoinTopic type of didReceiveTopicEvent event notification. For details, see Event Listeners.

Method

Call the joinTopic method as follows:

joinTopic(
    _ topic: String,
    option: AgoraRtmJoinTopicOption?
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
joinTopic(
    _ topic: String,
    option: AgoraRtmJoinTopicOption?,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
topicStringRequired-Topic name.
optionAgoraRtmJoinTopicOptionOptional-Options for joining a topic.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmJoinTopicOption data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
qosAgoraRtmMessageQosOptionalunorderedWhether the data transmitted in the topic is ordered. See AgoraRtmMessageQos.
priorityAgoraRtmMessagePriorityOptionalnormalThe priority of data transmission in the topic compared to other topics in the same channel. See AgoraRtmMessagePriority.
metaStringOptional-Adds additional metadata when joining the topic.
syncWithMediaBoolOptionalfalseWhether the data sent in this topic is synchronized (timestamp-aligned) with the RTC audio and video data stream of the common channel.

Basic usage

// async-await
let topic = "exampleTopic"
let joinOption = AgoraRtmJoinTopicOption()
joinOption.priority = .high
joinOption.syncWithMedia = false

let (response, error) = await stChannel.joinTopic(topic, option: joinOption)
if let errorInfo = error {
    // Handle error
    print("Failed to join topic with error: \(errorInfo.reason)")
} else if let joinTopicResponse = response {
    // Handle success
    print("Successfully joined topic.")
} else {
    // Handle unknown error
    print("Unknown error occurred while joining the topic.")
}
// callack
let topic = "exampleTopic"
let joinOption = AgoraRtmJoinTopicOption()
joinOption.priority = .high
joinOption.syncWithMedia = false

stChannel.joinTopic(topic, option: joinOption) { (response, error) in
    if let errorInfo = error {
 // Handle error
        print("Failed to join topic with error: \(errorInfo.reason)")
    } else if let joinTopicResponse = response {
 // Handle success
        print("Successfully joined topic.")
    } else {
 // Handle unknown error
        print("Unknown error occurred while joining the topic.")
    }
}

publishTopicMessage

Description

Use the publishTopicMessage method to send messages to a topic. Users who have subscribed to the topic and the message publisher in the channel receive the message within 100 milliseconds. Before calling the publishTopicMessage method, users MUST join the stream channel, and then register as a message publisher for that topic by calling the joinTopic method.

The messages sent by users are encrypted with TLS during transmission, and data link encryption is enabled by default and cannot be disabled. To achieve a higher level of data security, users can also enable client encryption during initialization. For details, see Setup.

Method

Call the publishTopicMessage[1/2] and publishTopicMessage[2/2] methods as follows:

  • Send a string message
  publishTopicMessage(
      topic: String,
      message: String,
      option options: AgoraRtmTopicMessageOptions?
  )   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
  publishTopicMessage(
      topic: String,
      message: String,
      option options: AgoraRtmTopicMessageOptions?,
      completion completionBlock: AgoraRtmOperationBlock? = nil
  )
  • Send a binary message
  publishTopicMessage(
      topic: String,
      ata: Data,
      option options: AgoraRtmTopicMessageOptions?
  )   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
  publishTopicMessage(
      topic: String,
      data: Data,
      option options: AgoraRtmTopicMessageOptions?,
      completion completionBlock: AgoraRtmOperationBlock? = nil
  )
ParametersTypeRequiredDefaultDescription
topicStringRequired-Topic name.
messageStringRequired-Sends string messages in the publishTopicMessage[1/2] method.
dataDataRequired-Sends binary messages in the publishTopicMessage[2/2] method.
optionsAgoraRtmTopicMessageOptionsOptional-Message options.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmTopicMessageOptions data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
sendTsUInt64Optional0The timestamp when the SDK sends a message. This parameter is only valid when you set syncWithMedia = true in the joinTopic method. The SDK synchronizes data with RTC audio and video streams based on this timestamp.
customTypeStringRequired-A user-defined field. Only supports String type.

Basic usage

Example 1: Send string messages to a specified channel.

// async-await
let topic = "exampleTopic"
let message = "Hello, Agora!"
let messageOptions = AgoraRtmTopicMessageOptions()
messageOption.customType = "PlainText"

let (response, error) = await stChannel.publishTopicMessage(
    topic: topic,
    message: message,
    option: messageOptions
)
if let errorInfo = error {
    // Handle error
    print("Failed to publish message with error: \(errorInfo.reason)")
} else if let publishResponse = response {
    // Handle success
    print("Message published successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while publishing the message.")
}
// callback
let topic = "exampleTopic"
let message = "Hello, Agora!"
let messageOptions = AgoraRtmTopicMessageOptions()
messageOption.customType = "PlainText"

stChannel.publishTopicMessage(
    topic: topic,
    message: message,
    option: messageOptions
) { (response, error) in
    if let errorInfo = error {
 // Handle error
        print("Failed to publish message with error: \(errorInfo.reason)")
    } else if let publishResponse = response {
 // Handle success
        print("Message published successfully.")
    } else {
 // Handle unknown error
        print("Unknown error occurred while publishing the message.")
    }
}

Example 2: Send byte messages to a specified channel.

// async-await
let topic = "exampleTopic"
let messageOptions = AgoraRtmTopicMessageOptions()
messageOption.customType = "BinaryData"
let byteArray: [UInt8] = [0x00, 0x01, 0x02, 0x03]
let binaryData = Data(byteArray)

let (response, error) = await stChannel.publishTopicMessage(
    topic: topic,
    ata: binaryData,
    option: messageOptions
)
if let errorInfo = error {
    // Handle error
    print("Failed to publish binary data with error: \(errorInfo.reason)")
} else if let publishResponse = response {
    // Handle success
    print("Binary data published successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while publishing the binary data.")
}
// callback
let topic = "exampleTopic"
let messageOptions = AgoraRtmTopicMessageOptions()
messageOption.customType = "BinaryData"
let byteArray: [UInt8] = [0x00, 0x01, 0x02, 0x03]
let binaryData = Data(byteArray)

stChannel.publishTopicMessage(
    topic: topic,
    ata: binaryData,
    option: messageOptions
) { (response, error) in
    if let errorInfo = error {
    // Handle error
        print("Failed to publish binary data with error: \(errorInfo.reason)")
    } else if let publishResponse = response {
    // Handle success
        print("Binary data published successfully.")
    } else {
    // Handle unknown error
        print("Unknown error occurred while publishing the binary data.")
    }
}

leaveTopic

Description

When you no longer need to publish messages to a topic, can call the leaveTopic method to unregister as a message publisher for that topic and release resources. This method does not affect whether or not you subscribe to that topic or any other operations performed by other users on that topic.

After successfully calling this method, users who subscribe to the channel and enable event listeners receive the remoteLeaveTopic type of didReceiveTopicEvent event notification. See Event Listeners.

Method

Call the leaveTopic method as follows:

leaveTopic(
    _ topic: String
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
leaveTopic(
    _ topic: String,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
topicStringRequired-Topic name.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let topic = "exampleTopic"

let (response, error) = await stChannel.leaveTopic(topic)
if let errorInfo = error {
 // Handle error
    print("Failed to leave topic with error: \(errorInfo.reason)")
} else if let leaveTopicResponse = response {
 // Handle success
    print("Successfully left topic.")
} else {
 // Handle unknown error
    print("Unknown error occurred while leaving the topic.")
}
// callback
let topic = "exampleTopic"

stChannel.leaveTopic(topic) { (response, error) in
    if let errorInfo = error {
    // Handle error
        print("Failed to leave topic with error: \(errorInfo.reason)")
    } else if let leaveTopicResponse = response {
    // Handle success
        print("Successfully left topic.")
    } else {
    // Handle unknown error
        print("Unknown error occurred while leaving the topic.")
    }
}

subscribeTopic

Description

After joining a channel, you can call the subscribeTopic method to subscribe to message publishers of the topic in the channel.

subscribeTopic is an incremental method. For example, if you call this method for the first time with a subscribing list of [UserA, UserB], and then call it again with a subscribing list of [UserB, UserC], the final successful subscribing result is [UserA, UserB, UserC].

A user can subscribe to a maximum of 50 topics in each channel, and a maximum of 64 message publishers in each topic. See API usage restrictions.

Method

You can call the subscribeTopic method as follows:

subscribeTopic(
    _ topic: String,
    option: AgoraRtmTopicOption?
)   async -> (AgoraRtmTopicSubscriptionResponse?, AgoraRtmErrorInfo?)
subscribeTopic(
    _ topic: String,
    option: AgoraRtmTopicOption?,
    completion completionBlock: AgoraRtmTopicSubscriptionBlock? = nil
)
ParametersTypeRequiredDefaultDescription
topicStringRequired-Topic name.
optionAgoraRtmTopicOptionOptional-Options for subscribing to a topic.
completionAgoraRtmTopicSubscriptionBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmTopicSubscriptionResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmTopicOption data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
users[String]Optional-A list of UserId of message publishers that you want to subscribe to. If you do not set this property, you can randomly subscribe to up to 64 users by default.

The AgoraRtmTopicSubscriptionResponse data type contains the following properties:

PropertiesTypeDescription
succeedUsers[String]A list of successfully subscribed users.
failedUsers[String]A list of users that the SDK fails to subscribe to.

Basic usage

Example 1: Subscribe to the specified message publisher in the topic.

// async-await
let topic = "exampleTopic"
let topicOption = AgoraRtmTopicOption() // Optional, configure if needed
topicOption.users = ["user1", "user2"] // Specify users to subscribe to

let (response, error) = await stChannel.subscribeTopic(topic, option: topicOption)
if let errorInfo = error {
    // Handle error
    print("Failed to subscribe to topic with error: \(errorInfo.reason)")
} else if let subscriptionResponse = response {
    // Handle success
    print("Successfully subscribed to topic.")
} else {
    // Handle unknown error
    print("Unknown error occurred while subscribing to the topic.")
}
// callback
let topic = "exampleTopic"
let topicOption = AgoraRtmTopicOption() // Optional
topicOption.users = ["user1", "user2"] // Specify users to subscribe to

stChannel.subscribeTopic(topic, option: topicOption) { (response, error) in
    if let errorInfo = error {
    // Handle error
        print("Failed to subscribe to topic with error: \(errorInfo.reason)")
    } else if let subscriptionResponse = response {
    // Handle success
        print("Successfully subscribed to topic.")
    } else {
    // Handle unknown error
        print("Unknown error occurred while subscribing to the topic.")
    }
}

Example 2: Randomly subscribe to 64 message publishers in the topic.

// async-await
let topic = "exampleTopic"

let (response, error) = await stChannel.subscribeTopic(topic, option: nil)
if let errorInfo = error {
    // Handle error
    print("Failed to subscribe to topic with error: \(errorInfo.reason)")
} else if let subscriptionResponse = response {
    // Handle success
    print("Successfully subscribed to topic.")
} else {
    // Handle unknown error
    print("Unknown error occurred while subscribing to the topic.")
}
// callback
let topic = "exampleTopic"

stChannel.subscribeTopic(topic, option: nil) { (response, error) in
    if let errorInfo = error {
    // Handle error
        print("Failed to subscribe to topic with error: \(errorInfo.reason)")
    } else if let subscriptionResponse = response {
    // Handle success
        print("Successfully subscribed to topic.")
    } else {
    // Handle unknown error
        print("Unknown error occurred while subscribing to the topic.")
    }
}

unsubscribeTopic

Description

When you are no longer interested in a specified topic, or no longer need to subscribe to one or more message publishers in the topic, call the unsubscribeTopic method to unsubscribe from the topic or the specified message publishers in the topic.

Method

Call the unsubscribeTopic method as follows:

unsubscribeTopic(
    _ topic: String,
    option: AgoraRtmTopicOption?
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
unsubscribeTopic(
    _ topic: String,
    option: AgoraRtmTopicOption?,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
topicStringRequired-Topic name.
optionsAgoraRtmTopicOptionOptional-Options for unsubscribe from a topic.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmTopicOption data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
users[String]Optional-A list of UserId of message publishers that you want to unsubscribe from. If you do not set this property, you can randomly unsubscribe from up to 64 users by default.

Basic usage

Example 1: Unsubscribe the specified message publisher in the topic

// async-await
let topic = "exampleTopic"
let topicOption = AgoraRtmTopicOption()
// Set specific users to unsubscribe from if needed
topicOption.users = ["user1", "user2"]

let (response, error) = await stChannel.unsubscribeTopic(topic, option: topicOption)
if let errorInfo = error {
    // Handle error
    print("Failed to unsubscribe from topic with error: \(errorInfo.reason)")
} else if let unsubscribeResponse = response {
     // Handle success
    print("Successfully unsubscribed from topic with specified options.")
} else {
    // Handle unknown error
    print("Unknown error occurred while unsubscribing from the topic.")
}
// callback
let topic = "exampleTopic"
let topicOption = AgoraRtmTopicOption()
 // Set specific users to unsubscribe from if needed
topicOption.users = ["user1", "user2"]

stChannel.unsubscribeTopic(topic, option: topicOption) { (response, error) in
    if let errorInfo = error {
    // Handle error
        print("Failed to unsubscribe from topic with error: \(errorInfo.reason)")
    } else if let unsubscribeResponse = response {
    // Handle success
        print("Successfully unsubscribed from topic with specified options.")
    } else {
    // Handle unknown error
        print("Unknown error occurred while unsubscribing from the topic.")
    }
}

Example 2: Randomly unsubscribe from up to 64 message publishers in the topic.

// async-await
let topic = "exampleTopic"

 // Unsubscribing without options
let (response, error) = await stChannel.unsubscribeTopic(topic, option: nil)
if let errorInfo = error {
    // Handle error
    print("Failed to unsubscribe from topic with error: \(errorInfo.reason)")
} else if let unsubscribeResponse = response {
    // Handle success
    print("Successfully unsubscribed from topic.")
} else {
    // Handle unknown error
    print("Unknown error occurred while unsubscribing from the topic.")
}
// callback
let topic = "exampleTopic"

// Unsubscribing without options
stChannel.unsubscribeTopic(topic, option: nil) { (response, error) in
    if let errorInfo = error {
    // Handle error
        print("Failed to unsubscribe from topic with error: \(errorInfo.reason)")
    } else if let unsubscribeResponse = response {
    // Handle success
        print("Successfully unsubscribed from topic.")
    } else {
    // Handle unknown error
        print("Unknown error occurred while unsubscribing from the topic.")
    }
}

Messages

Sending and receiving messages is the most basic function of the Signaling service. A message sent by the Signaling server is delivered to online subscribing users within 100 ms. Depending on your business requirements, you can send messages to a single user or broadcast messages to multiple users.

Signaling offers three types of channels. These channel types have the following differences in how messages are transmitted and methods are called:

  • Message Channel: The real-time channel. Messages are transmitted through the channel, and the channel is highly scalable. Local users can call the publishTopicMessage method, set the channelType parameter to message, and set the channelName parameter to the channel name to send messages in the channel. The remote users can call the subscribeTopic method to subscribe to the channel and receive messages.

  • User Channel: The real-time channel. Messages are transmitted to the specified user. Local users can call the publishTopicMessage method, set the channelType parameter to user, and set the channelName parameter to the user ID to send messages to the specified user. The specified remote users receive messages through the didReceiveMessageEvent event notifications.

  • Stream Channel: The streaming transmission channel. Messages are transmitted through topics. Users need to join a channel first, and then join a topic. Local users can call the publishTopicMessage method to send messages in the topic, and remote users can call the subscribeTopic method to subscribe to the topic and receive messages.

This section introduces APIs to send and receive messages in a message channel or a user channel.

publishTopicMessage

Description

Directly call the publishTopicMessage method to send messages to all online users subscribed to a channel. Even a user does not subscribe to the channel, they can still send messages in the channel.

Information

The following practices can effectively improve the reliability of message transmission:
- The message payload should be within 32 KB; otherwise, sending will fail.
- The upper limit of the rate at which messages are sent to a single channel is 60 QPS. If the sending rate exceeds the limit, some messages are discarded. A lower rate is better, as long as the requirements are met.

After successfully calling this method, the SDK triggers a didReceiveMessageEvent event notification. Users who subscribe to the channel and have enabled the event listener receive this event notification. For details, see Event Listeners.

Method

Call the publishTopicMessage[1/2] or publishTopicMessage[2/2] methods as follows:

  • Send a string message
  publish(
      channelName: String,
      message: String,
      option publishOption: AgoraRtmPublishOptions?
  )   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
  publish(
      channelName: String,
      message: String,
      option publishOption: AgoraRtmPublishOptions?,
      completion completionBlock: AgoraRtmOperationBlock? = nil
  )
  • Send a binary message
  publish(
      channelName: String,
      data: Data,
      option publishOption: AgoraRtmPublishOptions?
  )   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
  publish(
      channelName: String,
      data: Data,
      option publishOption: AgoraRtmPublishOptions?,
      completion completionBlock: AgoraRtmOperationBlock? = nil
  )
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-
- Fill in a channel name to send messages in a specified channel.
- Fill in a user ID to send messages to a specified user.
messageStringRequired-Sends string messages in the publishTopicMessage[1/2] method.
dataDataRequired-Sends binary messages in the publishTopicMessage[2/2] method.
publishOptionAgoraRtmTopicMessageOptionsOptional-Message options.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmTopicMessageOptions data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
channelTypeAgoraRtmChannelTypeRequired-Channel types. See AgoraRtmChannelType.
customTypeStringRequired-A user-defined field. Only supports string type.

Basic usage

Example 1: Send string messages to a specified channel.

// async-await
let publishOptions = AgoraRtmPublishOptions()
publishOptions.channelType = .message // Which channel type, .user or .message
publishOptions.customType = "PlaintText" // Custom type "PlaintText"
let (response, error) = await rtmClient.publish(
    channelName: "exampleChannel",
    message: "Hello, Agora!",
    option: publishOptions)
if let errorInfo = error {
    // handle error
    print("Failed to publish message with error: \(errorInfo.reason)")
} else if let publishResponse = response {
    // handle success
    print("Message published successfully.")
} else {
    // handle unknown error
    print("Unknown error occurred while publishing the message.")
}
// callback
let publishOptions = AgoraRtmPublishOptions()
publishOptions.channelType = .message // Set the channel type to .message
publishOptions.customType = "PlainText" // Set a custom type to "PlainText"

rtmClient.publish(
    channelName: "exampleChannel",
    message: "Hello, Agora!",
    option: publishOptions
) { (response, error) in
    if let errorInfo = error {
    // Handle error
        print("Failed to publish message with error: \(errorInfo.reason)")
    } else if let publishResponse = response {
    // Handle success
        print("Message published successfully.")
    } else {
    // Handle unknown error
        print("Unknown error occurred while publishing the message.")
    }
}

Example 2: Send byte messages to a specified channel.

// async-await
let publishOptions = AgoraRtmPublishOptions()
publishOptions.channelType = .message // Set channel type, .user or .message
publishOptions.customType = "BinaryData" // Set custom message type "BinaryData"
let byteArray: [UInt8] = [0x00, 0x01, 0x02, 0x03]
let binaryData = Data(byteArray)

let (response, error) = await rtmClient.publish(
    channelName: "exampleChannel",
    data: binaryData,
    option: publishOptions)
if let errorInfo = error {
    // handle error
    print("Failed to publish message with error: \(errorInfo.reason)")
} else if let publishResponse = response {
    // handle success
    print("Message published successfully.")
} else {
    // handle unknown error
    print("Unknown error occurred while publishing the message.")
}
// callback
let publishOptions = AgoraRtmPublishOptions()
publishOptions.channelType = .message // Set the channel type to .message
publishOptions.customType = "BinaryData" // Set a custom type to "BinaryData"
let byteArray: [UInt8] = [0x00, 0x01, 0x02, 0x03]
let binaryData = Data(byteArray)

rtmClient.publish(
    channelName: "exampleChannel",
    data: binaryData,
    option: publishOptions
) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to publish message with error: \(errorInfo.reason)")
    } else if let publishResponse = response {
        // Handle success
        print("Message published successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while publishing the message.")
    }
}

Example 3: Send messages to a specified user channel.

let publishOptions = AgoraRtmPublishOptions()
publishOptions.channelType = .user
publishOptions.customType = "PlaintText"

let (response, error) = await rtmClient.publish(
    channelName: "exampleChannel",
    message: "Hello, Agora!",
    option: publishOptions)
if let errorInfo = error {
    // handle error
    print("Failed to publish message with error: \(errorInfo.reason)")
} else if let publishResponse = response {
    // handle success
    print("Message published successfully.")
} else {
    // handle unknown error
    print("Unknown error occurred while publishing the message.")
}
// callback
let publishOptions = AgoraRtmPublishOptions()
publishOptions.channelType = .user
publishOptions.customType = "PlainText"

rtmClient.publish(
    channelName: "exampleChannel",
    message: "Hello, Agora!",
    option: publishOptions
) { (response, error) in
    if let errorInfo = error {
        // handle error
        print("Failed to publish message with error: \(errorInfo.reason)")
    } else if let publishResponse = response {
        // handle success
        print("Message published successfully.")
    } else {
        // handle unknown error
        print("Unknown error occurred while publishing the message.")
    }
}

Information

After successfully calling this method, the SDK triggers a didReceiveMessageEvent event notification. Users who subscribe to the channel and have enabled the event listener receive this event notification. For details, see Event Listeners.

Receive

Signaling provides event notifications for messages, states, and event changes. By listening for callbacks, you can receive messages and events within subscribed channels. The following code snippet shows how to receive messages from a user channel.

// message event handler
class MessageHandler: NSObject, AgoraRtmClientDelegate {
    func rtmKit(_ rtmKit: AgoraRtmClientKit, didReceiveMessageEvent event: AgoraRtmMessageEvent) {
        print("Message received from \(event.channelName): \(event.message)")
    }
}
let messageDelegate = MessageHandler()
rtmClient.addDelegate(messageDelegate)

For information on how to add and set event listeners, see Event Listeners.

Presence

The presence feature provides the ability to monitor user online, offline, and user historical state change. With the Presence feature, you can get real-time access to the following information:

  • Real-time event notification when a user joins or leaves a specified channel.
  • Real-time event notification when the custom temporary user state changes.
  • Query which channels a specified user has joined or subscribed to.
  • Query which users have joined a specified channel and their temporary user state data.

Information

Presence applies to both message channels and stream channels.

AgoraRtmPresence

Presence instance

getOnlineUsers

Description

By calling the getOnlineUsers method, you can query real-time information about the number of online users, the list of online users, and the temporary state of online users in a specified channel.

Method

Call the getOnlineUsers method as follows:

getOnlineUsers(
    channelName: String,
    channelType: AgoraRtmChannelType,
    options: AgoraRtmGetOnlineUsersOptions?
)   async -> (AgoraRtmGetOnlineUsersResponse?, AgoraRtmErrorInfo?)
getOnlineUsers(
    channelName: String,
    channelType: AgoraRtmChannelType,
    options: AgoraRtmGetOnlineUsersOptions?,
    completion completionBlock: AgoraRtmGetOnlineUsersBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
optionsAgoraRtmGetOnlineUsersOptionsOptional-Query options.
completionAgoraRtmGetOnlineUsersBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmGetOnlineUsersResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmGetOnlineUsersOptions data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
includeUserIdBoolOptionaltrueWhether the returned result includes the user ID of online members.
includeStateBoolOptionalfalseWhether the returned result includes temporary state data of online users.
pageStringRequired-Page number of the returned result. You can check whether there is next page in the returned result.

The AgoraRtmGetOnlineUsersResponse data type contains the following properties:

PropertiesTypeDescription
totalOccupancyintThe list length of UserStateList. When you set both includeUserId and includeState properties in the AgoraRtmGetOnlineUsersOptions data type to false, this value represents the total number of current online users in the channel.
userStateList[AgoraRtmUserState]List of online users and their temporary state information in a specified channel.
nextPageStringPage number of the next page. Confirm whether there is a next page:
- A null value indicates that next page does not exist.
- A non-null value indicates that there is a next page. You can fill this value in the page property of the getOnlineUsers method to query the next page results.

The AgoraRtmUserState data type contains the following properties:

PropertiesTypeDescription
userIdStringUser ID.
states[String : String]List of online users and their temporary state information in a specified channel.

Basic usage

// async-await
let presenceOptions = AgoraRtmGetOnlineUsersOptions()
presenceOptions.includeUserId = true
presenceOptions.includeState = true

let (response, error) = await rtmClient.getPresence()!.getOnlineUsers(
    channelName: "exampleChannel",
    channelType: .message,
    options: presenceOptions)

if let errorInfo = error {
    print("Error querying online users: \(errorInfo.reason)")
} else if let getOnlineUsersResponse = response {
    // Handle the response containing user presence information
    print("Users in channel: \(getOnlineUsersResponse)")
}
// callback
let presenceOptions = AgoraRtmGetOnlineUsersOptions()
presenceOptions.includeUserId = true
presenceOptions.includeState = true

rtmClient.getPresence()!.getOnlineUsers(
    channelName: "exampleChannel",
    channelType: .message,
    options: presenceOptions
) { (response, error) in
    if let errorInfo = error {
        print("Error querying online users: \(errorInfo.reason)")
    } else if let getOnlineUsersResponse = response {
        // Handle the response containing user presence information
        print("Users in channel: \(getOnlineUsersResponse)")
    } else {
        print("Online users query completed with no response or error.")
    }
}

getUserChannels

Description

In use-cases such as statistic analytics and debugging, you may need to know all the channels that a specified user has subscribed to or joined. Call the getUserChannels method to get the list of channels where the specified user is present in real time.

Method

Call the getUserChannels method as follows:

getUserChannels(userId: String) async -> (AgoraRtmGetUserChannelsResponse?, AgoraRtmErrorInfo?)
getUserChannels(userId: String, completion completionBlock: AgoraRtmGetUserChannelsBlock? = nil)
ParametersTypeRequiredDefaultDescription
userIdStringRequired-User ID.
completionAgoraRtmGetUserChannelsBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmGetUserChannelsResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmGetUserChannelsResponse data type contains the following properties:

PropertiesTypeDescription
totalChannelInt32Number of channels the user is in.
channels[AgoraRtmChannelInfo]List of channel information, including channel name and channel type.

The AgoraRtmChannelInfo data type contains the following properties:

PropertiesTypeDescription
channelNameStringChannel name.
channelTypeAgoraRtmChannelTypeChannel types. See AgoraRtmChannelType.

Basic usage

// async-await
let (response, error) = await rtmClient.getPresence()!.getUserChannels(userId:"your_userID")
if let errorInfo = error {
    print("Error querying user channels: \(errorInfo.reason)")
} else if let getUserChannelsResponse = response {
 // Handle the response containing user presence information
    print("channels: \(getUserChannelsResponse)")
}
// callback
rtmClient.getPresence()!.getUserChannels(userId: "your_userID") { (response, error) in
    if let errorInfo = error {
        print("Error querying user channels: \(errorInfo.reason)")
    } else if let getUserChannelsResponse = response {
        // Process the list of channels the user is currently in
        print("User is present in channels: \(getUserChannelsResponse.channels)")
    } else {
        print("No response or error.")
    }
}

setState

Description

To meet requirements in different business use-cases for setting user states, Signaling provides the setState method to customize the temporary user state. Users can add custom states such as scores, game state, location, mood, and hosting state for themselves.

After successful setup, as long as the user keeps subscribing to the channel and stays online, the custom states persists in the channel. setState method sets the temporary user state, and the state disappears when the user leaves the channel or disconnects from Signaling. If you need to restore user states when rejoining a channel or reconnecting, you need to cache the data locally in real time. If you want to permanently save user states, Agora recommends using the setUserMetadata method of the storage functionality instead.

If a user modifies the temporary user state, Signaling triggers the remoteStateChanged type of didReceivePresenceEvent event in real time. You can receive the event by subscribing to the channel and configuring the corresponding property.

Method

Call the setState method as follows:

setState(
    channelName: String,
    channelType: AgoraRtmChannelType,
    items: [String : String]
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
setState(
    channelName: String,
    channelType: AgoraRtmChannelType,
    items: [String : String],
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name
channelTypeAgoraRtmChannelTypeRequired-Channel types. See AgoraRtmChannelType.
items[String : String]Required-User state, an array of StateItem.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let stateItems: [String: String] = ["user1": "online","user2": "busy"]
// Call the setState function
let (response, error) = await rtmClient.getPresence()!.setState(
    channelName: "exampleChannel",
    channelType: .message,
    items: stateItems
  )
if let errorInfo = error {
    print("Error setting state: \(errorInfo.reason)")
} else if let setStateResponse = response {
    // Handle the response indicating the result of the state update
    print("State update response: \(setStateResponse)")
}
// callback
let stateItems: [String: String] = ["user1": "online", "user2": "busy"]
 // Call the setState function using a callback
rtmClient.getPresence()?.setState(
    channelName: "exampleChannel",
    channelType: .message,
    items: stateItems
) { (response, error) in
    if let errorInfo = error {
        print("Error setting state: \(errorInfo.reason)")
    } else if let setStateResponse = response {
        // Handle the response indicating the result of the state update
        print("State update response: \(setStateResponse)")
    } else {
        print("No response or error received.")
    }
}

getState

Description

To get the temporary user state of a specified user in a channel, use the getState method.

Method

Call the getState method as follows:

getState(
    channelName: String,
    channelType: AgoraRtmChannelType,
    userId: String
)   async -> (AgoraRtmPresenceGetStateResponse?, AgoraRtmErrorInfo?)
getState(
    channelName: String,
    channelType: AgoraRtmChannelType,
    userId: String,
    completion completionBlock: AgoraRtmPresenceGetStateBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel types. See AgoraRtmChannelType.
userIdStringRequired-User ID.
completionAgoraRtmPresenceGetStateBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmPresenceGetStateResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmPresenceGetStateResponse data type contains the following properties:

PropertiesTypeDescription
stateAgoraRtmUserStateList of online users and their temporary state information in a specified channel.

The AgoraRtmUserState data type contains the following properties:

PropertiesTypeDescription
userIdStringUser ID.
states[String : String]List of online users and their temporary state information in a specified channel.

Basic usage

// async-await
let userId = "user1"
// Call the getState function
let (response, error) = await rtmClient.getPresence()!.getState(
    channelName: "exampleChannel",
    channelType: .message,
    userId: userId
)
if let errorInfo = error {
    print("Error getting state: \(errorInfo.reason)")
} else if let getStateResponse = response {
    // Handle the response containing the user's state
    print("User state response: \(getStateResponse)")
}
// callback
let userId = "user1"
// Call the getState function
rtmClient.getPresence()?.getState(
    channelName: "exampleChannel",
    channelType: .message,
    userId: userId,
    completion: { (response, error) in
        if let errorInfo = error {
            print("Error getting state: \(error.reason)")
        } else if let getStateResponse = response {
            // Handle the response containing the user's state
            print("User state response: \(getStateResponse)")
        }
    }
)

removeState

Description

When a temporary user state is no longer needed, call the removeState method to remove one or more of your temporary states. When the user state is removed, the user who has subscribed to the channel and enabled the presence event listener receives the remoteStateChanged type of didReceivePresenceEvent event notification. See Event Listeners.

Method

Call the removeState method as follows:

removeState(
    channelName: String,
    channelType: AgoraRtmChannelType,
    keys: [String]
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
removeState(
  channelName: String,
  channelType: AgoraRtmChannelType,
  keys: [String],
  completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
keys[String]Required-List of keys to be deleted. If you do not provide this property, the SDK removes all states.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let stateKeys: [String] = ["user1", "user2"]
// Call the removeState function
let (response, error) = await rtmClient.getPresence()!.removeState(
    channelName: "exampleChannel",
    channelType: .message,
    keys: stateKeys
)
if let errorInfo = error {
    print("Error removing state: \(errorInfo.reason)")
} else if let removeStateResponse = response {
    // Handle the response indicating the result of the state removal
    print("State removal response: \(removeStateResponse)")
}
// callback
let stateKeys: [String] = ["user1", "user2"]
rtmClient.getPresence()?.removeState(
    channelName: "exampleChannel",
    channelType: .message,
    keys: stateKeys,
    completion: { (response, error) in
        if let errorInfo = error {
            print("Error removing state: \(errorInfo.reason)")
        } else if let removeStateResponse = response {
            // Handle the response indicating the result of the state removal
            print("State removal response: \(removeStateResponse)")
        }
    }
)

Storage

The storage feature provides a dynamic database mechanism that allows developers to dynamically set, store, update, and delete data such as channel metadata and user metadata.

setChannelMetadata

Description

The setChannelMetadata method sets metadata for a message channel or stream channel. A channel can only have one set of metadata, but each set of metadata can have one or more metadata items. When you call this method multiple times, the SDK retrieves the key of the metadata items in turn and applies settings according to the following rules:

  • If you set metadata with different keys, the SDK adds each set of metadata in sequence according to the order of the method calls.
  • If you set metadata with the same key, the value of the last setting overwrites the previous one.

After successfully setting channel metadata, users who subscribe to the channel and enable event listeners receive the channel type of didReceiveStorageEvent event notification. See Event listeners.

Channel metadata also provides version control logic CAS (Compare And Set). This feature provides two independent version control fields. You can set one or both according to your actual business use-case:

  • Enable version number verification for the entire set of channel metadata by setting the majorRevision property in the AgoraRtmMetadata data type.

  • Enable version number verification for a single metadata item by setting the revision property in the AgoraRtmMetadataItem class.

When setting channel metadata or metadata items, you can control whether to enable version number verification by specifying the revision property:

  • The default value of the revision property is -1, which indicates that this method call does not perform any CAS verification. If the channel metadata or metadata item already exists, the latest value overwrites the previous one. If the channel metadata or metadata item does not exist, the SDK creates it.

  • If the revision property is a positive integer, this method call performs CAS verification. If the channel metadata or metadata item already exists, the SDK updates the corresponding value after the version number verification succeeds. If the channel metadata or metadata item does not exist, the SDK returns the error code.

Method

Call the setChannelMetadata method as follows:

setChannelMetadata(
    channelName: String,
    channelType: AgoraRtmChannelType,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?,
    lock: String?
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
setChannelMetadata(
    channelName: String,
    channelType: AgoraRtmChannelType,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?,
    lock: String?,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
dataAgoraRtmMetadataRequired-Metadata item. See AgoraRtmMetadata.
optionsAgoraRtmMetadataOptionsOptional-Options for setting the channel metadata.
lockStringOptionalnil string ''Lock name. If set, only users who call the acquireLock method to acquire the lock can perform operations.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmMetadataOptions data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
recordTsBoolOptionalfalseWhether to record the timestamp of the edits.
recordUserIdBoolOptionalfalseWhether to record the user ID of the editor.

Basic usage

// async-await
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message

// Create metadata items
let metadataItem1 = AgoraRtmMetadataItem(key: "topic", value: "discussion", revision: 174298200)
let metadataItem2 = AgoraRtmMetadataItem(key: "status", value: "active", revision: 174298100)

// Create metadata
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 174298270
metadata.items = [metadataItem1, metadataItem2]

// Set metadata options
let metadataOptions = AgoraRtmMetadataOptions()
metadataOptions.recordTs = true
metadataOptions.recordUserId = true

// Set channel metadata
let (response, error) = await rtmClient.setChannelMetadata(
    channelName: channelName,
    channelType: channelType,
    data: metadata,
    options: metadataOptions,
    lock: nil // or provide a lock name
)

if let errorInfo = error {
// Handle error
    print("Failed to set channel metadata with error: \(errorInfo.reason)")
} else if let setMetadataResponse = response {
// Handle success
    print("Channel metadata set successfully.")
} else {
// Handle unknown error
    print("Unknown error occurred while setting channel metadata.")
}
// callback
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message

// Create metadata items
let metadataItem1 = AgoraRtmMetadataItem(key: "topic", value: "discussion", revision: 174298200)
let metadataItem2 = AgoraRtmMetadataItem(key: "status", value: "active", revision: 174298100)

// Create metadata
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 174298270
metadata.items = [metadataItem1, metadataItem2]

// Set metadata options
let metadataOptions = AgoraRtmMetadataOptions()
metadataOptions.recordTs = true
metadataOptions.recordUserId = true

// Set channel metadata
rtmClient.setChannelMetadata(
    channelName: channelName,
    channelType: channelType,
    data: metadata,
    options: metadataOptions,
    lock: nil, // or provide a lock name
    completion: { (response, error) in
        if let errorInfo = error {
            // Handle error
            print("Failed to set channel metadata with error: \(errorInfo.reason)")
        } else if let setMetadataResponse = response {
            // Handle success
            print("Channel metadata set successfully.")
        } else {
            // Handle unknown error
            print("Unknown error occurred while setting channel metadata.")
        }
    }
)

getChannelMetadata

Description

The getChannelMetadata method gets the metadata of the specified channel.

Method

Call the getChannelMetadata method as follows:

getChannelMetadata(
    channelName: String,
    channelType: AgoraRtmChannelType
)   async -> (AgoraRtmGetMetadataResponse?, AgoraRtmErrorInfo?)
getChannelMetadata(
    channelName: String,
    channelType: AgoraRtmChannelType,
    completion completionBlock: AgoraRtmGetMetadataBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
completionAgoraRtmGetMetadataBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmGetMetadataResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmGetMetadataResponse data type contains the following properties:

PropertiesTypeDescription
dataAgoraRtmMetadataMetadata item. See AgoraRtmMetadata.

Basic usage

// async-await
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message

// Retrieve channel metadata
let (response, error) = await rtmClient.getChannelMetadata(
    channelName: channelName,
    channelType: channelType
)
if let errorInfo = error {
    // Handle error
    print("Failed to get channel metadata with error: \(errorInfo.reason)")
} else if let getMetadataResponse = response, let metadata = getMetadataResponse.data {
    // Handle success and access the metadata
    print("Retrieved channel metadata: \(metadata)")
    for item in metadata.items ?? [] {
        print("Key: \(item.key), Value: \(item.value), Last updated by: \(item.authorUserId)")
    }
} else {
    // Handle unknown error
    print("Unknown error occurred while getting channel metadata.")
}
// callback
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message

// Retrieve channel metadata
rtmClient.getChannelMetadata(
    channelName: channelName,
    channelType: channelType,
    completion: { (response, error) in
        if let errorInfo = error {
            // Handle error
            print("Failed to get channel metadata with error: \(errorInfo.reason)")
        } else if let getMetadataResponse = response, let metadata = getMetadataResponse.data {
            // Handle success and access the metadata
            print("Retrieved channel metadata: \(metadata)")
            for item in metadata.items ?? [] {
                print("Key: \(item.key), Value: \(item.value), Last updated by: \(item.authorUserId)")
            }
        } else {
            // Handle unknown error
            print("Unknown error occurred while getting channel metadata.")
        }
    }
)

removeChannelMetadata

Description

The removeChannelMetadata method removes channel metadata or metadata items.

When removing channel metadata or metadata items, you can control whether to enable version number verification by specifying the revision property:

  • The default value of the revision property is -1, which indicates that this method call does not perform any CAS verification. If the channel metadata or metadata item already exists, the SDK removes it. If the channel metadata or metadata item does not exist, the SDK returns the error code.

  • If the revision property is a positive integer, this method call performs CAS verification. If the channel metadata or metadata item already exists, the SDK removes the corresponding value after the version number verification succeeds. If the channel metadata or metadata item does not exist, the SDK returns the error code.

After successfully removing channel metadata or metadata items, users who subscribe to the channel and enable event listeners receive the channel type of didReceiveStorageEvent event notification. See Event listeners.

Method

Call the removeChannelMetadata method as follows:

removeChannelMetadata(
    channelName: String,
    channelType: AgoraRtmChannelType,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?,
    lock: String?
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
removeChannelMetadata(
    channelName: String,
    channelType: AgoraRtmChannelType,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?,
    lock: String?,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
dataAgoraRtmMetadataRequired-Metadata item. See AgoraRtmMetadata.
optionsAgoraRtmMetadataOptionsOptional-Options for setting the channel metadata.
lockStringOptionalnil string ''Lock name. If set, only users who call the acquireLock method to acquire the lock can perform operations.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmMetadataOptions data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
recordTsBoolOptionalfalseWhether to record the timestamp of the edits.
recordUserIdBoolOptionalfalseWhether to record the user ID of the editor.

Basic usage

// async-await
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
// Create metadata items to remove
let itemToRemove = AgoraRtmMetadataItem()
itemToRemove.key = "topic"
itemToRemove.revision = 174298200
// Create metadata object
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 174298270
metadata.items = [itemToRemove]

// Call removeChannelMetadata with async/await
let (response, error) = await rtmClient.removeChannelMetadata(
    channelName: channelName,
    channelType: channelType,
    data: metadata,
    options: nil,
    lock: nil
)
if let errorInfo = error {
    // Handle error
    print("Failed to remove metadata with error: \(errorInfo.reason)")
} else if let removeMetadataResponse = response {
    // Handle success
    print("Metadata removed successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while removing metadata.")
}
// callback
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
// Create metadata items to remove
let itemToRemove = AgoraRtmMetadataItem()
itemToRemove.key = "topic"
itemToRemove.revision = 174298200
// Create metadata object
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 174298270
metadata.items = [itemToRemove]

// Call removeChannelMetadata with callback
rtmClient.removeChannelMetadata(
    channelName: channelName,
    channelType: channelType,
    data: metadata,
    options: nil,
    lock: nil
) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to remove metadata with error: \(errorInfo.reason)")
    } else if let removeMetadataResponse = response {
        // Handle success
        print("Metadata removed successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while removing metadata.")
    }
}

updateChannelMetadata

Description

The updateChannelMetadata method updates existing channel metadata. Each time you call this method, you can update one channel metadata or a channel metadata item.

After successfully updating channel metadata, users who subscribe to the channel and enable event listeners receive the channel type of didReceiveStorageEvent event notification. See Event listeners.

Information

You cannot use this method to update metadata items which do not exist.

Method

Call the updateChannelMetadata method as follows:

updateChannelMetadata(
    channelName: String,
    channelType: AgoraRtmChannelType,
    data: AgoraRtmMetadata, options: AgoraRtmMetadataOptions?, lock: String?
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
updateChannelMetadata(
    channelName: String,
    channelType: AgoraRtmChannelType,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?,
    lock: String?,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
dataAgoraRtmMetadataRequired-Metadata item. See AgoraRtmMetadata.
optionsAgoraRtmMetadataOptionsOptional-Options for setting the channel metadata.
lockStringOptionalnil string ''Lock name. If set, only users who call the acquireLock method to acquire the lock can perform operations.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmMetadataOptions data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
recordTsBoolOptionalfalseWhether to record the timestamp of the edits.
recordUserIdBoolOptionalfalseWhether to record the user ID of the editor.

Basic usage

// async-await
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
// Create metadata items to update
let metadataItem = AgoraRtmMetadataItem()
metadataItem.key = "topic"
metadataItem.value = "New Topic"
metadataItem.revision = 174298200
// Create metadata object
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 174298270
metadata.items = [metadataItem]
// Optional: Define metadata options if needed
let metadataOptions = AgoraRtmMetadataOptions()
metadataOptions.recordTs = true
metadataOptions.recordUserId = true

// Call updateChannelMetadata with async/await
let (response, error) = await rtmClient.updateChannelMetadata(
    channelName: channelName,
    channelType: channelType,
    data: metadata,
    options: metadataOptions,
    lock: nil
)
if let errorInfo = error {
    // Handle error
    print("Failed to update metadata with error: \(errorInfo.reason)")
} else if let updateMetadataResponse = response {
    // Handle success
    print("Metadata updated successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while updating metadata.")
}
// callback
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
// Create metadata items to update
let metadataItem = AgoraRtmMetadataItem()
metadataItem.key = "topic"
metadataItem.value = "New Topic"
metadataItem.revision = 174298200
// Create metadata object
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 174298270
metadata.items = [metadataItem]
// Optional: Define metadata options if needed
let metadataOptions = AgoraRtmMetadataOptions()
metadataOptions.recordTs = true
metadataOptions.recordUserId = true

// Call updateChannelMetadata with completion block (callback)
rtmClient.updateChannelMetadata(
    channelName: channelName,
    channelType: channelType,
    data: metadata,
    options: metadataOptions,
    lock: nil
) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to update metadata with error: \(errorInfo.reason)")
    } else if let updateMetadataResponse = response {
        // Handle success
        print("Metadata updated successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while updating metadata.")
    }
}

setUserMetadata

Description

The setUserMetadata method sets metadata for a user. If you call this method multiple times, the SDK retrieves the key of the metadata items and applies settings according to the following rules:

  • If you set metadata with different keys, the SDK adds each set of metadata in sequence according to the order of the method calls.

  • If you set metadata with the same key, the value of the last setting overwrites the previous one.

After successfully setting user metadata, users who subscribe to the user and enable event listeners receive the user type of didReceiveStorageEvent event notification. See Event listeners.

User metadata also provides version control logic CAS (Compare And Set). This method provides two independent version control fields. You can set one or both of them according to your actual business use-case:

  • Enable version number verification for the entire set of channel metadata by setting the majorRevision property in the AgoraRtmMetadata data type.

  • Enable version number verification for a single metadata item by setting the revision property in the AgoraRtmMetadataItem class.

When setting user metadata or metadata items, you can control whether to enable version number verification by specifying the revision property:

  • The default value of the revision property is -1, which indicates that this method call does not perform any CAS verification. If the user metadata or metadata item already exists, the latest value overwrites the previous one. If the user metadata or metadata item does not exist, the SDK creates it.

  • If the revision property is a positive integer, this method call performs the CAS verification. If the user metadata or metadata item already exists, the SDK updates the corresponding value after the version number verification succeeds. If the user metadata or metadata item does not exist, the SDK returns the error code.

Method

Call the setUserMetadata method as follows:

setUserMetadata(
    userId: String,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
setUserMetadata(
    userId: String,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
userIdStringRequireduserId of the current userUser ID.
dataAgoraRtmMetadataRequired-Metadata item. See AgoraRtmMetadata.
optionsAgoraRtmMetadataOptionsOptional-Options for setting the channel metadata.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmMetadataOptions data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
recordTsBoolOptionalfalseWhether to record the timestamp of the edits.
recordUserIdBoolOptionalfalseWhether to record the user ID of the editor.

Basic usage

// async-await
let userId = "exampleUser"
// Create metadata items to update
let metadataItem = AgoraRtmMetadataItem()
metadataItem.key = "status"
metadataItem.value = "active"
metadataItem.revision = 12345
// Create metadata object
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 12346
metadata.items = [metadataItem]
// Optional: Define metadata options if needed
let metadataOptions = AgoraRtmMetadataOptions()
metadataOptions.recordTs = true
metadataOptions.recordUserId = true

// Call setUserMetadata with async/await
let (response, error) = await rtmClient.setUserMetadata(
    userId: userId,
    data: metadata,
    options: metadataOptions
)
if let errorInfo = error {
    // Handle error
    print("Failed to set user metadata with error: \(errorInfo.reason)")
} else if let setUserMetadataResponse = response {
    // Handle success
    print("User metadata set successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while setting user metadata.")
}
// callback
let userId = "exampleUser"
// Create metadata items to update
let metadataItem = AgoraRtmMetadataItem()
metadataItem.key = "status"
metadataItem.value = "active"
metadataItem.revision = 12345
// Create metadata object
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 12346
metadata.items = [metadataItem]
// Optional: Define metadata options if needed
let metadataOptions = AgoraRtmMetadataOptions()
metadataOptions.recordTs = true
metadataOptions.recordUserId = true

// Call setUserMetadata with a callback
rtmClient.setUserMetadata(
    userId: userId,
    data: metadata,
    options: metadataOptions
) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to set user metadata with error: \(errorInfo.reason)")
    } else if let setUserMetadataResponse = response {
        // Handle success
        print("User metadata set successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while setting user metadata.")
    }
}

getUserMetadata

Description

The getUserMetadata method gets the metadata and metadata item for the specified user.

Method

Call the getUserMetadata method as follows:

getUserMetadata(userId: String) async -> (AgoraRtmGetMetadataResponse?, AgoraRtmErrorInfo?)
getUserMetadata(userId: String, completion completionBlock: AgoraRtmGetMetadataBlock? = nil)
ParametersTypeRequiredDefaultDescription
userIdStringRequireduserId of the current userUser ID.
completionAgoraRtmGetMetadataBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmGetMetadataResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmGetMetadataResponse data type contains the following properties:

PropertiesTypeDescription
dataAgoraRtmMetadataMetadata item. See AgoraRtmMetadata.

Basic usage

// async-await
let userId = "exampleUser"

// Call getUserMetadata with async/await
let (response, error) = await rtmClient.getUserMetadata(userId: userId)
if let errorInfo = error {
    // Handle error
    print("Failed to get user metadata with error: \(errorInfo.reason)")
} else if let getUserMetadataResponse = response {
    // Handle success and access the metadata
    if let metadata = getUserMetadataResponse.data {
        print("User metadata retrieved successfully.")
        for item in metadata.items ?? [] {
            print("Key: \(item.key), Value: \(item.value), Author: \(item.authorUserId), Revision: \(item.revision)")
        }
    }
} else {
    // Handle unknown error
    print("Unknown error occurred while getting user metadata.")
}
// callback
let userId = "exampleUser"

// Call getUserMetadata with a callback
rtmClient.getUserMetadata(userId: userId) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to get user metadata with error: \(errorInfo.reason)")
    } else if let getUserMetadataResponse = response {
        // Handle success and access the metadata
        if let metadata = getUserMetadataResponse.data {
            print("User metadata retrieved successfully.")
            for item in metadata.items ?? [] {
                print("Key: \(item.key), Value: \(item.value), Author: \(item.authorUserId), Revision: \(item.revision)")
            }
        }
    } else {
        // Handle unknown error
        print("Unknown error occurred while getting user metadata.")
    }
}

removeUserMetadata

Description

The removeUserMetadata method removes user metadata or metadata items.

After successfully removing user metadata, users who subscribe to the user and enable event listeners receive the user type of didReceiveStorageEvent event notification. See Event listeners.

Method

Call the removeUserMetadata method as follows:

removeUserMetadata(
    userId: String,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
removeUserMetadata(
    userId: String,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
userIdStringRequireduserId of the current userUser ID.
dataAgoraRtmMetadataRequired-Metadata item. See AgoraRtmMetadata.
optionsAgoraRtmMetadataOptionsOptional-Options for setting the channel metadata.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmMetadataOptions data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
recordTsBoolOptionalfalseWhether to record the timestamp of the edits.
recordUserIdBoolOptionalfalseWhether to record the user ID of the editor.

Basic usage

// async-await
let userId = "exampleUser"
// Create metadata items to remove
let metadataItem = AgoraRtmMetadataItem()
metadataItem.key = "status"
metadataItem.revision = 12345 // Make sure to provide the correct revision
// Create metadata object
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 12346
metadata.items = [metadataItem]
// Call removeUserMetadata with async/await
let (response, error) = await rtmClient.removeUserMetadata(
    userId: userId,
    data: metadata,
    options: nil
)

if let errorInfo = error {
    // Handle error
    print("Failed to remove user metadata with error: \(errorInfo.reason)")
} else if let removeUserMetadataResponse = response {
    // Handle success
    print("User metadata removed successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while removing user metadata.")
}
// callback
let userId = "exampleUser"

// Create metadata items to remove
let metadataItem = AgoraRtmMetadataItem()
metadataItem.key = "status"
metadataItem.revision = 12345 // Make sure to provide the correct revision

// Create metadata object
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 12346
metadata.items = [metadataItem]

// Call removeUserMetadata with a callback
rtmClient.removeUserMetadata(userId: userId, data: metadata, options: nil) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to remove user metadata with error: \(errorInfo.reason)")
    } else if let removeUserMetadataResponse = response {
        // Handle success
        print("User metadata removed successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while removing user metadata.")
    }
}

updateUserMetadata

Description

The updateUserMetadata method updates existing user metadata. After successfully updating channel metadata, users who subscribe to the user and enable event listeners receive the user type of didReceiveStorageEvent event notification. See Event listeners.

Information

You cannot use this method to update metadata items which do not exist.

Method

Call the updateUserMetadata method as follows:

updateUserMetadata(
    userId: String,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
updateUserMetadata(
    userId: String,
    data: AgoraRtmMetadata,
    options: AgoraRtmMetadataOptions?,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
userIdStringRequireduserId of the current userUser ID.
dataAgoraRtmMetadataRequired-Metadata item. See AgoraRtmMetadata.
optionsAgoraRtmMetadataOptionsOptional-Options for setting the channel metadata.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmMetadataOptions data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
recordTsBoolOptionalfalseWhether to record the timestamp of the edits.
recordUserIdBoolOptionalfalseWhether to record the user ID of the editor.

Basic usage

// async-await
let userId = "exampleUser"
// Create metadata items to update
let metadataItem = AgoraRtmMetadataItem()
metadataItem.key = "status"
metadataItem.value = "active"// Set the new value
metadataItem.revision = 12345// Provide the current revision
// Create metadata object
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 12346
metadata.items = [metadataItem]
// Optional: Define metadata options if needed
let metadataOptions = AgoraRtmMetadataOptions()
metadataOptions.recordTs = true
metadataOptions.recordUserId = true

// Call updateUserMetadata with async/await
let (response, error) = await rtmClient.updateUserMetadata(
    userId: userId,
    data: metadata,
    options: metadataOptions
)
if let errorInfo = error {
    // Handle error
    print("Failed to update user metadata with error: \(errorInfo.reason)")
} else if let updateUserMetadataResponse = response {
    // Handle success
    print("User metadata updated successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while updating user metadata.")
}
// callback
let userId = "exampleUser"
// Create metadata items to update
let metadataItem = AgoraRtmMetadataItem()
metadataItem.key = "status"
metadataItem.value = "active"// Set the new value
metadataItem.revision = 12345// Provide the current revision
// Create metadata object
let metadata = AgoraRtmMetadata()
metadata.majorRevision = 12346
metadata.items = [metadataItem]
// Optional: Define metadata options if needed
let metadataOptions = AgoraRtmMetadataOptions()
metadataOptions.recordTs = true
metadataOptions.recordUserId = true

// Call updateUserMetadata with a callback
rtmClient.updateUserMetadata(userId: userId, data: metadata, options: metadataOptions) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to update user metadata with error: \(errorInfo.reason)")
    } else if let updateUserMetadataResponse = response {
        // Handle success
        print("User metadata updated successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while updating user metadata.")
    }
}

subscribeUserMetadata

Description

The subscribeUserMetadata method subscribes to metadata for a specified user. After successfully subscribing to the user metadata, you receive the user type of didReceiveStorageEvent event notification when the metadata for that user changes. See Event listeners.

Method

Call the subscribeUserMetadata method as follows:

subscribeUserMetadata(userId: String) async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
subscribeUserMetadata(userId: String, completion completionBlock: AgoraRtmOperationBlock? = nil)
ParametersTypeRequiredDefaultDescription
userIdStringRequireduserId of the current userUser ID.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let userId = "exampleUser"

// Call subscribeUserMetadata with async/await
let (response, error) = await rtmClient.subscribeUserMetadata(userId: userId)
if let errorInfo = error {
    // Handle error
    print("Failed to subscribe to user metadata with error: \(errorInfo.reason)")
} else if let subscribeResponse = response {
    // Handle success
    print("Successfully subscribed to user metadata.")
} else {
    // Handle unknown error
    print("Unknown error occurred while subscribing to user metadata.")
}
// callback
let userId = "exampleUser"

// Call subscribeUserMetadata with a callback
rtmClient.subscribeUserMetadata(userId: userId) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to subscribe to user metadata with error: \(errorInfo.reason)")
    } else if let subscribeResponse = response {
        // Handle success
        print("Successfully subscribed to user metadata.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while subscribing to user metadata.")
    }
}

unsubscribeUserMetadata

Description

When you no longer need to receive notifications of changes to a user's metadata, call the unsubscribeUserMetadata method to unsubscribe.

Method

Call the unsubscribeUserMetadata method as follows:

unsubscribeUserMetadata(userId: String) async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
unsubscribeUserMetadata(userId: String, completion completionBlock: AgoraRtmOperationBlock? = nil)
ParametersTypeRequiredDefaultDescription
userIdStringRequireduserId of the current userUser ID.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let userId = "exampleUser"

// Call unsubscribeUserMetadata with async/await
let (response, error) = await rtmClient.unsubscribeUserMetadata(userId: userId)
if let errorInfo = error {
    // Handle error
    print("Failed to unsubscribe from user metadata with error: \(errorInfo.reason)")
} else if let unsubscribeResponse = response {
    // Handle success
    print("Successfully unsubscribed from user metadata.")
} else {
    // Handle unknown error
    print("Unknown error occurred while unsubscribing from user metadata.")
}
// callback
let userId = "exampleUser"

// Call unsubscribeUserMetadata with a callback
rtmClient.unsubscribeUserMetadata(userId: userId) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to unsubscribe from user metadata with error: \(errorInfo.reason)")
    } else if let unsubscribeResponse = response {
        // Handle success
        print("Successfully unsubscribed from user metadata.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while unsubscribing from user metadata.")
    }
}

AgoraRtmMetadata

Use the AgoraRtmMetadata data type to set and manage metadata, containing the following properties:

PropertiesTypeRequiredDefaultDescription
majorRevisionInt64Optional-1The version control switch:
- -1: Disable the version verification.
- > 0: Enable the version verification. The operation can only be performed if the target version number matches this value.
items[AgoraRtmMetadataItem]OptionalnilMetadata items.

The AgoraRtmMetadataItem data type contains the following properties:

PropertiesTypeRequiredDefaultDescription
keyStringRequired-Key.
valueStringRequired-Value.
authorUserIdStringRequired-The user ID of the editor. This value is read-only and does not support writing.
revisionUInt64Optional-1
- Returns the real version number in read operations.
- Serves as a version control switch in write operations:
- -1: Disable the version verification.
- > 0: Enable version verification, only perform the operation if the target version number matches this value.
updateTsUInt64Optional0Update timestamp. This value is read-only and does not support writing.

Lock

A critical resource should only be used by one process at a time. If a critical resource is shared between different processes, each process needs to adopt a mutually exclusive method to prevent mutual interference. Signaling provides a full set of lock solutions. By controlling different processes in a distributed system, you can solve the competition problem when users access shared resources.

Information

The client is able to set, remove, and revoke locks. Agora recommends that you control the permissions of these operations on the client side based on your business needs.

setLock

Description

Configure the lock name, time to live (TTL) and other parameters by calling the setLock method. If the configuration succeeds, all users in the channel receive the didReceiveLockEvent event notifications of lockSet type. For details, see Event Listeners.

Method

Call the setLock method as follows:

setLock(
    channelName: String,
    channelType: AgoraRtmChannelType,
    lockName: String,
    ttl: Int32
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
setLock(
    channelName: String,
    channelType: AgoraRtmChannelType,
    lockName: String,
    ttl: Int32,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
lockNameStringRequired-Lock name.
ttlInt32Optional10The expiration time of the lock. The value is in seconds, ranging from [10 to 300]. When the user who owns the lock goes offline, if the user returns to the channel within the time they can still use the lock; otherwise, the lock is released and the users who listen for the didReceiveLockEvent event receives the lockReleased event.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let lockName = "myLock"
// Lock for 1 hour
let ttl: Int32 = 3600

let (response, error) = await rtmClient.setLock(
    channelName: channelName,
    channelType: channelType,
    lockName: lockName,
    ttl: ttl)
if let errorInfo = error {
    // Handle error
    print("Failed to set lock with error: \(errorInfo.reason)")
} else if let setLockResponse = response {
    // Handle success
    print("Lock set successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while setting the lock.")
}
// callback
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let lockName = "myLock"
// Lock for 1 hour
let ttl: Int32 = 3600

rtmClient.setLock(
    channelName: channelName,
    channelType: channelType,
    lockName: lockName,
    ttl: ttl
) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to set lock with error: \(errorInfo.reason)")
    } else if let setLockResponse = response {
        // Handle success
        print("Lock set successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while setting the lock.")
    }
}

acquireLock

Description

After successfully configuring a lock, call the acquireLock method on the client to acquire the right to the lock. When you acquire the lock, other users in the channel receive the lockAcquired type of didReceiveLockEvent event. For details, see Event Listeners.

Method

Call the acquireLock method as follows:

acquireLock(
    channelName: String,
    channelType: AgoraRtmChannelType,
    lockName: String,
    retry: Bool
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
acquireLock(
    channelName: String,
    channelType: AgoraRtmChannelType,
    lockName: String,
    retry: Bool,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
lockNameStringRequired-Lock name.
retryBoolRequired-If the lock acquisition fails, whether to retry until the acquisition succeeds or the user leaves the channel.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let lockName = "myLock"
// Whether to retry acquiring the lock if it fails
let retry = true

let (response, error) = await rtmClient.acquireLock(
    channelName: channelName,
    channelType: channelType,
    lockName: lockName,
    retry: retry
)

if let errorInfo = error {
    // Handle error
    print("Failed to acquire lock with error: \(errorInfo.reason)")
} else if let acquireLockResponse = response {
    // Handle success
    print("Lock acquired successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while acquiring the lock.")
}
// callback
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let lockName = "myLock"
// Whether to retry acquiring the lock if it fails
let retry = true

rtmClient.acquireLock(
    channelName: channelName,
    channelType: channelType,
    lockName: lockName,
    retry: retry
) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to acquire lock with error: \(errorInfo.reason)")
    } else if let acquireLockResponse = response {
        // Handle success
        print("Lock acquired successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while acquiring the lock.")
    }
}

releaseLock

Description

When a user no longer needs to own a lock, call the releaseLock method on the client side to release the lock. After successful lock release, other users in the channel receive the lockReleased type of didReceiveLockEvent event. See Event Listeners.

At this time, if another user wants to acquire the lock, they can call the acquireLock method on the client side to compete. New users acquiring locks have the same contention priority as the users who set the retry property to automatically retry to acquire locks.

Method

Call the releaseLock method as follows:

releaseLock(
    channelName: String,
    channelType: AgoraRtmChannelType,
    lockName: String
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
releaseLock(
    channelName: String,
    channelType: AgoraRtmChannelType,
    lockName: String,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
lockNameStringRequired-Lock name.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let lockName = "myLock"

let (response, error) = await rtmClient.releaseLock(
    channelName: channelName,
    channelType: channelType,
    lockName: lockName
)

if let errorInfo = error {
    // Handle error
    print("Failed to acquire lock with error: \(errorInfo.reason)")
} else if let acquireLockResponse = response {
    // Handle success
    print("Lock released successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while releasing the lock.")
}
// callback
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let lockName = "myLock"

rtmClient.releaseLock(
    channelName: channelName,
    channelType: channelType,
    lockName: lockName
) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to acquire lock with error: \(errorInfo.reason)")
    } else if let acquireLockResponse = response {
        // Handle success
        print("Lock released successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while releasing the lock.")
    }
}

revokeLock

Description

To ensure that your business is not affected when a lock is occupied, you may need to revoke the lock and give other users a chance to acquire it. Revoke the occupied lock by calling revokeLock. When a lock is revoked, all users in the channel receive the lockReleased type of didReceiveLockEvent event. See Event Listeners.

At this time, if other users want to acquire the lock, they can call the acquireLock method on the client side to compete. New users acquiring locks have the same contention priority as the users who set the retry property to automatically retry to acquire locks.

Method

Call the revokeLock method as follows:

revokeLock(
    channelName: String,
    channelType: AgoraRtmChannelType,
    lockName: String,
    userId: String
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
revokeLock(
    channelName: String,
    channelType: AgoraRtmChannelType,
    lockName: String,
    userId: String,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
lockNameStringRequired-Lock name.
userIdStringRequired-The ID of the user who has a lock.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let lockName = "myLock"
let userId = "user123"// The ID of the user who holds the lock
let (response, error) = await rtmClient.revokeLock(
    channelName: channelName,
    channelType: channelType,
    lockName: lockName,
    userId: userId
)
if let errorInfo = error {
    // Handle error
    print("Failed to revoke lock with error: \(errorInfo.reason)")
} else if let revokeLockResponse = response {
    // Handle success
    print("Lock revoked successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while revoking the lock.")
}
// callback
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let lockName = "myLock"
let userId = "user123" // The ID of the user who holds the lock
rtmClient.revokeLock(
    channelName: channelName,
    channelType: channelType,
    lockName: lockName,
    userId: userId
) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to revoke lock with error: \(errorInfo.reason)")
    } else if let revokeLockResponse = response {
        // Handle success
        print("Lock revoked successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while revoking the lock.")
    }
}

getMessages

Description

To query lock information such as the total number of locks, lock name, lock user, and time to live, call the getMessages method on the client.

Method

Call the getMessages method as follows:

getLocks(
    channelName: String,
    channelType: AgoraRtmChannelType
)   async -> (AgoraRtmGetLocksResponse?, AgoraRtmErrorInfo?)
getLocks(
    channelName: String,
    channelType: AgoraRtmChannelType,
    completion completionBlock: AgoraRtmGetLocksBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
completionAgoraRtmGetLocksBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmGetLocksResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

The AgoraRtmGetLocksResponse data type contains the following properties:

PropertiesTypeDescription
lockDetailList[AgoraRtmLockDetail]Lock details array.

The AgoraRtmLockDetail data type contains the following properties:

PropertiesTypeDescription
lockNameStringLock name.
ownerStringThe ID of the user who has a lock.
ttlInt32The expiration time of the lock. The value is in seconds, ranging from [10 to 300]. When the user who owns the lock goes offline, if the user returns to the channel within the time they can still use the lock; otherwise, the lock is released and the users who listen for the didReceiveLockEvent event receives the lockReleased event.

Basic usage

// async-await
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let (response, error) = await rtmClient.getLocks(
    channelName: channelName,
    channelType: channelType
)
if let errorInfo = error {
    // Handle error
    print("Failed to get locks with error: \(errorInfo.reason)")
} else if let getLocksResponse = response {
    // Handle success
    print("Locks retrieved successfully.")
    print("Locks: \(getLocksResponse.locks)")
} else {
    // Handle unknown error
    print("Unknown error occurred while retrieving the locks.")
}
// callback
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
rtmClient.getLocks(
    channelName: channelName,
    channelType: channelType
) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to get locks with error: \(errorInfo.reason)")
    } else if let getLocksResponse = response {
        // Handle success
        print("Locks retrieved successfully.")
        print("Locks: \(getLocksResponse.locks)")
    } else {
        // Handle unknown error
        print("Unknown error occurred while retrieving the locks.")
    }
}

removeLock

Description

When you no longer need a lock, call the removeLock method to remove the lock. After successfully removing the lock, all users in the channel receive the lockRemoved type of didReceiveLockEvent event notification. See Event Listeners.

Method

Call the removeLock method as follows:

removeLock(
    channelName: String,
    channelType: AgoraRtmChannelType,
    lockName: String
)   async -> (AgoraRtmCommonResponse?, AgoraRtmErrorInfo?)
removeLock(
    channelName: String,
    channelType: AgoraRtmChannelType,
    lockName: String,
    completion completionBlock: AgoraRtmOperationBlock? = nil
)
ParametersTypeRequiredDefaultDescription
channelNameStringRequired-Channel name.
channelTypeAgoraRtmChannelTypeRequired-Channel type. See AgoraRtmChannelType.
lockNameStringRequired-Lock name.
completionAgoraRtmOperationBlockOptional-Callback of invocation result:
- Success: Returns the AgoraRtmCommonResponse data in the response parameter, and nil in the errorInfo parameter.
- Failure: Returns nil in the response parameter, and error information in the errorInfo parameter.

Basic usage

// async-await
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let lockName = "myLock"

let (response, error) = await rtmClient.removeLock(
    channelName: channelName,
    channelType: channelType,
    lockName: lockName
)

if let errorInfo = error {
    // Handle error
    print("Failed to remove lock with error: \(errorInfo.reason)")
} else if let removeLockResponse = response {
    // Handle success
    print("Lock removed successfully.")
} else {
    // Handle unknown error
    print("Unknown error occurred while removing the lock.")
}
// callback
let channelName = "exampleChannel"
let channelType: AgoraRtmChannelType = .message
let lockName = "myLock"

rtmClient.removeLock(
    channelName: channelName,
    channelType: channelType,
    lockName: lockName
) { (response, error) in
    if let errorInfo = error {
        // Handle error
        print("Failed to remove lock with error: \(errorInfo.reason)")
    } else if let removeLockResponse = response {
        // Handle success
        print("Lock removed successfully.")
    } else {
        // Handle unknown error
        print("Unknown error occurred while removing the lock.")
    }
}

Enumerated types

Enum

AgoraRtmAreaCode

The region for connection, which is the region where the server the SDK connects to is located.

ValueDescription
CN0x00000001: Mainland China.
NA0x00000002: North America.
EU0x00000004: Europe.
AS0x00000008: Asia, excluding Mainland China.
JP0x00000010: Japan.
IN0x00000020: India.
GLOB0xFFFFFFFF: Global.

AgoraRtmChannelType

Channel types.

ValueDescription
message1: Message channel.
stream2: Stream channel.
user3: User Channel.

AgoraRtmClientConnectionChangeReason

Reasons causing the change of the connection state.

ValueDescription
connecting0: The SDK is connecting with the server.
joinSuccess1: The SDK has joined the channel successfully.
interrupted2: The connection between the SDK and the server is interrupted.
bannedByServer3: The connection between the SDK and the server is banned by the server.
joinFailed4: The SDK fails to join the channel. When the SDK fails to join the channel for more than 20 minutes, this error occurs and the SDK stops reconnecting to the channel.
leaveChannel5: The SDK has left the channel.
invalidAppId6: The connection failed because the App ID is not valid.
invalidChannelName7: The connection failed because the channel name is not valid.
invalidToken8: The connection failed because the token is not valid.
tokenExpired9: The connection failed because the token is expired.
rejectedByServer10: The connection is rejected by server.
settingProxyServer11: The connection state changed to reconnecting because the SDK has set a proxy server.
renewToken12: The connection state changed because the token is renewed.
clientIpAddressChanged13: The IP address of the client has changed, possibly because the network type, IP address, or port has been changed.
keepAliveTimeout14: Timeout for the keep-alive of the connection between the SDK and the server. The connection state changes to reconnecting.
rejoinSuccess15: The user has rejoined the channel successfully.
changedLost16: The connection between the SDK and the server is lost.
echoTest17: The connection state changes due to the echo test.
clientIpAddressChangedByUser18: The local IP address was changed by the user. The connection state changes to reconnecting.
sameUidLogin19: The user joined the same channel from different devices with the same UID.
tooManyBroadcasters20: The number of hosts in the channel has reached the upper limit.
licenseValidationFailure21: License verification failed.
certificationVerifyFailure22: Server certificate verification failed.
streamChannelNotAvailable23: The stream channel does not exist.
inconsistentAppId24: App ID and Token do not match.
loginSuccess10001: The SDK logs in to the Signaling system.
logout10002: The SDK logs out from the Signaling system.
presenceNotReady10003: Presence service is not ready. You need to call the login method again to log in to the Signaling system and re-execute all operations on the SDK.

AgoraRtmClientConnectionState

SDK connection states.

ValueDescription
disconnected1: The SDK has disconnected with the server.
connecting2: The SDK is connecting with the server.
connected3: The SDK has connected with the server.
reconnecting4: The connection is lost. The SDK is reconnecting with the server.
failed5: The SDK failed to connect with the server.

AgoraRtmEncryptionMode

Encryption mode.

ValueDescription
none0: No encryption.
AES128GCM1: AES-128-GCM mode.
AES256GCM2: AES-256-GCM mode.

AgoraRtmJoinChannelFeature

The event notification types set when joining a channel. You can use bitwise operations to set multiple event notifications simultaneously.

ValueDescription
``0: No event notifications.
presence1: Sets the presence event notification.
metadata1 << 1: Sets the storage event notification.
lock1 << 2: Sets the lock event notification.
beQuiet1 << 3: Sets the silent mode. In this mode, the SDK has the following behaviors:
- You can still receive other users' event notifications.
- Event notifications related to your channel activity such as joining or leaving the channel, and actions related to setting, getting, or deleting temporary user states, can not be broadcasted to other users.
- When calling the getOnlineUsers method, your information can not be found.
- When calling the getUserChannels method, channels that you join in silent mode can not be detected.

AgoraRtmLockEventType

Lock event type.

ValueDescription
snapshot1: The snapshot of the lock when the user joined the channel.
lockSet2: The lock is set.
lockRemoved3: The lock is removed.
lockAcquired4: The lock is acquired.
lockReleased5: The lock is released.
lockExpired6: The lock expired.

AgoraRtmLogLevel

Log output levels.

ValueDescription
none0x0000: No log.
info0x0001: Output the log at the FATAL, ERROR, WARN, or INFO level. We recommend you set to this value.
warn0x0002: Output the log at the FATAL, ERROR, WARN level.
error0x0004: Output the log at the FATAL, ERROR level.
fatal0x0008: Output the log at the FATAL level.

AgoraRtmMessagePriority

Message priority.

ValueDescription
highest0: Highest.
high1: High.
normal4: Normal.
low8: Low.

AgoraRtmMessageQos

QoS guarantee when sending topic messages.

ValueDescription
unordered0: Message data is not guaranteed to arrive in order.
ordered1: Message data arrives in order.

AgoraRtmPresenceEventType

Presence event type.

ValueDescription
snapshot1: The snapshot of the presence when the user joined the channel.
interval2: When users in the channel reach the setting value, the event notifications are sent at intervals rather than in real time.
remoteJoinChannel3: A remote user joined the channel.
remoteLeaveChannel4: A remote user left the channel.
remoteConnectionTimeout5: A remote user's connection timed out.
remoteStateChanged6: A remote user's temporary state changed.
errorOutOfService7: The user did not enable presence when joining the channel.

AgoraRtmLinkOperation

Operation type.

ValueDescription
login0: The user logins to the Signaling system.
logout1: The user logouts of the Signaling system.
join2: The user joins in a stream channel.
leave3: The user leaves a stream channel.
serverReject4: The Signaling server reject the connection.
autoReconnect5: The SDK is automatically reconnecting to the Signaling server.
reconnected6: The SDK is reconnected to the Signaling server.
heartbeatTimeout7: The Signaling server does not receive the heartbeat packet within the specified timeout period.
serverTimeout8: The Signaling server has timed out.
networkChange9: The network status changes.

AgoraRtmLinkState

Link state type.

ValueDescription
idle0: The init state.
connecting1: Connecting.
connected2: Connected.
disconnected3: Disconnected.
suspended4: Suspended.
failed5: Failed.

AgoraRtmLinkStateChangeReason

Link state change reason.

ValueDescription
unknown0: Unknown reason.
login1: Logging in.
loginSuccess2: Login successful.
loginTimeout3: Login timeout.
loginNotAuthorized4: Login not authorized.
loginRejected5: Login rejected.
relogin6: Re-login.
logout7: Logout.
autoReconnect8: Auto-reconnect.
reconnectTimeout9: Reconnect timeout.
reconnectSuccess10: Reconnect successful.
join11: Joining a channel.
joinSuccess12: Join channel successful.
joinFailed13: Join channel failed.
rejoin14: Re-join a channel.
leave15: Leave a channel.
invalidToken16: Invalid token.
tokenExpired17: Token expired.
inconsistentAppId18: Inconsistent app ID.
invalidChannelName19: Invalid channel name.
invalidUserId20: Invalid user ID.
notInitialized21: SDK not initialized.
rtmServiceNotConnected22: RTM service not connected.
channelInstanceExceedLimitation23: Channel instance exceeds the limit.
operationRateExceedLimitation24: Operation frequency exceeds the limit.
channelInErrorState25: Channel in error state.
presenceNotConnected26: Presence service not connected.
sameUidLogin27: Login with the same user ID.
kickedOutByServer28: Kicked out by the server.
keepAliveTimeout29: Keepalive timeout.
connectionError30: Connection error.
presenceNotReady31: Presence service not ready.
networkChange32: Network changed.
serviceNotSupported33: Service not supported.
streamChannelNotAvailable34: Stream Channel not available.
storageNotAvailable35: Storage service not available.
lockNotAvailable36: Lock service not available.
loginTooFrequent37: The login operation is too frequent.

AgoraRtmProtocolType

Protocol type.

ValueDescription
tcpUdp0: Both TCP and UDP protocols.
tcpOnly1: Only TCP protocol.

AgoraRtmServiceType

Service type

ValueDescription
messageThe foundational services comprise the message channel, user channel, presence, storage, and lock services.
streamThe stream channel service.

Information

To fully utilize all services offered by Signaling, you can employ bitwise operations to simultaneously configure two service types.

AgoraRtmProxyType

Proxy type.

ValueDescription
none0: Do not enable the proxy.
http1: Enable the proxy for the HTTP protocol.
cloudTcp2: Enable the cloud proxy for the TCP protocol.

AgoraRtmStorageEventType

Storage event type.

ValueDescription
snapshot1: When a user subscribes to channel metadata or user etadata for the first time, or joins a channel, the local user receives notifications of this type of event.
set2: Occurs when calling setChannelMetadata or setUserMetadata. Caution: This event only occurs in incremental data update mode.
update3: Occurs when calling methods to set, update, or delete the channel metadata or user metadata.
remove4: Occurs when calling removeChannelMetadata or removeUserMetadata. Caution: This event only occurs in incremental data update mode.

AgoraRtmStorageType

Storage type.

ValueDescription
user1: User metadata event.
channel2: Channel metadata event.

AgoraRtmSubscribeChannelFeature

The event notification types set when subscribing to a channel. You can use bitwise operations to set multiple event notifications simultaneously.

ValueDescription
``0: No event notifications
presence1: Sets the presence event notification.
metadata1 << 1: Sets the storage event notification.
message1 << 2: Sets the message event notification.
lock1 << 3: Sets the lock event notification.
beQuiet1 << 4: Sets the silent mode. In this mode, the SDK has the following behaviors:
- You can still receive other users' event notifications.
- Event notifications related to your channel activity such as subscribing or unsubscribing the channel, and actions related to setting, getting, or deleting temporary user states, can not be broadcasted to other users.
- When calling the getOnlineUsers method, your information can not be found.
- When calling the getUserChannels method, channels that you join in silent mode can not be detected.

AgoraRtmTopicEventType

Topic event type.

ValueDescription
snapshot1: The snapshot of the topic when the user joined the channel.
remoteJoinTopic2: A remote user joined the channel.
remoteLeaveTopic3: A remote user left the channel.

Troubleshooting

Refer to the following information for troubleshooting API calls.

ErrorInfo

When calling the Signaling Swift API, if an error occurs, the SDK returns a value of type AgoraRtmErrorInfo:

PropertiesTypeDescription
errorCodeAgoraRtmErrorCodeError code for this operation.
reasonStringError reason for this operation.
operationStringOperation type.

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

Error codes table

Refer to the following error codes table to identify and troubleshoot the problem:

Error codeError descriptionCause and solution
0okCorrect call
-10001notInitializedThe SDK is not initialized. Please initialize the RtmClient instance by calling the `` method before performing other operations.
-10002notLoginThe user called the API without logging in to Signaling, disconnected due to timeout, or actively logged out. Please log in to Signaling first.
-10003invalidAppIdInvalid App ID:
- Check that the App ID is correct.
- Ensure that Signaling has been activated for the App ID.
-10005invalidTokenInvalid Token:
- The token is invalid, check whether the Token Provider generates a valid Signaling Token.
-10006invalidUserIdInvalid User ID:
- Check if user ID is empty.
- Check if the user ID contains illegal characters.
-10007initServiceFailedSDK initialization failed. Please reinitialize by calling the `` method.
-10008invalidChannelNameInvalid channel name:
- Check if the channel name is empty.
- Check if the channel name contains illegal characters.
-10009tokenExpiredToken expired. Call renewToken to reacquire the Token.
-10010loginNoServerResourcesServer resources are limited. It is recommended to log in again.
-10011loginTimeoutLogin timeout. Check whether the current network is stable and switch to a stable network environment.
-10012loginRejectedSDK login rejected by the server:
- Check tha Signaling is activated on your App ID.
- Check if the token or userId is banned.
-10013loginAbortedSDK login interrupted due to unknown problem:
- Check that the current network is stable and switch to a stable network environment.
- The current userId is logged in.
-10014invalidParameterInvalid parameter. Please check if the parameters you provided are correct.
-10015loginNotAuthorizedNo RTM service permissions. Check that the console opens Signaling services.
-10016loginInconsistentAppIdInconsistent App ID. Please check whether the App ID used for initialization, login, and joining a channel are consistent.
-10017duplicateOperationDuplicate operation.
-10018instanceAlreadyReleasedRepeat rtm instantiation or RTMStreamChannel instantiation.
-10019invalidChannelTypeInvalid channel type. The SDK only supports the following channel types. Please use the correct value:
- message: Message Channel
- stream: Stream Channel
- user: User Channel
-10020invalidEncryptionParameterMessage encryption parameters are invalid.
- Check that the encryption key generated is a String.
- Check that the generated encryption salt is Uint8Array type and that the length is 32 bytes.
- Check that the encryption method matches the encryption key and the encryption salt.
-10021operationRateExceedLimitationChannel metadata or User Metadata -related API call frequency is exceeding the limit. Please control the call frequency within 10/second.
-10022serviceNotSupportThe service type is not supported. Check whether the service type you set in AgoraRtmServiceType is correct. This error code is only applicable to the private deployment function.
-10023loginCanceledThe login operation has been canceled. Possible reasons are as follows:
- After calling the login method, if you call the method again before receiving the call result, the previous call operation will be canceled and the SDK will execute the next call.
- Calling the logout method to log out before successfully logging in.
-10024invalidPrivateConfigThe private deployment parameter settings are invalid. Please check whether the service type and server address you set in AgoraRtmPrivateConfig are valid.
-10025notConnectedNot connected to the Signaling server. When you call APIs related to message subscription, metadata, or the Lock module, this error is reported if the Signaling service is not connected.
-10026renewTokenTimeoutToken renewal timed out.
-11001channelNotJoinedThe user has not joined the channel:
- The user is not online, offline or has not joined the channel
- Check for typos in userId.
-11002channelNotSubscribedThe user has not subscribed to the channel:
- The user is not online, offline or has not joined the channel
- Check for typos in userId.
-11003channelExceedTopicUserLimitationThe number of subscribers to this topic exceeds the limit.
-11004channelReusedIn co-channel mode, RTM released the Stream Channel.
-11005channelInstanceExceedLimitationThe number of created or subscribed channels exceeds the limit. See API usage limits for details.
-11006channelInErrorStateChannel is not available. Please recreate the Stream Channel or resubscribe to the Message Channel.
-11007channelJoinFailedFailed to join this channel:
- Check if the number of joined channels exceeds the limit.
- Check if the channel name is illegal.
- Check if the network is disconnected.
-11008channelInvalidTopicNameInvalid topic name:
- Check whether the topic name contains illegal characters.
- Check if the topic name is empty.
-11009channelInvalidMessageInvalid message. Check whether the message type is legal, Signaling only supports string, Uint8Array type messages.
-11010channelMessageLengthExceedLimitationMessage length exceeded limit. Check if the message payload size exceeds the limit:
- Message Channel single message package limit is 32 KB.
- Stream Channel single message package limit is 1 KB.
-11011channelInvalidUserListInvalid user list:
- Check if the user list is empty.
- Check if the user list contains invalid entries.
-11012channelNotAvailableInvalid user list:
- Check if the user list is empty
- Check if the user list contains illegal items.
-11013channelTopicNotSubscribedThe topic is not subscribed.
-11014channelExceedTopicLimitationThe number of topics exceeds the limit.
-11015channelJoinTopicFailedFailed to join this topic. Check whether the number of added topics exceeds the limit.
-11016channelTopicNotJoinedThe topic has not been joined. To send a message, you need to join the Topic first.
-11017channelTopicNotExistThe topic does not exist. Check that the topic name is correct.
-11018channelInvalidTopicMetaThe meta parameters in the topic are invalid. Check if the meta parameter exceeds 256 bytes.
-11019channelSubscribeTimeoutChannel subscription timed out. Check for broken connections.
-11020channelSubscribeTooFrequentThe channel subscription operation is too frequent. Make sure that the subscription operation of the same channel within a 5 seconds interval does not exceed 2 attempts.
-11021channelSubscribeFailedChannel subscription failed.11003 Check if the number of subscribed channels exceeds the limit.
-11022channelUnsubscribeFailedFailed to unsubscribe from the channel. Check if the connection is disconnected.
-11023channelEncryptMessageFailedMessage encryption failed:
- Check that the cipherKey is valid.
- Check that the salt is valid.
- Check if encryptionMode mode matches the cipherKey and salt.
-11024channelPublishMessageFailedMessage publishing failed. Check for broken connections.
-11026channelPublishMessageTimeoutMessage publishing timed out. Check for broken connections.
-11027channelNotConnectedThe SDK is disconnected from the Signaling server. Please log in again.
-11028channelLeaveFailedFailed to leave the channel. Check for broken connections.
-11029channelCustomTypeLengthOverflowCustom type length overflow. The length of the customType field must to be within 32 characters.
-11030channelInvalidCustomTypecustomType field is invalid. Check the customType field for illegal characters.
-11031channelUnsupportedMessageTypeMessage type is not supported.
-11032channelPresenceNotReadyPresence service is not ready. Please rejoin the Stream Channel or resubscribe to the Message Channel.
-11033channelReceiverOfflineWhen sending a user message, the remote user is offline:
- Check if the user ID set when calling the method is correct.
- Check if the remote user is logged in and online.
-11034channelJoinCanceledThe join channel operation has been canceled. After calling the joinTopic method, if you call the method again before receiving the call result, the previous call operation will be canceled and the SDK will execute the next call.
-12001storageOperationFailedStorage operation failed.
-12002storageMetadataItemExceedLimitationThe number of Storage Metadata Items exceeds the limit.
-12003storageInvalidMetadataItemInvalid Metadata Item.
-12004storageInvalidArgumentInvalid argument.
-12005storageInvalidRevisionInvalid Revision parameter.
-12006storageMetadataLengthOverflowMetadata overflows.
-12007storageInvalidLockNameInvalid Lock name.
-12008storageLockNotAcquiredThe Lock was not acquired.
-12009storageInvalidKeyInvalid Metadata key.
-12010storageInvalidValueInvalid metadata value.
-12011storageKeyLengthOverflowMetadata key length overflow.
-12012storageValueLengthOverflowMetadata value length overflow.
-12013storageDuplicateKeyDuplicate Metadata Item key.
-12014storageOutdatedRevisionOutdated Revision parameter.
-12015storageNotSubscribeThis channel is not subscribed.
-12016storageInvalidMetadataInstanceMetadata instance does not exist. Please create a Metadata instance.
-12017storageSubscribeUserExceedLimitationThe number of subscribers exceeds the limit.
-12018storageOperationTimeoutStorage operation timed out.
-12019storageNotAvailableThe Storage service is not available.
-13001presenceNotConnectedThe user is not connected to the system.
-13002presenceNotWritablePresence service is unavailable.
-13003presenceInvalidArgumentInvalid argument.
-13004presenceCacheTooManyStatesThe temporary user state cached before joining the channel exceeds the limit. See API usage limits for details.
-13005presenceStateCountOverflowThe number of temporary user state key/value pairs exceeds the limit. See API usage limits for details.
-13006presenceInvalidStateKeyInvalid state key.
-13007presenceInvalidStateValueInvalid state value.
-13008presenceStateKeySizeOverflowPresence key length overflow.
-13009presenceStateValueSizeOverflowPresence value overflow
-13010presenceStateDuplicateKeyRepeated state key.
-13011presenceUserNotExistThe user does not exist.
-13012presenceOperationTimeoutPresence operation timed out.
-13013presenceOperationFailedPresence operation failed.
-14001lockOperationFailedLock operation failed.
-14002lockOperationTimeoutLock operation timed out.
-14003lockOperationPerformingLock operation in progress.
-14004lockAlreadyExistLock already exists.
-14005lockInvalidNameInvalid Lock name.
-14006lockNotAcquiredThe Lock was not acquired.
-14007lockAcquireFailedFailed to acquire the Lock.
-14008lockNotExistThe Lock does not exist.
-14009lockNotAvailableLock service is not available.