Manage channel updates
Update channel details and receive events whenever someone updates them.
Requires App Context
To store data about channels, you must enable App Context for your app's keyset in the Admin Portal.
Update channel details
You can edit the metadata of an existing channel with update()
and updateChannel()
.
Both of them give the same output. The only difference is that you call a given method either on the Chat
(updateChannel()
) or the Channel
(update()
) object. Depending on the object, these methods take a different set of input parameters - you either have to specify the channel ID you want to update or not because it's already known.
Method signature
These methods take the following parameters:
-
update()
channel.update(
name: String? = nil,
custom: [String: JSONCodableScalar]? = nil,
description: String? = nil,
status: String? = nil,
type: ChannelType? = nil,
completion: ((Swift.Result<ChannelImpl, Error>) -> Void)? = nil
) -
updateChannel()
chat.updateChannel(
id: String,
name: String? = nil,
custom: [String: JSONCodableScalar]? = nil,
description: String? = nil,
status: String? = nil,
type: ChannelType? = nil,
completion: ((Swift.Result<ChannelImpl, Error>) -> Void)? = nil
)
Input
Parameter | Type | Required in update() | Required in updateChannel() | Default | Description |
---|---|---|---|---|---|
id | String | No | Yes | n/a | Unique channel identifier. |
name | String | No | No | n/a | Display name for the channel. |
custom | [String: JSONCodableScalar] | No | No | n/a | JSON providing custom data about the channel. Values must be scalar only; arrays or objects are not supported. App Context filtering language doesn’t support filtering by custom properties. |
description | String | No | No | n/a | Additional details about the channel. |
status | String | No | No | n/a | Tag that categorizes a channel by its state, like archived . |
type | ChannelType | No | No | n/a | Tag that categorizes a channel by its function, like offtopic . |
API limits
To learn about the maximum length of parameters used to set channel metadata, refer to REST API docs.
Output
Type | Description |
---|---|
ChannelImpl | Object containing the updated channel with its metadata. |
Basic usage
Update the description of the support
channel.
-
update()
show all 28 lines/// Assuming you have a "chat" instance available
/// Fetch metadata of the "support" channel
chat?.getChannel(
channelId: "support"
) {
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
/// Update the description of the "support" channel
channel.update(
description: "This is the updated description for the support channel."
) { result in
switch result { -
updateChannel()
/// Assuming you have a "chat" instance available
/// Update the description of the "support" channel using `updateChannel()`
chat.updateChannel(
id: "support",
description: "This is the updated description for the support channel."
) { result in
switch result {
case let .success(updatedChannel):
print("Successfully updated the channel description to: \(updatedChannel.description ?? "No Description")")
case let .failure(error):
print("Failed to update the channel description: \(error)")
}
}
Get channel updates
You can receive updates when specific Channel
object(s) are edited or removed on other clients using the following methods:
streamUpdates()
checks updates on a singleChannel
object and it's tied to an instance of theChannel
class.streamUpdatesOn()
checks updates on aChannel
object list and it's tied to theChannel
class.
Both methods accept a callback function as an argument. The Chat SDK invokes this callback whenever someone adds, changes, or removes channel metadata.
Underneath, these methods subscribe the current user to a channel and add an objects event listener to receive all objects
events of type channel
. These methods also return the unsubscribe
function you can invoke to stop receiving objects
events and unsubscribe from the channel.
Method signature
These methods take the following parameters:
-
streamUpdates()
channel.streamUpdates(
callback: @escaping ((ChannelImpl)?) -> Void
) -> AutoCloseable -
streamUpdatesOn()
(static)Channel.streamUpdatesOn(
channels: [ChannelImpl],
callback: @escaping (([ChannelImpl]) -> Void)
) -> AutoCloseable
Input
Parameter | Type | Required in streamUpdates() | Required in streamUpdatesOn() | Default | Description |
---|---|---|---|---|---|
channels | [ChannelImpl] | No | Yes | n/a | A collection of ChannelImpl objects for which you want to get updates. |
callback | @escaping ((ChannelImpl)?) -> Void | Yes | No | n/a | Function that takes a single ChannelImpl object. It defines the custom behavior to be executed when detecting channel changes. |
callback | @escaping (([ChannelImpl]) -> Void) | No | Yes | n/a | Function that takes a set of ChannelImpl objects. It defines the custom behavior to be executed when detecting channel changes. |
Output
Type | Description |
---|---|
AutoCloseable | Interface that lets you stop receiving channel-related updates (objects events) by invoking the close() method. |
Basic usage
-
streamUpdates()
Get updates on the
support
channel.
show all 27 lines/// Assuming you have a "chat" instance available
/// Fetch metadata of the "support" channel
chat?.getChannel(
channelId: "support"
) {
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
/// Stream updates on the "support" channel
let autoCloseable = channel.streamUpdates { updatedChannel in
if let updatedChannel = updatedChannel {
debugPrint("Received update for channel with ID: \(updatedChannel.id)")
} else { -
streamUpdatesOn()
Get updates on the
support
andincident-management
channels.
show all 27 lines/// Assuming you have a "chat" instance available
/// Fetch metadata of the "support" channel
chat?.getChannel(
channelId: "support"
) {
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Stream updates on the "support" channel using static method
let autoCloseable = ChannelImpl.streamUpdatesOn(
channels: [channel]
) { updatedChannels in
for updatedChannel in updatedChannels {
Other examples
-
streamUpdates()
Stop listening to updates on the
support
channel.
show all 33 linesimport Foundation /// Required for DispatchQueue
/// Assuming you have a "chat" instance available
/// Fetch metadata of the "support" channel
chat?.getChannel(
channelId: "support"
) {
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Stream updates on the "support" channel
let autoCloseable = channel.streamUpdates { updatedChannel in
if let updatedChannel = updatedChannel { -
streamUpdatesOn()
Stop listening to updates on the
support
andincident-management
channels.
show all 29 lines/// Assuming you have a "chat" instance available
/// Fetch metadata of the "support" channel
chat?.getChannel(
channelId: "support"
) {
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
/// Stream updates on the "support" channel using static method
let autoCloseable = ChannelImpl.streamUpdatesOn(channels: [channel]) { updatedChannels in
for updatedChannel in updatedChannels {
debugPrint("Received update for channel with ID: \(updatedChannel.id)")
}