| 1 | Queue |
| 2 | === |
| 3 | |
| 4 | Users often want to call `Publish` and then move on to other things on the assumption that the message will eventually |
| 5 | get to the server (even if the connection is down at the time the message is sent). This example shows how to implement |
| 6 | this with `autopaho`; it sends a large number of QOS1/2 messages and confirms that they are correctly received (using a |
| 7 | second connection). |
| 8 | |
| 9 | The main reason for writing this example was to load test the library (sending a large number of messages and, |
| 10 | potentially, simulating network outages). |
| 11 | |
| 12 | ## MQTT V5 |
| 13 | |
| 14 | Note that a linked concept is the MQTT V5 "Receive maximum" functionality. This allows the server to tell the client the |
| 15 | maximum number of inflight QOS1/2 messages that it will accept. The client then needs to limit inflight messages to |
| 16 | comply; this may mean that the client needs to queue messages to avoid exceeding the limit. How this is configured will |
| 17 | vary depending upon the server (with Mosquitto its the `max_inflight_messages` setting and defaults to 20). |
| 18 | |
| 19 | Note that, by default, a memory-based queue is used. This example demonstrates storage on disk thus enabling queued |
| 20 | messages to survive an application restart. |
| 21 | |
| 22 | ## Running with the docker Mosquitto instance and testing network outages |
| 23 | |
| 24 | Use three consoles; one each for: |
| 25 | * Mosquitto |
| 26 | * The example |
| 27 | * Simulating network outages |
| 28 | |
| 29 | ### Mosquitto |
| 30 | |
| 31 | **Note**: Before running this ensure that `mosquitto.conf` contains `persistence true` (this is not in the default |
| 32 | config and without it tests that include restarting the server will fail). |
| 33 | |
| 34 | Change to the `autopaho/examples/docker` folder and run `docker compose up mosquitto` this will start Mosquitto and the |
| 35 | log will be output. Note that logging significantly impacts performance so if you are testing that edit the |
| 36 | `autopaho/examples/docker/binds/mosquitto/config/mosquitto.conf` and comment out `log_type all`. |
| 37 | |
| 38 | ### The example |
| 39 | |
| 40 | Build and run the example (i.e. `go build` then `queue`). Note that `queue~.msg` files will be written to the current |
| 41 | folder. |
| 42 | |
| 43 | ### Simulating Network Outages |
| 44 | |
| 45 | Due to the use of QOS1/2 the test app should receive 100% of the messages sent. To confirm that everything is working |
| 46 | as anticipated, we can simulate network issues. |
| 47 | |
| 48 | There are a variety of options: |
| 49 | * Stop and start the Mosquitto container |
| 50 | * Kill the network |
| 51 | * Identify the network with `docker network ls` (probably called `docker_paho-test-net`) |
| 52 | * Identify the mosquitto container with `docker container ls` (probably be `docker-mosquitto-1`) |
| 53 | * disconnect: `docker network disconnect docker_paho-test-net docker-mosquitto-1` |
| 54 | * reconnect: `docker network connect docker_paho-test-net docker-mosquitto-1` |