# Channel

## Channel <a href="#undefined" id="undefined"></a>

In Game Chat, the channel is a virtual space where users can communicate in a group. Through channels, you can share information suitable for a specific subject or purpose, enhance teamwork, and systematize communication. Channel is a useful tool to increase business efficiency, and centralize and manage communication in a specific group. The following describes the channel functions of Game Chat.

### Main functions of channel <a href="#undefined" id="undefined"></a>

1. **Group communication**: you can create a channel and communicate with members in a specific group. It is suitable for diverse forms of groups, such as project teams, divisions, or clubs.
2. **Message and file sharing**: you can easily share various forms of files such as text messages, images, videos, and documents in the channel.
3. **Real-time update**: all activities in the channel are updated in real time so that every participant can obtain the latest information.
4. **Admin control**: the creator or admin of the channel has the right to change channel settings or add and remove users.
5. **Calls and video conferencing function**: some channels provide voice calls or video conferencing for effective communication between members.
6. **Notification settings**: you can set notifications by channel so as not to miss important messages.
7. **Search features**: you can easily search dialogs or files in the channel, so you can quickly find the information you need.

### Manage channel <a href="#undefined" id="undefined"></a>

* **Create channel**: you can create a new channel suitable for your purpose and set information such as channel name, descriptions, and members.
* **Channel invitation**: the admin of the channel can invite other users to the channel. The user who is invited can accept or decline the invitation.
* **Manage members**: the admin can set the permissions of the channel members or remove members from the channel.

### Security <a href="#undefined" id="undefined"></a>

* **Data security**: all data in the channel are encrypted and transmitted, and are stored safely in the server.
* **Personal information protection**: the information shared in the channel can be accessed only among channel members and is protected against leak to the outside.

When you use the channel function of Game Chat, you can enjoy easy communication within the organization and among individuals, and manage information effectively. These features are very useful when you manage large-scale organizations or various projects.

### Create channel <a href="#undefined" id="undefined"></a>

All conversations require you to create channels and participate in the channel to chat normally. The following guides you on how to create and subscribe to channels.

```csharp
await nc.createChannel(new NBaseSDK.Channel
{
    name = "New Channel",
    type =[TYPE],    // PUBLIC or PRIVATE
    customField = [CustomField]
});
```

<table><thead><tr><th width="163">ID</th><th width="103">Type</th><th width="368">Description</th><th>Required</th></tr></thead><tbody><tr><td>NAME</td><td>string</td><td>Channel name</td><td>O</td></tr><tr><td>TYPE</td><td>string</td><td>Channel type (PUBLIC or PRIVATE)</td><td>O</td></tr><tr><td>UniqueID</td><td>string</td><td>Unique ID</td><td>X</td></tr><tr><td>push</td><td>boolean</td><td>Push notification status</td><td>X</td></tr><tr><td>linkUrl</td><td>string</td><td>Link status</td><td>X</td></tr><tr><td>imageUrl</td><td>string</td><td>Link status</td><td>X</td></tr><tr><td>integrationId</td><td>string</td><td>Integration feature (translation, voice, etc.)</td><td>X</td></tr><tr><td>disabled</td><td>string</td><td>Channel use status</td><td>X</td></tr><tr><td>members</td><td>array</td><td>ID allowed to join if PRIVATE</td><td>X</td></tr><tr><td>CustomField</td><td>string</td><td>User-defined field, can be put as a JSON String for more versatility</td><td>X</td></tr></tbody></table>

> Note
>
> * For security, create a channel through the server instead of creating a channel on the client side.

### Subscribe to channel <a href="#undefined" id="undefined"></a>

Subscribe to the desired channel (join the room). Once you join a channel, you will join automatically when you access it again until you unsubscribe from it.

```csharp
Hashtable option = new Hashtable
{
    { "language", "en" }    // In addition to the options required for automatic translation, various other options can also be added.
};
await nc.subscribe([CHANNEL_ID], option);
```

### Unsubscribe from channels <a href="#undefined" id="undefined"></a>

Unsubscribe from the channel. You will no longer receive messages from this channel.

```csharp
await nc.unsubscribe([CHANNEL_ID]);
```

### Participant list <a href="#undefined" id="undefined"></a>

You can import the participant list (for a specific channel).

```csharp
Hashtable filter = new Hashtable
{
    { "channel_id", channelId }
};
Hashtable sort = new Hashtable
{
    { "created_at", -1 }
};
Hashtable option = new Hashtable
{
    { "offset", 0 },
    { "per_page", 10 }
};
var subscriptions = await nc.getSubscriptions(filter, sort, option);
foreach (var subscription in subscriptions.edges)
{
    string id = subscription.node.id.ToString();
    Console.WriteLine("[CloudChatSample] id={0}", id);
}
```

* Parameters

<table><thead><tr><th width="121">ID</th><th width="100">Type</th><th>Description</th></tr></thead><tbody><tr><td>filter</td><td>object</td><td>Search is available for all fields of a query through filtering</td></tr><tr><td>sort</td><td>object</td><td>Define the filter for the fields you want to sort (ascending order "1", descending order "-1")</td></tr><tr><td>option</td><td>object</td><td>See the following when there are options</td></tr></tbody></table>

* Filter

| ID          | Type    | Description               |
| ----------- | ------- | ------------------------- |
| project\_id | String  | Project ID                |
| channel\_id | String  | Channel ID                |
| user\_id    | String  | User ID                   |
| language    | String  | Language                  |
| uniquekey   | String  | Unique Key                |
| online      | Boolean | Online Status             |
| push        | Boolean | Push Notification Enabled |
| created\_at | String  | Creation Date             |
| updated\_at | String  | Update Date               |

* Sort

<table><thead><tr><th width="193">ID</th><th width="166">Type</th><th>Description</th></tr></thead><tbody><tr><td>created_at</td><td>number</td><td>Sort Date (Ascending '1', Descending '-1')</td></tr></tbody></table>

* Options

<table><thead><tr><th width="198">ID</th><th width="163">Type</th><th>Description</th></tr></thead><tbody><tr><td>offset</td><td>number</td><td>Start offset</td></tr><tr><td>per_page</td><td>number</td><td>The number of returns (up to 100)</td></tr></tbody></table>

#### **Advanced**

To import only a list of online users accessing a specific channel, add "online" as "true" to the filter.

```csharp
Hashtable filter = new Hashtable
{
    { "channel_id", [CHANNEL_ID] },
    { "online" , true}
};
Hashtable sort = new Hashtable
{
    { "created_at", -1 }
};
Hashtable option = new Hashtable
{
    { "offset", 0 },
    { "per_page", 10 }
};

var subscriptions = await nc.getSubscriptions(filter, sort, option);

```

#### **Subscribe to channel**

* Subscription Data Class

<table><thead><tr><th width="207">ID</th><th width="109">Type</th><th>Description</th></tr></thead><tbody><tr><td>id</td><td>string</td><td>Unique ID</td></tr><tr><td>channel_id</td><td>string</td><td>Channel ID</td></tr><tr><td>user_id</td><td>string</td><td>Unique ID of the user</td></tr><tr><td>created_at</td><td>string</td><td>Creation date</td></tr><tr><td>online</td><td>boolean</td><td>Whether the user is online</td></tr><tr><td>push</td><td>boolean</td><td>Whether the user allows push notifications</td></tr><tr><td>language</td><td>string</td><td>Language</td></tr><tr><td>channel</td><td>string</td><td>Channel information</td></tr><tr><td>mark.user_id</td><td>string</td><td>User who sent the last message</td></tr><tr><td>mark.message_id</td><td>string</td><td>ID of the last message</td></tr><tr><td>mark.sort_id</td><td>string</td><td>Last message sort ID</td></tr><tr><td>mark.unread</td><td>string</td><td>Number of unread messages since the last message</td></tr></tbody></table>

### Channel information <a href="#undefined" id="undefined"></a>

* ChannelData data class

| ID         | Type    | Description              |
| ---------- | ------- | ------------------------ |
| totalCount | Int     | Total Number of Channels |
| channels   | Channel | Channel Data List        |

* Channel Data Class

<table><thead><tr><th width="174">ID</th><th width="113">Type</th><th>Description</th></tr></thead><tbody><tr><td>id</td><td>string</td><td>Channel ID (unique)</td></tr><tr><td>project_id</td><td>string</td><td>Project ID</td></tr><tr><td>unique_id</td><td>string</td><td>Channel ID that can be set by the developer (unique)</td></tr><tr><td>name</td><td>string</td><td>Channel name</td></tr><tr><td>user_id</td><td>string</td><td>User ID (that created the channel)</td></tr><tr><td>unique_id</td><td>string</td><td>Unique ID of channel</td></tr><tr><td>default_lang</td><td>string</td><td>Default language</td></tr><tr><td>lang</td><td>string</td><td>The language of the current user</td></tr><tr><td>members</td><td>string</td><td>If private, the list of participating users</td></tr><tr><td>last_message</td><td>array</td><td>See the last message information [MessageType]</td></tr><tr><td>push</td><td>boolean</td><td>Whether push messages are supported (if the channel is private)</td></tr><tr><td>state</td><td>boolean</td><td>Channel status</td></tr><tr><td>customField</td><td>string</td><td>User-defined data</td></tr><tr><td>created_at</td><td>string</td><td>Creation date</td></tr><tr><td>updated_at</td><td>string</td><td>Renewal date</td></tr></tbody></table>

#### **Import channel data**

Use the following code to import the channel data of a project in the form of a list.

```csharp
Hashtable filter = new Hashtable
{
    { "state", true }
};
Hashtable sort = new Hashtable
{
    { "created_at", -1 }
};
Hashtable option = new Hashtable
{
    { "offset", 0 },
    { "per_page", 10 }
};
var channels = await nc.getChannels(filter,sort,option);
foreach (var channel in channels.edges)
{
    string id = channel.node.id.ToString();
    Console.WriteLine("[CloudChatSample] id={0}", id);
}
```

* Parameters

<table><thead><tr><th width="113">ID</th><th width="106">Type</th><th width="419">Description</th><th>Required</th></tr></thead><tbody><tr><td>filter</td><td>object</td><td>Search is available for all fields of a query through filtering</td><td>O</td></tr><tr><td>sort</td><td>object</td><td>Define a filter for the fields you want to sort</td><td>X</td></tr><tr><td>option</td><td>object</td><td>See the following when there are options</td><td>X</td></tr></tbody></table>

* Filter

<table><thead><tr><th width="229">ID</th><th width="215">Type</th><th>Description</th></tr></thead><tbody><tr><td>id</td><td>String</td><td>Channel ID</td></tr><tr><td>project_id</td><td>String</td><td>Project ID</td></tr><tr><td>name</td><td>String</td><td>Channel Name</td></tr><tr><td>user_id</td><td>String</td><td>User ID</td></tr><tr><td>unique_id</td><td>String</td><td>Unique ID</td></tr><tr><td>type</td><td>String</td><td>Channel Type</td></tr><tr><td>push</td><td>Boolean</td><td>Push Notification Enabled</td></tr><tr><td>disabled</td><td>Boolean</td><td>Active Status</td></tr><tr><td>customField</td><td>String</td><td>Custom Fields</td></tr><tr><td>link_url</td><td>String</td><td>Link URL</td></tr><tr><td>image_url</td><td>String</td><td>Image URL</td></tr><tr><td>subscribed</td><td>Boolean</td><td>Subscription Status</td></tr><tr><td>unread</td><td>Int</td><td>Number of Unread Messages</td></tr><tr><td>created_at</td><td>String</td><td>Creation Date</td></tr><tr><td>updated_at</td><td>String</td><td>Update Date</td></tr></tbody></table>

* Sort

<table><thead><tr><th width="193">ID</th><th width="168">Type</th><th>Description</th></tr></thead><tbody><tr><td>created_at</td><td>number</td><td>ort Date (Ascending '1', Descending '-1')</td></tr></tbody></table>

<br>

* Options

<table><thead><tr><th width="190">ID</th><th width="161">Type</th><th>Description</th></tr></thead><tbody><tr><td>offset</td><td>number</td><td>Start offset</td></tr><tr><td>per_page</td><td>number</td><td>The number of returns (up to 100)</td></tr></tbody></table>

### Individual channel <a href="#undefined" id="undefined"></a>

* You can get information about individual channels.

```csharp
Channel channel = await nc.getChannel(id);
```

### Invite user to channel <a href="#undefined" id="undefined"></a>

Invite a user to participate when the channel is private.

```csharp
await nc.addUsers(newChannelId, new string[] { "ID", "ID" });
```

### Delete user from channel <a href="#undefined" id="undefined"></a>

Delete a user who is a participant when the channel is private.

```csharp
await nc.removeUsers(channelId, new string[] { "ID", "ID" });
```

### Block user from channel <a href="#undefined" id="undefined"></a>

Block a user within a channel. It can only be used by users who have permission to create a channel, or admin with full access.

```csharp
Hashtable option = new Hashtable
{
    { "timeout", [End Time (seconds)] },
    { "reason", [Reason for Blocking] }
};
await nc.banUser(channelId, userId, options);
```

* Options Data Class

<table><thead><tr><th width="158">ID</th><th width="176">Type</th><th>Description</th></tr></thead><tbody><tr><td>timeout</td><td>string</td><td>Block time (seconds)</td></tr><tr><td>reason</td><td>string</td><td>Cause of blocking</td></tr></tbody></table>

### Unblock user from channel <a href="#undefined" id="undefined"></a>

Unblock the blocked person within the channel. It can only be used by users who have permission to create a channel, or admin with full access.

```csharp
await nc.unbanUser(channelId, userId);
```

### Delete channel <a href="#undefined" id="undefined"></a>

Delete the corresponding channel (you can delete 1 channel or multiple channels).

```csharp
Channel channel = await nc.deleteChannel([CHANNEL_ID]);
```

### Edit channel <a href="#undefined" id="undefined"></a>

Update the channel information.

```csharp
Channel channel = await nc.updateChannel([CHANNEL_ID],new NBaseSDK.Channel
{
    name = "Update Channel",
    type =[TYPE],    // PUBLIC or PRIVATE
    customField = [CustomField]
});
```
