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,
custom?: ObjectCustom,
description?: string,
status?: string,
type?: string
}
): Promise<Channel> -
updateChannel()
chat.updateChannel(
id: string,
{
name?: string,
custom?: ObjectCustom,
description?: string,
status?: string,
type?: string
}
): Promise<Channel>
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 | ObjectCustom | 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 | string | 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 |
---|---|
Promise<Channel> | Object returning the updated channel metadata. |
Errors
Whenever the channel ID is required, and you try to edit a channel without providing its ID, you will receive the ID is required
error. If the channel you're trying to edit doesn't exist, you'll receive the Channel with this ID does not exist
error.
Basic usage
Update the description of the support
channel.
-
update()
// reference the "channel" object
const channel = await chat.getChannel("support")
// invoke the "update()" method on the "channel" object
await channel.update(
{
description: "Channel for CRM tickets"
}
) -
updateChannel()
// reference the "chat" object and invoke the "updateChannel()" method
const channel = await chat.updateChannel(
"support",
{
description: "Channel for CRM tickets"
}
)
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.streamUpdatesOn()
checks updates on aChannel
object list.
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: (channel: Channel) => unknown
): () => void -
streamUpdatesOn()
static Channel.streamUpdatesOn(
channels: Channel[],
callback: (channels: Channel[]) => unknown
): () => void
Input
Parameter | Type | Required in streamUpdates() | Required in streamUpdatesOn() | Default | Description |
---|---|---|---|---|---|
channels | Channel[] | No | Yes | n/a | Array of Channel objects for which you want to get updates. |
callback | n/a | Yes | Yes | n/a | Callback function passed as a parameter to both methods. It defines the custom behavior to be executed when detecting channel metadata changes. |
→ channel | Channel | Yes | No | n/a | Returned Channel object with the updated data. |
→ channels | Channel[] | No | Yes | n/a | Returned array of Channel objects with the updated data. |
Output
Type | Description |
---|---|
() => void | Function you can call to disconnect (unsubscribe) from the channel and stop receiving objects events. |
Errors
Whenever a list of Channel
objects is required as a parameter, and you try to get updates on channels without specifying their list, you will receive the Cannot stream channel updates on an empty list
error.
Basic usage
-
streamUpdates()
Get updates on the
support
channel.const channel = await chat.getChannel("support")
channel.streamUpdates((channel) => {
console.log("Updated channel: ", channel)
}) -
streamUpdatesOn()
Get updates on the
support
andincident-management
channels.const supportChannel = await chat.getChannel("support")
const incidentChannel = await chat.getChannel("incident-management")
Channel.streamUpdatesOn([supportChannel, incidentChannel], (channels) => {
console.log("Updated channels: ", channels)
})
Other examples
-
streamUpdates()
Stop listening to updates on the
support
channel.const channel = await chat.getChannel("support")
const stopUpdates = channel.streamUpdates(/* handle update callback */)
// after some time...
stopUpdates() -
streamUpdatesOn()
Stop listening to updates on the
support
andincident-management
channels.const supportChannel = await chat.getChannel("support")
const incidentChannel = await chat.getChannel("incident-management")
const stopUpdates = Channel.streamUpdatesOn([supportChannel, incidentChannel], /* handle update callback */)
// after some time...
stopUpdates()