User channels
Updated
Send direct, point-to-point messages to individual users.
Signaling enables you to send direct, point-to-point messages to individual users through user channels. This feature is useful in one-on-one communication use-cases such as private chats and customer support interactions.
Understand the tech
To implement point-to-point communication between individual users in your app:
- Set up the Signaling SDK: Integrate the Signaling SDK in your app and initialize an instance of the Signaling client.
- Authenticate the user: Log in to Signaling using a unique user ID.
- Send direct messages: Call the
publishmethod with the User channel type and specify the recipient's user ID as the channel name to send direct messages. - Handle incoming messages: Listen for message events to receive messages sent directly to the local user.
Prerequisites
Ensure that you have:
- Integrated the Signaling SDK in your project, and implemented the framework functionality from the SDK quickstart.
Implement user messages
This section shows you how to use the Signaling SDK to send messages directly to a specified user.
In a user channel, you send a point-to-point message to a specified user by calling the publish method. Set the channelType parameter to user and the channelName parameter to the user ID of the specified user. This method enables you to send messages to one user at a time. While Signaling does not limit the number of users you can send messages to or receive messages from, it does limit the frequency at which you can send messages to users.
Refer to the following sample code for sending messages:
String message
let message = "Hello Agora!"
let user = "Tony"
let publishOption = AgoraRtmPublishOptions()
publishOption.channelType = .user
rtm.publish(channelName: user, message: message, option: publishOption, completion: { res, error in
if error != nil {
print("\(error?.operation) failed! error reason is \(error?.reason)")
} else {
print("success")
}
})// Send string message
NSString* message = @"Hello Agora!";
NSString* user = @"Tony";
AgoraRtmPublishOptions* publish_option = [[AgoraRtmPublishOptions alloc] init];
publish_option.channelType = AgoraRtmChannelTypeUser;
[rtm publish:user message:message option:publish_option completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"publish success!!");
} else {
NSLog(@"publish failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];Binary message
let bytes: [UInt8] = [ /* your raw values */ ]
let rawMessage = Data(bytes: bytes, count: bytes.count)
streamChannel.publish(channelName: user, data: rawMessage, option: nil) { response, errorInfo in
if errorInfo == nil {
print("publish success!!")
} else {
print("publish failed, errorCode \(errorInfo!.errorCode), reason \(errorInfo!.reason)")
}
}// Send string message
NSString* message = @"Hello Agora!";
NSString* user = @"Tony";
AgoraRtmPublishOptions* publish_option = [[AgoraRtmPublishOptions alloc] init];
publish_option.channelType = AgoraRtmChannelTypeUser;
[rtm publish:user message:message option:publish_option completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"publish success!!");
} else {
NSLog(@"publish failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];Signaling currently supports only string and binary message formats. To send other types of data such as JSON objects, or data from third-party data construction tools such as protobuf, serialize the data before sending the message. For information on how to effectively construct the payload data structure and recommended serialization methods, refer to Message payload structuring.
Reference
This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.
Message packet size
Signaling SDK imposes 32 KB size limits on message payload packets sent in user channels. The message payload packet size includes the message payload itself plus the size of the customType field. If the message payload package size exceeds the limit, you receive the following error message.
// errorInfo
{
errorInfo.errorCode = rrorCode = .channelMessageLengthExceedLimitation
errorInfo.reason = "Publish too long message."
errorInfo.operation = "publish"; // or "publishTopicMessage
}// errorInfo
{
errorInfo.errorCode = AgoraRtmErrorChannelMessageLengthExceedLimitation;
errorInfo.reason = @"Publish too long message.";
errorInfo.Operation = @"publish"; // or "publishTopicMessage"
}To avoid sending failure due to excess message payload packet size, check the packet size before sending.
API reference
Swift
Objective-C
