Skip to content

Commit 14e27df

Browse files
Update readme to configure TCP keep alive when updating MQTT keep alive time (#1896)
* add docs to update MQTT keep alive time * fix formatting * add docs to update MQTT keep alive time * Update demos/readme.md Co-authored-by: Gaurav-Aggarwal-AWS <[email protected]> * updating PR with review comments * Fix indentation --------- Co-authored-by: Gaurav-Aggarwal-AWS <[email protected]>
1 parent 5aa7013 commit 14e27df

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

demos/readme.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
## Updating MQTT keep alive time
3+
4+
When you increase MQTT keep alive time and observe that the device is getting disconnected before the MQTT keep alive kicks in, one possible reason might be that the underlying TCP connection dies before the MQTT keep alive timer expires.
5+
6+
One possible solution is to enable TCP keep alive to ensure that the TCP connection does not die because of inactivity. On BSD (or similar) sockets it can be enabled by configuring the following options described in [socket(7)](https://man7.org/linux/man-pages/man7/socket.7.html) and [tcp(7)](https://man7.org/linux/man-pages/man7/tcp.7.html) man pages:
7+
8+
* `SO_KEEPALIVE` - Keep-alives are sent only when the `SO_KEEPALIVE` socket option is enabled.
9+
* `TCP_KEEPIDLE`- The time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes, if the socket option `SO_KEEPALIVE` has been set on this socket.
10+
* `TCP_KEEPINTVL` - The time (in seconds) between individual keepalive probes.
11+
* `TCP_KEEPCNT` - The maximum number of keepalive probes TCP should send before dropping the connection.
12+
13+
Sample code snippet to update TCP keep alive configuration for BSD sockets:
14+
15+
``` c
16+
int status;
17+
int keepAliveFlag = 1, keepAliveIdle = 60, keepAliveInterval = 15, keepAliveCount = 4;
18+
19+
/* Enable keep alive feature. */
20+
status = setsockopt( *pTcpSocket,
21+
SOL_SOCKET,
22+
SO_KEEPALIVE,
23+
&keepAliveFlag,
24+
( socklen_t ) sizeof( keepAliveFlag ) );
25+
assert( status == 0 );
26+
27+
/* If the connection is idle for 60 seconds, send keep
28+
* alive probe. */
29+
status = setsockopt( *pTcpSocket,
30+
IPPROTO_TCP,
31+
TCP_KEEPIDLE,
32+
&keepAliveIdle,
33+
( socklen_t ) sizeof( keepAliveIdle ) );
34+
assert( status == 0 );
35+
36+
/* When a keep alive probe is unacknowledged, this is time interval
37+
* between keep alive probes sent. */
38+
status = setsockopt( *pTcpSocket,
39+
IPPROTO_TCP,
40+
TCP_KEEPINTVL,
41+
&keepAliveInterval,
42+
( socklen_t ) sizeof( keepAliveInterval ) );
43+
assert( status == 0 );
44+
45+
/* When these many keep alive probes go unacknowledged, declare the
46+
* connection dead. */
47+
status = setsockopt( *pTcpSocket,
48+
IPPROTO_TCP,
49+
TCP_KEEPCNT,
50+
&keepAliveCount,
51+
( socklen_t ) sizeof( keepAliveCount ) );
52+
assert( status == 0 );
53+
54+
```

0 commit comments

Comments
 (0)