Skip to content

Latest commit

 

History

History

Architecture

alt text

(back to top)

Usage

This sample showcases peer-to-peer messaging in Agora RTM SDK. There is no need to subscribe to a particular channel before receiving messages. The user can directly publish the message to another user.

Subscribe features Description
.user Callback to receive messages from users directly

Sample Code

Initialize the Agora RTM SDK

// Initialize the Agora RTM SDK
let config = AgoraRtmClientConfig(appId: "your_app_id" , userId: "user_id")
var agoraRtmKit: AgoraRtmClientKit = try AgoraRtmClientKit(config, delegate: self)

Login to Agora Server

// Login to Agora Server
if let (response, error) = await agoraRtmKit?.login("user_token") {
    if error == nil{
       // Login successful
    }else{
      // Login failed
    }
} else {
    // Login failed
}

Subscribe to a Channel

// Define the subscription feature
let subOptions: AgoraRtmSubscribeOptions = AgoraRtmSubscribeOptions()
subOptions.features =  [.message, .presence]

// Subscribe to a channel  
if let (response, error) = await agoraRtmKit?.subscribe(channelName: channelName, option: subOptions){
    if error == nil{
       // Subscribe successful
    }else{
      // Subscribe failed
    }
}

Publish to a User

// Define the publish options
let pubOptions = AgoraRtmPublishOptions()
pubOptions.channelType = .user

// Publish message to a user
if let (response, error) = await agoraRtmKit?.publish(channelName: "remoteUserName", message: messageString, option: pubOptions){
    if error == nil {
        // Publish successful
    }else{
        // Publish failed
    }
    
}

Logout RTM

// Logout RTM server
func logoutRTM(){
    agoraRtmKit?.logout()
    agoraRtmKit?.destroy()
}

Setup RTM Callbacks

// Receive 'message' event notifications in subscribed message channels and subscribed topics.
func rtmKit(_ rtmKit: AgoraRtmClientKit, didReceiveMessageEvent event: AgoraRtmMessageEvent) {
    switch event.channelType {
    case .message:
        break
    case .stream:
        break
    case .user:
        print("Received msg = \(event.message.stringData ?? "Empty") from \(event.publisher)")
        break
    case .none:
        break
    @unknown default:
    }
}

References

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Bac Huang - [email protected]

Project Link: https://github.com/Bac1314/APIExample_AgoraRTM2x

(back to top)