# Session 22: MQTT

## Summary

MQTT 3.1.1/5.0 protocol adapter — allows MQTT clients to connect to NATS and interact with JetStream for persistence.

## Scope

| Go File | Features | Feature IDs | Go LOC |
|---------|----------|-------------|--------|
| server/mqtt.go | 153 | 2252–2404 | 4,758 |
| **Total** | **153** | | **4,758** |

## .NET Classes

- `MqttHandler` — MQTT protocol handler (35 features)
- `MqttAccountSessionManager` — per-account MQTT session tracking (26 features)
- `MqttSession` — individual MQTT session state (15 features)
- `MqttJetStreamAdapter` — bridges MQTT to JetStream (22 features)
- `MqttReader` — MQTT packet reader (8 features)
- `MqttWriter` — MQTT packet writer (5 features)
- Various MQTT reload options

## Test Files

| Test File | Tests | Test IDs |
|-----------|-------|----------|
| server/mqtt_test.go | 159 | 2170–2328 |
| server/mqtt_ex_test_test.go | 2 | 2168–2169 |
| server/mqtt_ex_bench_test.go | 1 | 2167 |
| **Total** | **162** | |

## Dependencies

- Session 01 (Foundation Types)
- Session 08 (Client Connection)
- Session 09 (Server Core Part 1)
- Session 11 (Accounts)
- Session 17 (Store Interfaces)
- Session 19 (JetStream Core)

## .NET Target Location

- `dotnet/src/ZB.MOM.NatsNet.Server/Mqtt/`

## Notes

- MQTT is a self-contained protocol layer — could potentially be a separate assembly
- 159 MQTT tests cover connection, subscribe, publish, QoS levels, sessions, retained messages
- MQTT ↔ JetStream bridging is the most complex part
- Consider using `System.IO.Pipelines` for MQTT packet parsing
