Channel Management

Channel Members

When a user joins a channel, they are able to observe and chat with other users in that channel. They are also automatically considered a member of that channel. The Chat SDK provides the ability to view which users are currently in the channel as well as invite other users to join the channel.
Channel Type
Discoverable by
Message sending privileges
Moderation access
Community
All users and admins
Users and admins
All Moderation tools
Live
Only members and admins
Users and admins
All Moderation tools
Broadcast
All users and admins
Admins
Admin Moderation tools
Conversation
Only members
Users
No Moderation tools
Each channel is identified by a unique channelId, which is any string that uniquely identifies the channel and is immutable through its lifetime. When creating channels, you can specify your own channelId, or leave it to Amity's Chat SDK to automatically generate one for you.
There can be only one and one only channel with a given channelId. An error will be thrown when trying to generate two channels with the same channelId.
There are three ways of obtaining a channel:
  1. 1.
    create
  2. 2.
    join
  3. 3.
    get
createChannel: guarantees that the requested channel is a new channel, whereas joinChannel: will attempt to join an existing channel. If there is a requirement to create a new channel, use createChannel: then call joinChannel:. Lastly, calling getChannel: only gives you back the channel data, but it won't make you join the said channel.

Create Channel

TypeScriptSDK provides createChannel method to create a new channel. It supports creating of 3 types of channels Community, Live and Conversation. Each channel type has specific builder classes which helps you create that particular channel. Build your channel information first and then create the particular channel.
1
import { createChannel, createQuery, runQuery } from '@amityco/ts-sdk';
2
const query = createQuery(createChannel, {
3
displayName: "myChannel",
4
avatarFileId: "fileId",
5
type: "standard" | "live" | "conversation" | "broadcast",
6
tags: ["tag1"],
7
metadata: {
8
data: "anything"
9
}
10
});
11
12
runQuery(query, result => console.log(result));
Copied!
The above code creates a channel and notifies you through the callback.
3 channel types can be created through SDK i.e Community, Live and Conversation. Creation of Private and Standard type has been removed. Creation of Broadcast channel type is not supported through the SDK. But for query, channelCollection: method supports all channel types including Broadcast, Private and Standard.

Join Channel

1
import { joinChannel, createQuery, runQuery } from '@amityco/ts-sdk';
2
const query = createQuery(joinChannel, 'channelId');
3
4
runQuery(query, result => console.log(result));
Copied!
joinChannel: is an idempotent method. This means that it can be called multiple times throughout the lifecycle of the application, and you can expect this method to always return the same channel. Because of this, you can also use joinChannel: any time you need to fetch a channel, even if you know the user may already be in the channel.

Leave Channel

1
import { leaveChannel, createQuery, runQuery } from '@amityco/ts-sdk';
2
3
const query = createQuery(leaveChannel, 'channelId');
4
5
runQuery(query, result => console.log(result));
Copied!

Get Channel

In cases where you'd like to fetch a channel's data without joining, the getChannel: method can be used:
1
import { getChannel, createQuery, runQuery } from '@amityco/ts-sdk';
2
const query = createQuery(getChannel, 'channelId');
3
4
runQuery(query, result => console.log(result));
Copied!

Observe Channel

To display the channel, you may also use observerChannel
1
import { observeChannel } from "@amityco/ts-sdk";
2
export default function ChannelItem({ channelId }: { channelId: string }) {
3
const [channelResult, setChannelResult] = useState<Amity.QueryResult<Amity.Channel | undefined>>(null)
4
const { data: avatar, options] = chnnelResult;
5
const { loading, error } = options;
6
7
useEffect(
8
() => channel && observeUser(channelId, setChannelResult),
9
[channel],
10
);
11
return (
12
// Put your code here to display the post
13
);
14
}
Copied!

Channel Query

TypeScript SDK provides a way to query a list of channels using the queryChannels method. It returns a list of all the matching channels available.
1
import { queryChannels, createQuery, runQuery } from '@amityco/ts-sdk';
2
const query = createQuery(queryChannels, {});
3
4
runQuery(query, result => console.log(result));
Copied!

Channel Filtering

You can filter channels by various criteria such as membership and displayName.
1
import { queryChannels, createQuery, runQuery } from '@amityco/ts-sdk';
2
const query = createQuery(queryChannels, {
3
displayName: 'displayName',
4
membership: 'all' | 'member' | 'notMember',
5
sortBy: 'displayName' | 'firstCreated' | 'lastCreated'
6
});
7
8
runQuery(query, result => console.log(result));
Copied!

Delete Channel

Deleting a channel is not supported in SDK. Refer to this section for the instructions on how to delete a channel via API.

Metadata

Metadata is a general purpose data store that is automatically synchronized to all the channel members. It is meant as an elegant mechanism to store contextual information about a specific channel. The data can be any number of JSON key value pairs up to 100 kb.
Example use cases include:
  • Conversation title or cover photo
  • Global conversation settings
Metadata is implemented with last writer wins semantics: multiple mutations by independent users to the metadata object will result in a single stored value. No locking, merging, or other coordination is performed across multiple writes on the data.
To set metadata, call the following method:
1
import { updateChannel, createQuery, runQuery } from '@amityco/ts-sdk';
2
3
const yourMetadata = { foo: 'bar' }
4
5
const query = createQuery(updateChannel, 'channelId', {
6
metadata: yourMetadata
7
});
8
9
runQuery(query, result => console.log(result));
Copied!
The completion block will be triggered with the outcome of the request. The latest metadata of the channel is always exposed as part of the metadata property on the channel model.

Display Name

Every channel contains an optional displayName property. This property is mainly used to identify the channel in push notifications.
You can set a channel's displayName with the following method:
1
import { updateChannel, createQuery, runQuery } from '@amityco/ts-sdk';
2
const query = createQuery(updateChannel, 'channelId', {
3
displayName: 'newDisplayName'
4
});
5
6
runQuery(query, result => console.log(result));
Copied!
Last modified 1mo ago