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