Bird
Raised Fist0
IOT Protocolsdevops~10 mins

MQTT keep-alive and timeout in IOT Protocols - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - MQTT keep-alive and timeout
Client connects to Broker
↓
Client sends CONNECT with keep-alive
↓
Broker starts keep-alive timer
↓
Client sends PINGREQ before timer expires
↓
Broker receives PINGREQ and resets timer
↓
If no PINGREQ before timer expires
↓
Broker closes connection (timeout)
This flow shows how MQTT clients send a keep-alive value to the broker, which expects periodic PINGREQ messages to keep the connection alive. If the broker does not receive a PINGREQ in time, it closes the connection.
Execution Sample
IOT Protocols
CONNECT keep-alive=10s
---> Broker starts 10s timer
Client sends PINGREQ at 9s
---> Broker resets timer
Client sends PINGREQ at 19s
---> Broker resets timer
No PINGREQ by 29s
---> Broker disconnects client
This example shows a client connecting with a 10-second keep-alive, sending PINGREQ messages before the timer expires to keep the connection alive, and the broker disconnecting after a timeout.
Process Table
StepTime (s)Client ActionBroker ActionTimer StateConnection State
10CONNECT with keep-alive=10Start 10s timerTimer set to 10sConnected
29Send PINGREQReceive PINGREQ, reset timerTimer reset to 10sConnected
319Send PINGREQReceive PINGREQ, reset timerTimer reset to 10sConnected
429No PINGREQ sentTimer expired, disconnect clientTimer expiredDisconnected
💡 At 29s, no PINGREQ received before timer expired, so broker disconnects client
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Timer (seconds left)N/A1010100 (expired)
Connection StateDisconnectedConnectedConnectedConnectedDisconnected
Key Moments - 3 Insights
Why does the broker disconnect the client at step 4?
Because the broker did not receive a PINGREQ before the 10-second timer expired, indicating the client might be offline or unreachable (see execution_table row 4).
What happens when the client sends a PINGREQ before the timer expires?
The broker resets the keep-alive timer to the full duration, keeping the connection alive (see execution_table rows 2 and 3).
Does the client have to send PINGREQ exactly at the timer expiration?
No, the client should send PINGREQ anytime before the timer expires to reset it; sending early is fine (see execution_table row 2 where PINGREQ is sent at 9s before 10s timer).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the connection state after step 3?
ADisconnected
BConnecting
CConnected
DUnknown
💡 Hint
Check the 'Connection State' column at step 3 in the execution_table.
At which step does the broker disconnect the client due to timeout?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look for the step where 'Timer expired' and 'Disconnected' appear in the execution_table.
If the client sends PINGREQ at 11 seconds instead of 9, what happens?
ABroker disconnects client at 10s
BBroker resets timer at 11s
CConnection stays alive without reset
DClient reconnects automatically
💡 Hint
Refer to the timer expiration logic in the execution_table and variable_tracker.
Concept Snapshot
MQTT keep-alive is a timer set by the client in CONNECT message.
Client must send PINGREQ before timer expires to keep connection alive.
Broker resets timer on PINGREQ receipt.
If timer expires without PINGREQ, broker disconnects client.
Keep-alive ensures broker detects lost clients quickly.
Full Transcript
MQTT uses a keep-alive timer to check if clients are still connected. When a client connects, it tells the broker how many seconds it will wait before sending a PINGREQ message. The broker starts a timer for this duration. The client must send a PINGREQ before the timer runs out. Each time the broker gets a PINGREQ, it resets the timer. If the timer expires without a PINGREQ, the broker assumes the client is gone and disconnects it. This process helps keep connections healthy and detect lost clients fast.

Practice

(1/5)
1. What is the main purpose of the MQTT keep-alive timer?
easy
A. To check if the connection between client and broker is still active
B. To encrypt messages sent between client and broker
C. To increase the message sending speed
D. To store messages on the broker

Solution

  1. Step 1: Understand MQTT keep-alive role

    The keep-alive timer is used to monitor connection health by expecting regular messages or pings.
  2. Step 2: Identify the purpose from options

    Only To check if the connection between client and broker is still active describes checking if the connection is still active, which matches the keep-alive function.
  3. Final Answer:

    To check if the connection between client and broker is still active -> Option A
  4. Quick Check:

    Keep-alive checks connection health = D [OK]
Hint: Keep-alive means connection check timer [OK]
Common Mistakes:
  • Confusing keep-alive with encryption
  • Thinking keep-alive speeds up messages
  • Assuming keep-alive stores messages
2. Which of the following is the correct way for an MQTT client to avoid a keep-alive timeout?
easy
A. Send a PINGREQ message before the keep-alive timer expires
B. Disconnect immediately after connecting
C. Send a CONNECT message repeatedly without waiting
D. Ignore the keep-alive timer completely

Solution

  1. Step 1: Review MQTT client behavior for keep-alive

    Clients must send any message or a PINGREQ before the keep-alive timer expires to keep the connection alive.
  2. Step 2: Match correct action with options

    Send a PINGREQ message before the keep-alive timer expires correctly states sending PINGREQ before timeout; others are incorrect or harmful.
  3. Final Answer:

    Send a PINGREQ message before the keep-alive timer expires -> Option A
  4. Quick Check:

    Send PINGREQ before timer ends = A [OK]
Hint: Send PINGREQ before timer ends to stay connected [OK]
Common Mistakes:
  • Sending CONNECT repeatedly causes errors
  • Disconnecting breaks connection
  • Ignoring timer causes timeout
3. Given a client with a keep-alive interval of 10 seconds, what happens if the client sends no messages or pings for 15 seconds?
medium
A. The client automatically reconnects
B. The broker waits indefinitely without closing
C. The broker sends a PINGRESP to the client
D. The broker closes the connection due to timeout

Solution

  1. Step 1: Understand keep-alive timeout behavior

    If no message or ping is received within the keep-alive interval, the broker considers the client disconnected and closes the connection.
  2. Step 2: Analyze the 15-second no message case

    Since 15 seconds > 10 seconds interval, the broker will close the connection due to timeout.
  3. Final Answer:

    The broker closes the connection due to timeout -> Option D
  4. Quick Check:

    No message > keep-alive interval = connection closed [OK]
Hint: No message past keep-alive means disconnect [OK]
Common Mistakes:
  • Thinking broker waits forever
  • Assuming client auto reconnects immediately
  • Confusing PINGRESP as timeout action
4. A developer sets the MQTT keep-alive interval to 0. What issue will most likely occur?
medium
A. The client will immediately timeout and disconnect
B. The client will never timeout, causing stale connections
C. The broker ignores the keep-alive setting and uses default
D. The client sends continuous PINGREQ messages flooding the broker

Solution

  1. Step 1: Understand keep-alive value 0 meaning

    A keep-alive value of 0 turns off the keep-alive mechanism. The broker does not expect PINGREQs and will not timeout the connection based on keep-alive.
  2. Step 2: Identify effect on connection

    This can lead to stale connections if the client dies without sending DISCONNECT.
  3. Final Answer:

    The client will never timeout, causing stale connections -> Option B
  4. Quick Check:

    Keep-alive 0 disables mechanism = no timeout = A [OK]
Hint: Keep-alive 0 disables mechanism, no timeout, risk of stale connections [OK]
Common Mistakes:
  • Thinking keep-alive 0 causes immediate timeout
  • Assuming broker uses default automatically
  • Believing client floods with pings
5. You want to configure an MQTT client to keep the connection alive even if no messages are sent for 30 seconds. Which setting and action combination is correct?
hard
A. Set keep-alive to 10 seconds and send PINGREQ every 40 seconds
B. Set keep-alive to 60 seconds and never send PINGREQ messages
C. Set keep-alive to 30 seconds and send PINGREQ before 30 seconds expire
D. Set keep-alive to 0 and rely on broker to keep connection alive

Solution

  1. Step 1: Match keep-alive interval with desired timeout

    To keep connection alive for 30 seconds without messages, set keep-alive to 30 seconds so broker expects activity within that time.
  2. Step 2: Ensure client sends PINGREQ before timeout

    Client must send PINGREQ before 30 seconds expire to reset timer and avoid timeout.
  3. Final Answer:

    Set keep-alive to 30 seconds and send PINGREQ before 30 seconds expire -> Option C
  4. Quick Check:

    Keep-alive = 30s + timely PINGREQ = A [OK]
Hint: Keep-alive time = max wait; send PINGREQ before then [OK]
Common Mistakes:
  • Setting keep-alive too low or too high without matching pings
  • Not sending PINGREQ within keep-alive interval
  • Using 0 disables keep-alive causing disconnect
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard