You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a minimal example demonstrating MQTT QoS 2 functionality with deduplication and session restoration capabilities.
4
+
5
+
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.
6
+
7
+
## Build
8
+
9
+
```bash
10
+
$ cmake .&& make
11
+
```
12
+
13
+
## Usage
14
+
15
+
This example requires a local `mosquitto` broker to run against.
16
+
17
+
### 1. Standard Testing
18
+
Run the client normally to see the full QoS 2 handshake without any interruptions:
19
+
20
+
```bash
21
+
$ ./lws-minimal-mqtt-client-qos2
22
+
```
23
+
24
+
The client will successfully connect, subscribe, publish a message, receive it, and log the payload, followed by the normal completion.
25
+
26
+
### 2. Fault Injection Mode (Session Resumption)
27
+
Run the client with the `-f` flag to simulate a dropped connection during the QoS 2 handshake:
28
+
29
+
```bash
30
+
$ ./lws-minimal-mqtt-client-qos2 -f
31
+
```
32
+
33
+
**Sequence of Events:**
34
+
1. The client receives the QoS 2 `PUBLISH` from the broker.
35
+
2. The custom `my_rx_add` callback fires, saving the unacknowledged `packet_id` to the simulated state store.
36
+
3. The client immediately drops its connection (forceful simulation).
37
+
4. The built-in retry policy triggers a reconnection.
38
+
5. On connection establishment, the client injects the stashed `packet_id` back into the library using `lws_mqtt_client_qos2_rx_add`.
39
+
6. Mosquitto resends the `PUBLISH` with `DUP=1`.
40
+
7.`libwebsockets` recognizes the duplicate, drops the payload to maintain the *Exactly-Once* guarantee, and seamlessly resumes the handshake (`PUBREC` -> `PUBREL` -> `PUBCOMP`).
41
+
42
+
## Commandline Options
43
+
44
+
-`-d <log level>`: Set the logging level (e.g., `-d 1039`)
45
+
-`-s`: Use TLS / HTTPS
46
+
-`-f`: Simulate connection drop for testing QoS 2 restoration
0 commit comments