CoAP Observe Not Sending Notifications
DodaTech
Updated 2026-06-26
1 min read
In this tutorial, you'll learn about CoAP Observe Not Sending Notifications. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
CoAP Observe registration succeeds but no notifications are sent on resource change.
Quick Fix
Wrong
res.observe(req, res); // Notifications not sent on change```
Client receives initial response but no subsequent updates.
### Right
```cpp
#include <coap.h>
static float temp = 25.0;
void get_handler(coap_context_t *ctx, coap_resource_t *res,
coap_session_t *session, coap_pdu_t *request,
coap_binary_t *token, coap_string_t *query,
coap_pdu_t *response) {
unsigned char buf[32];
int n = snprintf((char*)buf, sizeof(buf), "{"temp":%.1f}", temp);
coap_add_data(response, n, buf);
}
int main() {
coap_context_t *ctx = coap_new_context(NULL);
coap_resource_t *res = coap_resource_init(
(const uint8_t*)"temperature", 11, COAP_RESOURCE_FLAGS_NOTIFY);
coap_register_handler(res, COAP_REQUEST_GET, get_handler);
// Enable observable
coap_resource_set_get_observable(res, 1);
coap_start(ctx);
// Periodically change value and notify
while (1) {
sleep(5);
temp += 0.5;
coap_resource_notify_observers(res, NULL);
}
}```
Client receives initial 2.05, then updates every 5 seconds: {"temp":25.5}, {"temp":26.0}...
## Prevention
CoAP Observe enables pub/sub. The resource must be created with COAP_RESOURCE_FLAGS_NOTIFY. Register with GET containing Observe:0 option. Call coap_resource_notify_observers() when the resource changes. The server sends 2.05 with an Observe sequence number each update. Use NON messages for frequent updates.
DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.
## FAQ
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">### How does Observe registration work?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>Client sends GET with Observe:0. Server responds 2.05 + Observe seq number. Future updates carry incrementing Observe numbers.</p>
<h3 id="cancel-observation">Cancel observation?</h3><p>Client sends GET with Observe:1 (deregister). Server removes observers on notification failure (NACK/timeout after retries).</p>
<h3 id="what-happens-on-disconnect">What happens on disconnect?</h3><p>CoAP over UDP has no persistent connection. Server deregisters observers when notification fails after retry limit.</p>
</div></details>
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro