# Clients API

このAPIドキュメントは、MQTTクライアントの管理に関するさまざまな操作についての情報を提供します。クライアント情報の閲覧、クライアントのトピックへのサブスクライブおよびサブスクライブ解除、クライアントのキックなどが含まれます。

## すべてのクライアントの表示

### URI

GET /clients

クラスター内のすべてのクライアント情報を返し、ページネーションに対応しています。

**クエリパラメータ:**

| パラメータ         | 型       | 説明                                                        |
| ----------------- | -------- | ----------------------------------------------------------- |
| _page             | Integer  | ページ番号                                                  |
| _limit            | Integer  | 1ページあたりのデータ件数                                   |
| clientid          | String   | クライアント識別子                                          |
| username          | String   | クライアントのユーザー名                                    |
| zone              | String   | クライアントの設定グループ名                                |
| ip_address        | String   | クライアントのIPアドレス                                    |
| conn_state        | Enum     | クライアントの現在の接続状態。`connected`、`idle`、`disconnected`のいずれか |
| clean_start       | Bool     | クライアントがクリーンセッションを使用しているかどうか    |
| proto_name        | Enum     | クライアントのプロトコル名。`MQTT`、`CoAP`、`MQTT-SN`のいずれか |
| proto_ver         | Integer  | クライアントのプロトコルバージョン                          |
| _gte_created_at   | Integer  | クライアントセッション作成時刻（以上）                      |
| _lte_created_at   | Integer  | クライアントセッション作成時刻（以下）                      |
| _gte_connected_at | Integer  | クライアント接続作成時刻（以上）                            |
| _lte_connected_at | Integer  | クライアント接続作成時刻（以下）                            |
<!-- | _like_clientid    | String  | クライアント識別子の部分一致                                |
| _like_username    | String  | クライアントユーザー名の部分一致                            | -->

### リクエストメッセージ

なし

### レスポンスメッセージ

| 名前                     | 型               | 説明                                                        |
| ------------------------ | ---------------- | ----------------------------------------------------------- |
| data                     | オブジェクト配列 | すべてのクライアントの情報                                  |
| data[].node              | String           | クライアントが接続しているノード                            |
| data[].clientid          | String           | クライアント識別子                                          |
| data[].username          | String           | クライアント接続時に使用したユーザー名                      |
| data[].proto_name        | String           | クライアントのプロトコル名                                  |
| data[].proto_ver         | Integer          | クライアントのプロトコルバージョン                          |
| data[].ip_address        | String           | クライアントのIPアドレス                                    |
| data[].port              | Integer          | クライアントのポート番号                                    |
| data[].is_bridge         | Boolean          | クライアントがブリッジ経由で接続されているかどうか          |
| data[].connected_at      | String           | クライアント接続時刻（"YYYY-MM-DD HH:mm:ss"形式）           |
| data[].disconnected_at   | String           | クライアント切断時刻（"YYYY-MM-DD HH:mm:ss"形式）。`connected`が`false`の場合のみ返される |
| data[].connected         | Boolean          | クライアントが接続状態かどうか                              |
| data[].zone              | String           | クライアントが使用している設定グループ                      |
| data[].keepalive         | Integer          | キープアライブ時間（秒）                                    |
| data[].clean_start       | Boolean          | クライアントがクリーンセッションを使用しているかどうか    |
| data[].expiry_interval   | Integer          | セッションの有効期限間隔（秒）                             |
| data[].created_at        | String           | セッション作成時刻（"YYYY-MM-DD HH:mm:ss"形式）             |
| data[].subscriptions_cnt | Integer          | クライアントが確立しているサブスクリプション数              |
| data[].max_subscriptions | Integer          | クライアントに許可されている最大サブスクリプション数        |
| data[].inflight          | Integer          | 現在のインフライトキューの長さ                              |
| data[].max_inflight      | Integer          | インフライトキューの最大長さ                                |
| data[].mqueue_len        | Integer          | 現在のメッセージキューの長さ                                |
| data[].max_mqueue        | Integer          | メッセージキューの最大長さ                                  |
| data[].mqueue_dropped    | Integer          | キュー長超過により破棄されたメッセージ数                    |
| data[].awaiting_rel      | Integer          | 未確認のPUBRECメッセージ数                                  |
| data[].max_awaiting_rel  | Integer          | 許可されている未確認PUBRECメッセージの最大数                |
| data[].recv_oct          | Integer          | EMQXブローカーが受信したバイト数                            |
| data[].recv_cnt          | Integer          | 受信したTCPメッセージ数                                     |
| data[].recv_pkt          | Integer          | 受信したMQTTメッセージ数                                    |
| data[].recv_msg          | Integer          | 受信したPUBLISHメッセージ数                                 |
| data[].send_oct          | Integer          | 送信したバイト数                                            |
| data[].send_cnt          | Integer          | 送信したTCPメッセージ数                                     |
| data[].send_pkt          | Integer          | 送信したMQTTメッセージ数                                    |
| data[].send_msg          | Integer          | 送信したPUBLISHメッセージ数                                 |
| data[].mailbox_len       | Integer          | プロセスのメールボックスサイズ                              |
| data[].heap_size         | Integer          | プロセスのヒープサイズ（バイト）                            |
| data[].reductions        | Integer          | Erlangのリダクションカウント                                |
| meta                     | オブジェクト     | ページネーション情報                                        |
| meta.page                | Integer          | ページ番号                                                  |
| meta.limit               | Integer          | 1ページあたりのデータ件数                                   |
| meta.count               | Integer          | データの総件数                                              |

### リクエスト例

```bash
curl -u app_id:app_secret -X GET {api}/clients?_page=1&_limit=50
```

### レスポンス例

```json
{
  "meta": {
    "page": 1,
    "limit": 50,
    "hasnext": false,
    "count": 1
  },
  "data": [
    {
        "peersni": "qe92461d.dev-ala.cn-hangzhou.mqttce.com",
        "reductions": 10276,
        "expiry_interval": 0,
        "clean_start": true,
        "send_msg.dropped.expired": 0,
        "recv_msg.qos0": 3,
        "mqueue_dropped": 0,
        "recv_cnt": 9,
        "send_cnt": 3,
        "keepalive": 60,
        "recv_oct": 257,
        "heap_size": 987,
        "recv_pkt": 6,
        "recv_msg.dropped.await_pubrel_timeout": 0,
        "proto_ver": 5,
        "inflight_max": 32,
        "send_msg.dropped": 0,
        "created_at": "2023-09-15T09:36:20.871+00:00",
        "awaiting_rel_max": 100,
        "inflight_cnt": 0,
        "ip_address": "115.236.21.86",
        "mqueue_len": 0,
        "send_msg.qos2": 0,
        "send_pkt": 3,
        "subscriptions_cnt": 0,
        "send_msg.dropped.too_large": 0,
        "recv_msg": 3,
        "send_msg.dropped.queue_full": 0,
        "send_msg": 0,
        "node": "emqxsl-dev@10.66.128.31",
        "awaiting_rel_cnt": 0,
        "listener": "tcp:default",
        "connected": true,
        "username": "aip_user2",
        "recv_msg.qos1": 0,
        "proto_name": "MQTT",
        "port": 13312,
        "send_msg.qos1": 0,
        "is_persistent": false,
        "enable_authn": true,
        "mailbox_len": 0,
        "subscriptions_max": 10,
        "recv_msg.qos2": 0,
        "connected_at": "2023-09-15T09:36:20.871+00:00",
        "tenant_id_from": "peersni",
        "is_bridge": false,
        "clientid": "mqttx_07cb8109",
        "send_oct": 25,
        "send_msg.qos0": 0,
        "mqueue_max": 1000,
        "cn": null,
        "recv_msg.dropped": 0,
        "dn": null
    }
  ]
}
```

## 特定クライアントの情報表示

### URI

GET /clients/{clientid}

特定のクライアントの情報を返します。

**パラメータ:**

| パラメータ | 型     | 説明       |
| ---------- | ------ | ---------- |
| clientid   | String | クライアントID |

### リクエストメッセージ

なし

### レスポンスメッセージ

| 名前 | 型               | 説明                   |
| ---- | ---------------- | ---------------------- |
| data | オブジェクト配列 | クライアントの情報     |

### リクエスト例

特定クライアントの問い合わせ

```bash
curl -u app_id:app_ssecret -X GET {api}/clients/client_1
```

### レスポンス例

```json
// HTTPステータスコード
200
// HTTPレスポンスボディ
{
    "peersni": "qe92461d.dev-ala.cn-hangzhou.mqttce.com",
    "reductions": 21041,
    "expiry_interval": 0,
    "clean_start": true,
    "send_msg.dropped.expired": 0,
    "recv_msg.qos0": 5,
    "mqueue_dropped": 0,
    "recv_cnt": 18,
    "send_cnt": 10,
    "keepalive": 60,
    "recv_oct": 361,
    "heap_size": 987,
    "recv_pkt": 15,
    "recv_msg.dropped.await_pubrel_timeout": 0,
    "proto_ver": 5,
    "inflight_max": 32,
    "send_msg.dropped": 0,
    "created_at": "2023-09-15T09:36:20.871+00:00",
    "awaiting_rel_max": 100,
    "inflight_cnt": 0,
    "ip_address": "115.236.21.86",
    "mqueue_len": 0,
    "send_msg.qos2": 0,
    "send_pkt": 10,
    "subscriptions_cnt": 0,
    "send_msg.dropped.too_large": 0,
    "recv_msg": 5,
    "send_msg.dropped.queue_full": 0,
    "send_msg": 0,
    "node": "emqxsl-dev@10.66.128.31",
    "awaiting_rel_cnt": 0,
    "listener": "tcp:default",
    "connected": true,
    "username": "aip_user2",
    "recv_msg.qos1": 0,
    "proto_name": "MQTT",
    "port": 13312,
    "send_msg.qos1": 0,
    "is_persistent": false,
    "enable_authn": true,
    "mailbox_len": 0,
    "subscriptions_max": 10,
    "recv_msg.qos2": 0,
    "connected_at": "2023-09-15T09:36:20.871+00:00",
    "tenant_id_from": "peersni",
    "is_bridge": false,
    "clientid": "mqttx_07cb8109",
    "send_oct": 39,
    "send_msg.qos0": 0,
    "mqueue_max": 1000,
    "cn": null,
    "recv_msg.dropped": 0,
    "dn": null
}
```

## クライアントのキック

### URI

DELETE /clients/{clientid}

特定のクライアントを強制切断します。クライアントをキックすると、接続とセッションの両方が終了します。

### リクエストメッセージ

なし

### レスポンスメッセージ

ステータスコード

### リクエスト例

```bash
curl -u app_id:app_secret -X DELETE {api}/clients/client_1
```

### レスポンス例

```http
// HTTPステータスコード
204 
```

## クライアントを特定のトピックにサブスクライブさせる

### URI

POST /clients/{client_id}/subscribe

### リクエストメッセージ

| 名前  | 型      | 説明                          |
| ----- | ------- | ----------------------------- |
| topic | String  | サブスクライブするトピック    |
| qos   | Integer | QoS（サービス品質）レベル     |
| nl    | Integer | No Local                      |
| rap   | Integer | Retain as Published           |
| rh    | Integer | Retain Handling               |

### レスポンスメッセージ

| 名前     | 型      | 説明                          |
| -------- | ------- | ----------------------------- |
| clientid | String  | クライアントID                |
| topic    | String  | サブスクライブしたトピック    |
| qos      | Integer | QoS（サービス品質）レベル     |
| node     | String  | ノード情報                   |
| nl       | Integer | No Local                      |
| rap      | Integer | Retain as Published           |
| rh       | Integer | Retain Handling               |

### リクエスト例

```bash
curl -u app_id:app_secret -X POST -H 'Content-Type: application/json' -d '{"topic": "t/a","qos": 1}' {api}/clients/client_1/subscribe
```

### レスポンス例

```json
// HTTPステータスコード
200
// HTTPレスポンスボディ
{
  "clientid": "client_1",
  "topic": "t/a",
  "qos": 1,
  "nl": 0,
  "node": "emqxsl-dev@10.66.128.31",
  "qos": 0,
  "rap": 0,
  "rh": 0,
}
```

## クライアントの特定トピックからのサブスクライブ解除

### URI

POST /clients/{client_id}/unsubscribe

### リクエストメッセージ

| 名前  | 型     | 説明                        |
| ----- | ------ | --------------------------- |
| topic | String | サブスクライブ解除するトピック |

### レスポンスメッセージ

ステータスコード

### リクエスト例

```bash
curl -u app_id:app_secret -X POST -H 'Content-Type: application/json' -d '{"topic": "t/a"}' {api}/clients/client_1/unsubscribe'
```

### レスポンス例

```http
// HTTPステータスコード
204
```

## クライアントの複数トピック一括サブスクライブ

### URI

POST /clients/{client_id}/subscribe/bulk

### リクエストメッセージ

| 名前       | 型      | 説明                          |
| ---------- | ------- | ----------------------------- |
| [].topic   | String  | サブスクライブするトピックのリスト |
| [].qos     | Integer | QoS（サービス品質）レベルのリスト |

### レスポンスメッセージ

| 名前            | 型               | 説明                              |
| --------------- | ---------------- | --------------------------------- |
| data            | オブジェクト配列 | すべてのトピックのサブスクリプション情報 |
| data[].clientid | String           | クライアントID                    |
| data[].topic    | String           | サブスクライブしたトピック        |

### リクエスト例

```bash
curl -u app_id:app_secret -X POST -H 'Content-Type: application/json' -d '[{"topic": "t/a", "qos": 1}, {"topic": "t/b", "qos": 0}]' {api}/clients/client_1/subscribe/bulk
```

### レスポンス例

```json
// HTTPステータスコード
200
// HTTPレスポンスボディ
{
    "data": [
        {
            "topic": "t/a",
            "clientid": "client_1",
            "nl": 0,
            "node": "emqxsl-dev@10.66.128.31",
            "qos": 1,
            "rap": 0,
            "rh": 0,
        },
        {
            "topic": "t/b",
            "clientid": "client_1",
            "nl": 0,
            "node": "emqxsl-dev@10.66.128.31",
            "qos": 0,
            "rap": 0,
            "rh": 0,
        }
    ]
}
```

## クライアントの複数トピック一括サブスクライブ解除

### URI

POST /clients/{client_id}/unsubscribe/bulk

### リクエストメッセージ

| 名前       | 型     | 説明                          |
| ---------- | ------ | ----------------------------- |
| [].topic   | String | サブスクライブ解除するトピックのリスト |

### レスポンスメッセージ

ステータスコード

### リクエスト例

```bash
curl -u app_id:app_secret -X POST -H 'Content-Type: application/json' -d '[{"topic": "t/a"},{"topic": "t/b"}]' {api}/clients/client_1/unsubscribe/bulk
```

### レスポンス例

```http
// HTTPステータスコード
204 
```
