minimal-examples-lowlevel/mqtt-client/minimal-mqtt-client-qos2/README.md
This is a minimal example demonstrating MQTT QoS 2 functionality with deduplication and session restoration capabilities.
The client connects to a local Mosquitto broker, subscribes to a topic (test/topic0), and publishes a QoS 2 message to that same topic. It tracks the packet_id of unacknowledged QoS 2 receives using the operations API.
$ cmake . && make
This example requires a local mosquitto broker to run against.
Run the client normally to see the full QoS 2 handshake without any interruptions:
$ ./lws-minimal-mqtt-client-qos2
The client will successfully connect, subscribe, publish a message, receive it, and log the payload, followed by the normal completion.
Run the client with the -f flag to simulate a dropped connection during the QoS 2 handshake:
$ ./lws-minimal-mqtt-client-qos2 -f
Sequence of Events:
PUBLISH from the broker.my_rx_add callback fires, saving the unacknowledged packet_id to the simulated state store.packet_id back into the library using lws_mqtt_client_qos2_rx_add.PUBLISH with DUP=1.libwebsockets recognizes the duplicate, drops the payload to maintain the Exactly-Once guarantee, and seamlessly resumes the handshake (PUBREC -> PUBREL -> PUBCOMP).-d <log level>: Set the logging level (e.g., -d 1039)-s: Use TLS / HTTPS-f: Simulate connection drop for testing QoS 2 restoration