Subscriptions and Updates: CoAP Observe Reference
More
Depth: Practitioner
Progress saves as you read. Mark sections complete as you go; your progress appears in the sidebar.
This page preserves the source material accompanying CoAP Observe: Subscriptions and Updates. The three diagrams and arrival table are in the chapter. The five knowledge-check blocks are preserved on the standalone check page. The source headings and examples below retain their original wording.
Start With the Decision
A thermostat registers for /temperature and receives 23.5. Later, two updates arrive in reverse order. Which value should it display, and what should it do if no fresh update arrives?
CoAP is a compact request-and-response protocol. Observe extends a GET request so the server can send later responses called notifications. The client and server retain an observation, or registration state. Work through one thermostat trace to decide whether each notification belongs to that observation, is newer, and is still fresh.
Route Overview
This is part 1 of 2. Continue with CoAP Observe: Efficiency and Lifecycle.
Part Objectives
- Trace registration, notification, token, and sequence state.
- Set update pace and recovery rules for a CoAP observer.
Start With the Quiet Thermostat
The thermostat needs current temperature state, rather than a replay of every intermediate reading. Start with the registration above. If later notifications arrive out of order, retain the newer state. If its advertised freshness interval passes, mark the value stale and seek a fresh representation.
Observe does not promise that every change reaches the client. The following sections build the registration, ordering, pacing, and cleanup rules needed to make this decision.
- In 60 Seconds
- Start With the Quiet Thermostat
- Phoebe’s Field Notes: Why 89% Less Traffic Is Not 89% More Battery
- Phoebe’s Field Notes: Count Radio Wakes, Not Only Bytes
- Quick Check: Observe Fit
- Prerequisites
- Continue: CoAP Observe Registration and Freshness Contracts
- The Observe Extension (RFC 7641)
- Minimum Viable Understanding: CoAP Observe
- CoAP Observe Subscriptions and Notifications
- For Beginners: Understanding Observe vs Polling
- Putting Numbers to It
- Checkpoint: Push or Poll?
- Interactive Calculator: Polling vs Observe Bandwidth
- Observe Protocol Flow
- Try It: Deregistration Method Advisor
- Observer Management Implementation
- Try It: Observer Registry Simulator
- Putting Numbers to It
- Interactive: CoAP Congestion Control
- Interactive Calculator: Rate Limiting Configuration
- Checkpoint: Registry Discipline
- Deep Dive: Observe Internals
- Deep Dive: Observe Sequence Numbers and Ordering
- Interactive Tool: Sequence Number Freshness Checker
- Deep Dive: Observer Removal Conditions
- Try It: CON Retransmission Timeline Simulator
- Edge Cases and Gotchas
Learning Objectives
Given a temperature trace, you will be able to:
- Match a notification to its endpoint and Token, then decide which value is newer.
- Distinguish stale representation data from a removed server registration.
- Apply a pacing policy to a second sensor and explain its freshness limit.
- Choose cancellation or recovery evidence after a restart or lost network path.
Prerequisites
Before diving into this chapter, you should be familiar with:
- CoAP Introduction - CoAP basics and design goals
- CoAP Message Types - CON vs NON and reliability patterns
Continue: CoAP Observe Registration and Freshness Contracts
The main chapter below stays focused on Observe as a server-push pattern and implementation workflow. For the deeper contract behind registration-token binding, Max-Age freshness, Confirmable liveness probes, notification ordering, wraparound, and stale-update handling, continue to CoAP Observe Registration and Freshness Contracts.
The Observe Extension (RFC 7641)
The worked thermostat uses a GET with Observe: 0. Registration succeeds only when the response includes Observe. Later responses retain the request Token and carry Observe values for ordering. This is direct resource observation; no broker is required.
A GET with Observe: 1 and the same Token cancels the observation. A Reset (RST) rejecting an unwanted notification also tells the server to remove its observer entry. Max-Age expiry instead means the saved representation needs refreshing.
Temperature Terry sends the thermostat’s current value after registration. Lila adds a second observer of the same resource. Each observer needs its own endpoint and Token state: removing Lila’s entry must not remove the thermostat’s entry.
Unlike an event archive, this stream represents current state. Several changes may be combined into a later notification. The client must recover current state after a gap instead of assuming it received every change.
Polling repeats a GET to ask for a current representation. Observe registers a continuing request so the server can send later representations. With infrequent changes, this can avoid many repeated requests.
Neither approach guarantees immediate detection. Polling waits for the next request; Observe depends on notification policy, network delivery, and recovery. Compare both against the same freshness requirement before choosing.
Traditional Polling vs. Observe
The energy question is not simply “push or poll”; it is how many exchanges occur when a resource changes less often than a client would ask for it. The timelines in Figure 1 show the exchanges generated when client requests outpace changes to a resource.
Read Figure 1 from left to right along each lane. Polling repeats a GET and response even when the value has not changed; Observe pays once to register, then the server emits notifications when the representation changes. The diagram explains the message-count difference, while the figures below quantify it for one assumed polling interval and change rate rather than claiming the same saving for every workload.
Traffic comparison for 24 hours of temperature monitoring:
- Polling (every 60 seconds): 1,440 requests + 1,440 responses, about 61.9 KiB in the byte model below.
- Observe (10 changes/hour): 1 registration request + 1 initial response + 240 notifications, about 6.6 KiB, about 89% fewer modelled bytes.
Assume a lossless exchange, 16-byte requests and 28-byte responses. Count only these message bytes; exclude UDP/IP, security, ACKs, retries, cancellation, and recovery. These sizes are example inputs, not fixed CoAP sizes. For polling every 60 seconds over 24 hours:
With CoAP request (16 bytes) + response (28 bytes) = 44 bytes per exchange:
For Observe with 10 changes/hour over 24 hours:
The bandwidth reduction is:
Observer Architecture Overview
Before treating notifications as isolated responses, inspect the figure in the parent chapter to see the state that the client, wire exchange, and server must preserve together.
Start with one client. Track its own saved state. Read the architecture in the figure in the parent chapter from client state through Token and Observe ordering on the wire to the server’s observer registry. Then follow failure ownership and the bounded evidence required before the application accepts a notification. Multiple clients can observe one resource, but each relationship needs its own retained state; the next sections trace registration and notification behavior inside that contract.
Checkpoint: Push or Poll?
Predict the Observe message count if the initial response is counted separately. Does the byte saving prove a longer battery life?
Answer: There are 242 messages: two for registration and 240 later notifications. The model saves about 89.3% of the counted bytes, but excludes wake costs, retries and other energy use. Measure those before making a battery claim.
Observe Protocol Flow
Registration
Continue the thermostat trace with illustrative arrival times measured from registration. The server endpoint is 192.0.2.10:5683; the thermostat endpoint is 192.0.2.20:61616. Observation A binds those endpoints and Token 0xAB12 to /temperature with Accept: text/plain. Observation B uses the same endpoints and resource but Token 0xCD34 with Accept: application/json; it remains a separate registration throughout this trace. A representation is the resource value encoded in the response payload. Accept: text/plain asks for a plain-text representation. The client requests registration as follows:
Request
Client -> Server: GET /temperatureToken: 0xAB12Observe: 0(register)Accept: text/plain
Response
Server -> Client: 2.05 ContentToken: 0xAB12Observe: 1(sequence number)Max-Age: 60Payload: "23.5"
Observation A receives this response at t=0 s. The response includes Observe, so registration succeeded. Display 23.5°C until a newer representation is accepted; without one, mark it stale at t=60 s (0 + 60). Max-Age does not give the server a 60-second observer lease. Observation B receives its own successful registration response with Token 0xCD34 at t=0 s; responses for B never update A.
Notifications
Inspect the figure in the parent chapter to see how a client proves that each notification belongs to its observation and is fresher than the value already accepted.
Continue observation A with the arrivals in the table. The figure follows a subscription from registration through updates, cancellation or loss, and re-registration. For each arrival, first check that it came from the bound server endpoint to the bound thermostat endpoint with Token 0xAB12. Then compare its Observe value with the latest accepted response. All sequence comparisons occur within 128 seconds of the latest accepted response. A newer notification may update the value, but its Max-Age sets a freshness limit. A received notification still needs application acceptance checks. Times are local receipt times. The table records the receipt times, sequence values, temperatures, freshness limits, and decisions for A.
With no further accepted response, mark A’s 23.0°C stale at t=50 s and seek fresh state. The rejected arrival does not move the deadline to t=115 s (25 + 90). At t=55 s, the server receives A’s cancellation GET for /temperature with Observe: 1, Token 0xAB12, and the same endpoint binding. Remove only A’s registry entry; B with Token 0xCD34 remains. Expiry at 50 seconds did not itself remove either entry.
Watch one subscription from start to end. Track its state. Trace the figure in the parent chapter from registration through successive notifications. Token continuity identifies the observation, while Observe values and freshness rules help reject stale or reordered updates; a transport ACK confirms delivery but does not by itself prove that the application accepted the value. Cancellation or relationship loss ends that state, and re-registration requires reconciliation. That lifecycle frames the sequence-number knowledge check next.
Deregistration
In the worked trace, the explicit request below is sent from 192.0.2.20:61616 to 192.0.2.10:5683 and received at t=55 s. It removes observation A (0xAB12) while B (0xCD34) remains. More generally, cancel the thermostat observation explicitly, reject an unwanted notification, or let the server detect failed delivery. A lost client cannot promise that the server immediately stops sending.
1. Explicit deregistration (GET with Observe: 1):
Client -> Server: GET /temperatureToken: 0xAB12Observe: 1(deregister)
2. RST response to unwanted notification:
Server -> Client: NON 2.05 Content (notification)Client -> Server: RST(stop this observation)
3. Failed delivery or an application lease:
A Confirmable (CON) notification requests an acknowledgement (ACK) and can be retransmitted. A Non-confirmable (NON) notification has no such ACK exchange. Failed CON delivery can trigger observer removal; a server may separately implement a renewable application lease. Max-Age expiry alone does not remove an observer. Match a RST to the notification Message ID, then remove the associated observation.
The protocol flow gives the client-side contract: register with Observe: 0, accept ordered notifications, and leave cleanly with Observe: 1 or RST. The next layer is the server-side contract that remembers each observer without letting stale entries accumulate.
Observer Management Implementation
Server-Side Observer Registry
The registry below retains the thermostat endpoint, Token, resource, and representation-selecting GET options. Its 24-hour timeout is an example application lease. Cleanup occurs when notify_all runs; a quiet resource needs a separate cleanup scheduler. The send and representation-selection methods are placeholders, so this is a registry sketch rather than an executable CoAP server.
from collections import defaultdict
from dataclasses import dataclass
import time
@dataclass
class Observer:
client_addr: object
token: bytes
request_options: dict # Effective GET options that select/cache the representation.
registered_at: float
last_notification: float
timeout: float
class ObserverRegistry:
def __init__(self):
# Map: resource_uri -> list of Observer objects
self.observers = defaultdict(list)
self.observer_timeout = 86400 # 24 hours default
def register_observer(self, resource_uri, client_addr, token, request_options,
lease_seconds=None):
# Include effective Accept and other representation-selecting GET options.
options_snapshot = dict(request_options)
# RFC 7641 §4.1: refresh an existing endpoint/token pair, do not append it twice.
for existing in self.observers[resource_uri]:
if existing.client_addr == client_addr and existing.token == token:
existing.registered_at = time.time()
existing.timeout = lease_seconds or self.observer_timeout
existing.request_options = options_snapshot
return existing
observer = Observer(
client_addr=client_addr,
token=token,
request_options=options_snapshot,
registered_at=time.time(),
last_notification=time.time(),
timeout=lease_seconds or self.observer_timeout # application policy, not Max-Age
)
self.observers[resource_uri].append(observer)
return observer
def notify_all(self, resource_uri):
"""Send notification to all observers of a resource"""
expired = []
for observer in self.observers[resource_uri]:
# Check if observation expired
if time.time() - observer.registered_at > observer.timeout:
expired.append(observer)
continue
# Re-evaluate this observer's selected representation using its GET options.
value, content_format = self.select_representation(
resource_uri, observer.request_options
)
self.send_notification(observer, value, content_format)
# Clean up expired observers
for observer in expired:
self.observers[resource_uri].remove(observer)
def remove_observer(self, resource_uri, client_addr, token):
"""Remove observer on explicit deregistration or RST received"""
self.observers[resource_uri] = [
o for o in self.observers[resource_uri]
if not (o.client_addr == client_addr and o.token == token)
]

Knowledge check · self-test
Knowledge check
Test your understanding.
Notification Rate Limiting
Transfer the thermostat rule to the boiler example below. A change threshold is the minimum difference from the last sent value; a minimum interval limits routine send frequency. The example also has a separate urgent crossing path. These are application policies, not built-in Observe guarantees.
This callback checks only when a new value arrives. It has no timer to send a deferred value or refresh an unchanged representation. A complete implementation must schedule those actions and key per-observer state by endpoint and Token; this sketch keys only by Token.
class RateLimitedResource:
def __init__(self, min_interval=1.0, change_threshold=0.5,
urgent_alarm_threshold=None):
self.min_interval = min_interval # Minimum seconds between notifications
self.change_threshold = change_threshold # Minimum change to trigger notification
self.urgent_alarm_threshold = urgent_alarm_threshold
self.last_notify_time = {} # Per-observer last notification time
self.last_notified_value = {} # Per-observer last sent value
def on_value_change(self, new_value):
now = time.time()
for observer in self.observers:
last_time = self.last_notify_time.get(observer.token, 0)
last_value = self.last_notified_value.get(observer.token, None)
# Routine readings obey both gates; an alarm crossing bypasses the time gate.
routine_notify = (
last_value is None or
(abs(new_value - last_value) >= self.change_threshold and
(now - last_time) >= self.min_interval)
)
urgent_alarm = (
self.urgent_alarm_threshold is not None and
new_value >= self.urgent_alarm_threshold and
(last_value is None or last_value < self.urgent_alarm_threshold)
)
if routine_notify or urgent_alarm:
self.send_notification(observer, new_value)
self.last_notify_time[observer.token] = now
self.last_notified_value[observer.token] = new_value
The rate limiting logic above combines two thresholds to prevent notification storms. For a sensor with value at time , a notification is sent when:
where is the value change and is time since last notification.
For example, a temperature sensor monitoring a boiler room:
- Change threshold:
- Time threshold:
If a routine reading jumps from 80°C to 82°C in 10 seconds:
If the example application configures 82°C as an urgent alarm threshold (not a recommended safety limit), crossing it sends an immediate alarm notification even inside that 60-second interval. Alarm handling is a separate path; routine updates retain the cap.
If temperature drifts slowly from 80.0°C to 80.3°C over 65 seconds:
This dual-threshold policy caps the notification rate and ignores small changes. To bound staleness, schedule a separate refresh before the advertised Max-Age expires.
Checkpoint: Registry Discipline
The boiler last sent 80°C, rises to 82°C after 10 seconds, and uses a 0.5°C change threshold with a 60-second minimum interval. Predict the routine send. Does silence delete its observers?
Answer: The value gate passes but the time gate fails, so this callback sends no routine notification. A separately configured urgent crossing can send. Max-Age expiry makes client data stale; it does not delete the server entries. Schedule refresh and cleanup separately.
Deep Dive: Observe Internals
The thermostat accepts sequence 47, then receives sequence 46 with the same endpoint and Token one second later. How should it use the Observe value?
Use it to reject 46 as older than the accepted 47
Correct! The Observe option value in server responses and notifications is a 24-bit sequence number (0 to 16,777,215). The client compares it with later notifications using RFC 7641 Section 3.4 serial arithmetic; after more than 128 seconds, arrival time can make a notification newer.
An unseen trace has observations C (Token 0x10) and D (Token 0x20), both from client 192.0.2.30:61616 to server 192.0.2.10:5683 for /temperature. C accepts Observe=200, 19.0°C, Max-Age=40 at t=5 s; then receives 202, 20.0°C, Max-Age=20 at t=15 s and 201, 19.5°C, Max-Age=90 at t=18 s. No other responses arrive. At t=40 s the server receives a cancellation GET with Observe=1 and Token 0x20 from that client endpoint. Which result is correct?
C retains 20.0°C, becomes stale at t=35 s, and cancellation removes D only.
Correct. 202 advances from 200, so C accepts 20.0°C and sets its deadline to 15 + 20 = 35 seconds. The older 201 arrives only 3 seconds later and is rejected; its Max-Age cannot renew the accepted value. Token 0x20 at the bound endpoint identifies D for cancellation.
Deep Dive: Observe Sequence Numbers and Ordering
The Observe option value is a sequence number that helps clients detect:
- Out-of-order notifications (UDP doesn’t guarantee ordering)
- Notification freshness (which update is newer)
Client Ordering Rules (RFC 7641 Section 3.4):
def is_notification_fresh(current_seq, new_seq, elapsed_seconds=0):
"""
Determine if new notification is fresher than current.
Handles 24-bit wraparound.
"""
# Sequence numbers are 24-bit (0 to 16,777,215)
MAX_SEQ = (1 << 24) - 1
# Calculate difference handling wraparound
diff = (new_seq - current_seq) % (MAX_SEQ + 1)
# Within 128 s, use the serial half-range (positive diff below 2^23).
# After 128 s, accept by arrival time even if the sequence goes backward.
return elapsed_seconds > 128 or 0 < diff < (1 << 23)
Apply the rule to observation A in the worked trace: sequence 100 arrives at t=10 s with Max-Age 40; 102 at t=20 s with Max-Age 30; and 101 at t=25 s with Max-Age 90. All responses match A’s endpoint binding and Token 0xAB12. The protected sequence illustration below shows the same values. Accepting 102 sets the stale deadline to t=50 s; rejecting 101 leaves that deadline unchanged.
Example scenario:
Notification 1: Observe=100, temp=22.5
Notification 2: Observe=102, temp=23.0 (arrived out of order)
Notification 3: Observe=101, temp=22.8
Client receives: 100 -> 102 -> 101
Client should display: 22.5 -> 23.0 (ignore 101, it's older than 102)
Automatic observer removal triggers:
- RST received: Client sends RST in response to notification
- Application lease: No renewal within a configured server-side lease
- CON notification fails: After 4 retransmissions without ACK
- Resource deleted: Server removes all observers when resource gone
Retransmission behavior for CON notifications:
Server sends CON notification
Wait 2–3 seconds for ACK (RFC default randomization)
Retransmit with same Message ID
Wait 4–6 seconds (exponential backoff)
Retransmit
Wait 8–12 seconds
Retransmit
Wait 16–24 seconds
Final retransmission
Wait 32–48 seconds for ACK; if none arrives, remove observer
Deep internals explain why the same notification stream can survive UDP reordering and unreachable clients. Now apply those mechanics to deployment failures, where restarts, NAT mappings, and stale tokens are the usual reasons Observe looks unreliable.
Edge Cases and Gotchas
Token Reuse After Client Restart
Problem:
- Client registers observation with Token=0x42
- Client crashes and restarts
- Server sends notification with Token=0x42
- Client doesn't recognize token (state lost) -> sends RST
- Server removes observer
Solutions:
- Server MUST remove observer when RST received
- Client should re-register observations after restart
- If persisting observation state, reconcile it with the server and current representation after restart before trusting it
NAT Timeout Issue
Problem:
UDP NAT mapping lifetime varies by network; this example assumes 45 seconds
- Client behind NAT registers observation
- Server tries to push notification 5 minutes later
- NAT mapping expired -> notification never reaches client
Solutions:
- Client sends periodic traffic before its measured NAT mapping expires
- Client sends periodic re-registration (GET with Observe=0)
- Use Max-Age to bound representation freshness; plan notifications or re-registration accordingly
- Evaluate a supported CoAP-over-TCP deployment separately; changing transport does not remove the need for liveness and freshness rules
Continue to the Next Part
Carry this evidence into CoAP Observe: Efficiency and Lifecycle, which begins with Interactive Calculator: NAT Keep-Alive Interval.
Detail notes moved from the chapter
Historical protected source blocks retained for the chapter preservation check.



