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()
(on theChannel
object)channel.update(
name: String? = nil,
custom: [String: JSONCodableScalar]? = nil,
description: String? = nil,
status: String? = nil,
type: ChannelType? = nil
) async throws -> ChannelImpl -
updateChannel()
(on theChat
object)chat.updateChannel(
name: String? = nil,
custom: [String: JSONCodableScalar]? = nil,
description: String? = nil,
status: String? = nil,
type: ChannelType? = nil
) async throws -> ChannelImpl
Input
Parameter | Required in update() | Required in updateChannel() | Description |
---|---|---|---|
id Type: String Default: n/a | No | Yes | Unique channel identifier. |
name Type: String Default: n/a | No | No | Display name for the channel. |
custom Type: [String: JSONCodableScalar] Default: n/a | No | No | 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 Type: String Default: n/a | No | No | Additional details about the channel. |
status Type: String Default: n/a | No | No | Tag that categorizes a channel by its state, like archived . |
type Type: ChannelType Default: n/a | No | No | 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
Parameter | Description |
---|---|
ChannelImpl | Object containing the updated channel with its metadata. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Update the description of the support
channel.
update()
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
let updatedChannel = try await channel.update(name: "This is the updated description for the support channel")
debugPrint("Updated channel: \(channel)")
} else {
debugPrint("Stopped typing indicator on the 'support' channel")
}
}
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 return an asynchronous stream which produces a new value 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() -> AsyncStream<ChannelImpl?>
-
streamUpdatesOn()
(static)ChannelImpl.streamUpdatesOn(
channels: [ChannelImpl]
) -> AsyncStream<[ChannelImpl]>
Input
Parameter | Required in streamUpdates() | Required in streamUpdatesOn() | Description |
---|---|---|---|
channels Type: [ChannelImpl] Default: n/a | No | Yes | A collection of ChannelImpl objects for which you want to get updates. |
Output
An asynchronous stream that produces updates when the underlying channel(s) change.
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
-
streamUpdates()
Get updates on the
support
channel.- AsyncStream
- Closure
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
for await updatedChannel in channel.streamUpdates() {
debugPrint("Updated channel: \(String(describing: updatedChannel))")
}
} else {
debugPrint("Channel 'support' not found")
}
}// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
// to receive updates. If the "AutoCloseable" is deallocated, the stream will be cancelled,
// and no further items will be produced. You can also stop receiving updates manually
// by calling the "close()" method on the "AutoCloseable" object.
// Assuming you have a reference of type "ChannelImpl" named "channel"
autoCloseable = channel.streamUpdates { updatedChannel in
if let updatedChannel = updatedChannel {
debugPrint("Received update for channel with ID: \(updatedChannel.id)")
} else {
debugPrint("No updates received")
}
} -
streamUpdatesOn()
Get updates on the
support
andincident-management
channels.- AsyncStream
- Closure
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
for await updatedChannel in ChannelImpl.streamUpdatesOn(channels: [channel]) {
debugPrint("Updated channel: \(String(describing: updatedChannel))")
}
} else {
debugPrint("Channel 'support' not found")
}
}// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
// to receive updates. If the "AutoCloseable" is deallocated, the stream will be cancelled,
// and no further items will be produced. You can also stop receiving updates manually
// by calling the "close()" method on the "AutoCloseable" object.
// Assuming you have an array of "ChannelImpl" objects named "channels"
autoCloseable = ChannelImpl.streamUpdatesOn(channels: channels) { updatedChannels in
for updatedChannel in updatedChannels {
debugPrint("Received update for channel with ID: \(updatedChannel.id)")
}
}