# Loopback publish and subscribe The Atlas MQTT quick start, and the project the [tutorial](https://youseful.software/delphi/atlas-mqtt.html#tutorial) builds step by step. A console program that: 1. starts a `TAtlasMqttBroker` inside the program, on `127.0.0.1` and a port the system picks (`Port := 0`); 2. connects a `TAtlasMqttClient` to it with `ConnectAndWait`; 3. subscribes with the wildcard filter `sensors/+/temperature` at quality 2 and waits for `OnSubscribed`; 4. publishes three matching readings, at quality 0, 1 and 2, and one reading on `sensors/kitchen/humidity` that the filter does not match; 5. waits, with a timeout, for the three matching messages, for `WaitForIdle` and for an `OnPublished` event per acknowledged message, then checks every topic, payload and quality; 6. disconnects and stops the broker. The client receives its own messages because its subscription matches them: MQTT 3.1.1 has no "no local" option. Every check raises on a mismatch; the program exits with code 0 only when all of them pass. Nothing leaves the machine and nothing needs to be installed. ## Build and run Open `LoopbackPublishSubscribe.dpr` in Delphi 13; the IDE creates the project file. Add the Atlas MQTT `src` folder to the project's search path, then build and run for Win32 or Win64. No packages need to be installed. The program needs no input. It checks its own results and stops with an error if any of them differs from the output below. Windows is required: the transport uses Winsock. ## Expected output The port number changes on every run, and the three "acknowledged" lines can appear in a different order and interleaved with the "received" lines, because acknowledgements and deliveries travel independently. Packet identifier 1 went to the SUBSCRIBE request, so the three publications above quality 0 get 2, 3 and 4. ``` Broker listening on 127.0.0.1:62668 ok: the client connects ok: the broker counts one client subscribed to sensors/+/temperature: granted=True quality=2 ok: the broker grants the subscription at quality 2 received sensors/kitchen/temperature=21.5 q0 received sensors/hall/temperature=19.0 q1 broker acknowledged packet 2 received sensors/garage/temperature=12.5 q2 ok: three messages arrive ok: nothing is left in flight broker acknowledged packet 4 broker acknowledged packet 3 ok: OnPublished fired for each of the three acknowledged messages ok: the humidity message was filtered out by the broker ok: kitchen reading at quality 0 ok: hall reading at quality 1 ok: garage reading at quality 2 ok: in-flight count is zero ok: the broker accepted four messages ok: the client is disconnected ok: the broker is stopped Done. ``` `RoutedCount` is 4 because the broker counts every message it accepts from a publisher, including the humidity reading that no subscription matched. ## Adapting it - To talk to a broker that is already running, delete the `TAtlasMqttBroker` lines and set `Client.Host` and `Client.Port` to its address. Atlas MQTT speaks MQTT 3.1.1 over plain TCP only; there is no TLS. - Try other filters: `sensors/#` also matches the humidity reading, `sensors/kitchen/+` matches both kitchen readings. - In a VCL application, drop the `CheckSynchronize` loop: with `SynchronizeEvents` at its default the events already run on the main thread through the application's message loop.