# 即時推播 (https://nolantw.github.io/tdx/reference/mqtt/)

## `QoS`

```python
type QoS = Literal[0, 1, 2]
```

## `Topic`

```python
class Topic:
    path: str
    model: type[T]
    key: str | None
```

一個主題與其資料。

## `Message`

```python
class Message:
    topic: str
    records: Records[T]
```

同時送達的資料與其主題。

## `Topics`

```python
class Topics:
    ...
```

選擇推播的主題。

### `bus_news`

```python
def bus_news(city: City | str | None) -> Topic[BusNews]
```

### `bus_alerts`

```python
def bus_alerts(city: City | str | None) -> Topic[BusAlert]
```

### `railway_alerts`

```python
def railway_alerts() -> Topic[RailAlert]
```

### `high_speed_alerts`

```python
def high_speed_alerts() -> Topic[RailAlert]
```

### `metro_alerts`

```python
def metro_alerts(system: str | None = None) -> Topic[RailAlert]
```

### `ship_alerts`

```python
def ship_alerts() -> Topic[ShipAlert]
```

## `Subscriber`

```python
class Subscriber(_Credentials):
    ...
```

以 TDX 會員中心的 MQTT 帳號接收推播。未傳入時讀取環境變數 `TDX_MQTT_CLIENT_ID`、`TDX_MQTT_USERNAME` 與 `TDX_MQTT_PASSWORD`。同一組帳號只能有一個連線。

### `listen`

```python
def listen(*topics: Topic[T], qos: QoS = 1) -> Generator[Message[T]]
```

等待訊息，斷線時重新連線。