COAP_KEEPALIVE(3)
π COAP_KEEPALIVE π
okay so this is a page out of the libcoap Manual, and it's literally all about keeping your CoAP session alive and thriving, no cap π«Ά
β¨ NAME β¨
coap_keepalive, coap_context_set_keepalive β bestie, these are the ones that work with CoAP keepalive π
πΈ SYNOPSIS πΈ
#include <coap3/coap.h>
void coap_context_set_keepalive(coap_context_t *context, unsigned int
seconds);
so about linking β for specific (D)TLS library support, you link with one of these icons π:
-lcoap-3-notls-lcoap-3-gnutls-lcoap-3-openssl-lcoap-3-mbedtls-lcoap-3-wolfssl-lcoap-3-tinydtls
otherwise (like, the chill default path), you link with -lcoap-3 and you get the default (D)TLS library support β¨
π¦ DESCRIPTION π¦
so picture this: sometimes you need to send out keepalive traffic when the CoAP session is just sitting there idle β meaning no packets have been sent or received for a specified period. why? to keep, say, an interim NAT device "warm" with the NAT translation state, or to periodically check whether the device at the other end of the session has "gone away" π like, are you still there bestie??
for UDP/DTLS, this is done with the confirmable CoAP (0.00) Ping packet, which solicits a CoAP RST response. for TCP/TLS, this is done with the CoAP (7.02) Ping packet, which solicits a CoAP (7.03) Pong response β and all of that is handled by libcoap for you, so slay π
π FUNCTIONS π
π Function: coap_context_set_keepalive()
the coap_context_set_keepalive() function needs to be called to update the context with the keepalive for idle traffic timeout of seconds. and listen β if seconds is set to 0 (which is the default, fyi), then the sending of keepalives is disabled. any sessions created from this context will use the same seconds value to determine whether a keepalive "ping" is to be sent out or not π
applications can track the usage of the receipt of "pings" and receipt of "responses" by defining the respective handlers to use, via coap_register_ping_handler() and coap_register_pong_handler() π
and if the keepalive fails to solicit a response, you can track that too by defining the handler to use via coap_register_event_handler() β which will be called with a reason of COAP_EVENT_KEEPALIVE_FAILURE π
NOTE: since this may be used to keep an interim NAT device "warm", the exponentially increasing retransmit times for CON requests is limited to seconds, but the retransmit counter is not affected. and unless you actually need it, seconds should not be set to less than 30 β fr fr, don't do it π
β SEE ALSO β
π§ FURTHER INFORMATION π§
see these iconic docs for further information π
- "RFC7252: The Constrained Application Protocol (CoAP)"
- "RFC8323: CoAP (Constrained Application Protocol) over TCP, TLS, and WebSockets"
π BUGS π
please raise an issue on GitHub at https://github.com/obgm/libcoap/issues to report any bugs π
and please raise a Pull Request at https://github.com/obgm/libcoap/pulls for any fixes π«Ά
π AUTHORS π
the libcoap project <libcoap-developers@lists.sourceforge.net> π
and that's the whole vibe, bestie β this page is from coap_keepalive version 4.3.5, dated 08/24/2026, mwah πβ¨
π¦ where this page comes from
- ubuntu stonking: libcoap3t64 4.3.5-4ubuntu1 Β· BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms