<!-- review: finished -->

<a id="stream-mqtt-preread"></a>

# MQTT Preread

Enables extracting client IDs and usernames
from `CONNECT` packets for Message Queuing Telemetry Transport (MQTT)
versions
[3.1.1](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718028)
and
[5.0](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901033).

When [building from the source code](https://en.angie.software//angie/docs/installation/sourcebuild.md#sourcebuild),
the module must be enabled with the [build parameter](https://en.angie.software//angie/docs/installation/sourcebuild.md#configure)
`--with-stream_mqtt_preread_module`.
In packages and images from
[our repositories](https://en.angie.software//angie/docs/installation/index.md#install-packages),
the module is included in the build.

<a id="configuration-example-63"></a>

## Configuration Example

<a id="choosing-a-server-in-a-group-by-client-id"></a>

### Choosing a server in a group by client ID:

```nginx
stream {

    mqtt_preread on;

    upstream mqtt {
        hash $mqtt_preread_clientid;
        # ...
    }
}
```

<a id="directives-72"></a>

## Directives

<a id="index-0"></a>

<a id="s-mqtt-preread"></a>

### mqtt_preread

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `mqtt_preread` `on` | `off`;   |
|------------------------------------------------------------------------------------------|--------------------------------|
| Default                                                                                  | `mqtt_preread off;`            |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | stream, server                 |

Controls extracting information from `CONNECT` packets
during the
[preread phase](https://en.angie.software//angie/docs/configuration/processing.md#stream-sessions).
If the parameter is enabled (`on`),
the variables listed below are populated
in the context where it is specified.

<a id="built-in-variables-20"></a>

## Built-in Variables

For detailed description of value semantics,
see the MQTT protocol specification versions
[3.1.1](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718031)
and [5.0](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901033).

<a id="v-mqtt-preread-clientid"></a>

### `$mqtt_preread_clientid`

Unique client identifier.

<a id="v-mqtt-preread-username"></a>

### `$mqtt_preread_username`

Optional username.
