Manage open channel metadata
Use this page when you need to store lightweight key-value state on an open channel, such as seat assignment, game state, or room attributes. The iOS Chat SDK exposes this through OpenChannel instance methods.
Prerequisites
- Enable Open channel custom attributes in the Console under Chat > Chat settings > Open Channels.
- Join or enter the target open channel before relying on metadata sync callbacks.
Limits and behavior
- Metadata is destroyed when the open channel is destroyed.
- Each open channel can store up to 100 key-value pairs.
- Set and delete APIs support up to 10 keys per call.
- Recommended operation frequency is up to 100 metadata operations per second per channel.
Listen for metadata changes
Register an OpenChannelHandler to receive the initial metadata sync and later metadata changes. See Open channel events for registration details.
- Swift
- Objective-C
import NexconnChatSDK
final class OpenChannelMetadataObserver: NSObject, OpenChannelHandler {
func onMetadataSynced(_ event: OpenChannelMetadataSyncedEvent) {
print("Metadata synced for channel: \(event.channelId)")
}
func onMetadataChanged(_ event: OpenChannelMetadataChangedEvent) {
for change in event.changeInfo {
switch change.operationType {
case .update:
print("Key updated: \(change.key) = \(change.value ?? "") in channel \(change.channelId)")
case .remove:
print("Key deleted: \(change.key) in channel \(change.channelId)")
}
}
}
}
- (void)onMetadataSynced:(NCOpenChannelMetadataSyncedEvent *)event {
NSLog(@"Metadata synced for channel: %@", event.channelId);
}
- (void)onMetadataChanged:(NCOpenChannelMetadataChangedEvent *)event {
for (NCOpenChannelMetadataChangeInfo *change in event.changeInfo) {
if (change.operationType == NCOpenChannelMetadataOperationTypeRemove) {
NSLog(@"Key deleted: %@ in channel %@", change.key, change.channelId);
} else {
NSLog(@"Key updated: %@ = %@ in channel %@", change.key, change.value, change.channelId);
}
}
}
Metadata callback objects
| Callback / object | Key fields | Description |
|---|---|---|
onMetadataSynced(_:) | event.channelId | Fired after the SDK finishes the initial metadata sync for an entered open channel. |
onMetadataChanged(_:) | event.changeInfo | Fired when metadata is updated or removed in the current channel. |
OpenChannelMetadataChangeInfo | channelId, key, value, operationType | One metadata change item. value is meaningful for updates, and operationType is .update or .remove. |
Get a metadata value
Call getMetadata(key:completion:) on an OpenChannel instance to retrieve the value for one metadata key.
- Swift
- Objective-C
import NexconnChatSDK
guard let channel = OpenChannel(channelId: "channelId") else {
return
}
channel.getMetadata(key: "seatStatus") { metadata, error in
guard let metadata, error == nil else {
print("Failed to get metadata: \(error?.localizedDescription ?? "unknown error")")
return
}
print("seatStatus = \(metadata["seatStatus"] ?? "")")
}
NCOpenChannel *channel = [[NCOpenChannel alloc] initWithChannelId:@"channelId"];
[channel getMetadataWithKey:@"seatStatus"
completion:^(NSDictionary<NSString *, NSString *> * _Nullable metadata, NCError * _Nullable error) {
if (error == nil) {
NSLog(@"seatStatus = %@", metadata[@"seatStatus"]);
}
}];
getMetadata(key:completion:)
| Parameter | Type | Description |
|---|---|---|
key | String / NSString * | The metadata key to query. Use the exact key name stored on the channel. |
completion | ([String: String]?, NCError?) -> Void | Returns a dictionary containing the matched key-value pair on success, or NCError on failure. |
Set metadata
Use OpenChannelSetMetadataParams to write one or more metadata entries. Set overwrite to true if you want to overwrite values written by other users. Set deleteWhenLeft to true if the metadata should be removed automatically when the current user exits the channel.
- Swift
- Objective-C
import NexconnChatSDK
guard let channel = OpenChannel(channelId: "channelId") else {
return
}
let params = OpenChannelSetMetadataParams(
metadata: ["seat1": "occupied"],
overwrite: false,
deleteWhenLeft: true
)
channel.setMetadata(params: params) { failureKeys, error in
if let error {
print("Failed keys: \(failureKeys ?? []), error: \(error.localizedDescription)")
return
}
print("Metadata set successfully.")
}
NCOpenChannel *channel = [[NCOpenChannel alloc] initWithChannelId:@"channelId"];
NCOpenChannelSetMetadataParams *params = [[NCOpenChannelSetMetadataParams alloc] initWithMetadata:@{@"seat1": @"occupied"}
overwrite:NO
deleteWhenLeft:YES];
[channel setMetadataWithParams:params
completion:^(NSArray<NSString *> * _Nullable failureKeys, NCError * _Nullable error) {
if (error == nil) {
NSLog(@"Metadata set successfully.");
} else {
NSLog(@"Failed keys: %@, error: %@", failureKeys, error);
}
}];
OpenChannelSetMetadataParams
| Property | Type | Description |
|---|---|---|
metadata | [String: String] / NSDictionary<NSString *, NSString *> * | Key-value pairs to set. Up to 10 keys per call. |
overwrite | Bool / BOOL | true to overwrite values created by other users. false preserves existing values from other users. |
deleteWhenLeft | Bool / BOOL | true to remove these keys automatically when the current user leaves or disconnects from the open channel. |
setMetadata(params:completion:)
| Callback value | Description |
|---|---|
failureKeys | Keys that failed in the current operation. nil on full success. |
error | nil on success, or NCError when the metadata write fails. |
Key names can contain alphanumeric characters plus +, =, -, and _, with a maximum length of 128 characters. Each value can be up to 4096 characters.
Delete metadata
Use OpenChannelDeleteMetadataParams to remove one or more keys from the open channel.
- Swift
- Objective-C
import NexconnChatSDK
guard let channel = OpenChannel(channelId: "channelId") else {
return
}
let params = OpenChannelDeleteMetadataParams(keys: ["seat1", "seat2"], isForce: false)
channel.deleteMetadata(params: params) { failureKeys, error in
if let error {
print("Failed keys: \(failureKeys ?? []), error: \(error.localizedDescription)")
return
}
print("Keys deleted.")
}
NCOpenChannel *channel = [[NCOpenChannel alloc] initWithChannelId:@"channelId"];
NCOpenChannelDeleteMetadataParams *params = [[NCOpenChannelDeleteMetadataParams alloc] initWithKeys:@[@"seat1", @"seat2"]
isForce:NO];
[channel deleteMetadataWithParams:params
completion:^(NSArray<NSString *> * _Nullable failureKeys, NCError * _Nullable error) {
if (error == nil) {
NSLog(@"Keys deleted.");
} else {
NSLog(@"Failed keys: %@, error: %@", failureKeys, error);
}
}];
OpenChannelDeleteMetadataParams
| Property | Type | Description |
|---|---|---|
keys | [String] / NSArray<NSString *> * | Metadata keys to delete. Up to 10 keys per call. |
isForce | Bool / BOOL | true to delete keys created by other users. false only removes keys the current user can delete normally. |
deleteMetadata(params:completion:)
| Callback value | Description |
|---|---|
failureKeys | Keys that failed to delete in the current operation. nil on full success. |
error | nil on success, or NCError when the delete request fails. |