MQTT 00:00:01 A sleeping kitchen sensor speaks to a broker A kitchen thermometer and a hallway switch share an MQTT broker. The phone wants temperatures, while a home-rules service watches every device. Topic filters make those subscriptions overlap without coupling the publishers to particular consumers. Each client opens one connection and sends CONNECT. The broker answers CONNACK. The phone subscribes to temperatures one room below home. The rules service subscribes to every topic under home. A kitchen-temperature publication matches both subscriptions. A hallway-door publication matches the rules service's broader subscription only. home/kitchen/humidity would reach the rules service only. Without it, the broker drops the message. The sensor would not notice. Topic matching determines who is eligible; session settings, retained state, access controls and quality of service determine the obligations after a match. 00:00:59 Three delivery agreements The door sensor reports a state change. MQTT supports three quality-of-service levels on a delivery hop. Compare what sender and receiver retain when an acknowledgement is lost. At level zero a publish has no acknowledgement exchange. A break can lose the publication. The packet has the ID 7, and the lock keeps a copy. At level one, losing the acknowledgement can cause the sender to resend a publication that the receiver already handled. At level two the peers use a longer exchange and retain protocol state to resolve retransmissions. Completing the handshake releases the packet identifier for future reuse under the protocol rules. Exactly-once protocol delivery at the appropriate MQTT hop does not make a physical action or downstream database update exactly once. Give consequential side effects a durable application identity. 00:01:55 Retained readings and persistent sessions The phone opens while the thermometer sleeps. It wants the latest known temperature immediately, and may also need to catch up on eligible messages sent while its session was away. The thermometer publishes 21.5 with the retain flag. The broker keeps a copy. The app subscribes and gets 21.5 at once, although the sensor is asleep. The phone enters a tunnel. Its session is kept for an hour, with its QoS 1 subscription. Two QoS 1 readings match. They go into the session's queue. A QoS 0 reading would usually be dropped. Same client ID, clean start off: the queue is delivered in order. Nothing had to be subscribed again. A retained value belongs to a topic. A queued delivery belongs to a subscription in a retained session. Specify which state should survive a disconnect and how long it may be kept. 00:02:51 Tell the house when the sensor vanishes The device registers a last-will message when it connects, then announces that it is online. An abrupt power loss prevents it from sending a final status. The broker needs time to distinguish disappearance from temporary silence. In CONNECT the thermometer leaves a retained offline for its status topic, then publishes online. No DISCONNECT is sent. The broker cannot know yet; the connection looks merely quiet. No PINGREQ comes after 60 seconds. The broker waits for half a period more. After 90 seconds the broker closes the connection and publishes offline. The app and the rules service learn it. A normal graceful disconnect in this example suppresses the ordinary will. The device publishes its intended offline status before leaving. Treat the will as a liveness signal with a detection delay. Graceful disconnect behavior depends on the protocol version and options, so send an explicit status when the product requires it. 00:03:54 Power returns to an entire neighborhood One hundred thousand devices can overwhelm a perfectly healthy broker if all begin secure handshakes together. Spread reconnect attempts and verify that each returning device republishes its current state. Without a random delay, 100,000 TLS handshakes arrive within seconds. The broker's CPU is full and clients give up. Each device waits a random time of up to 5 minutes: 100,000 / 300 is about 333 connects a second. Each device publishes a retained online, which replaces the offline left by its will. Screens become right by themselves. Five minutes contain three hundred seconds. A uniform spread averages about three hundred thirty-three reconnects per second, although the real instantaneous rate varies. Plan for that variation and for clients that miss another attempt.