Skip to content

Coap Client

DodaTech 1 min read

In this tutorial, you'll learn about CoAP Client Request Timeout. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

CoAP client GET or POST requests keep timing out.

Quick Fix

Wrong

coap.request('coap://localhost/temp')  # Default timeout 2s
Request times out if server response takes longer than 2 seconds.
const coap = require('coap')

const req = coap.request({
  hostname: 'sensor-node.local',
  port: 5683,
  pathname: '/temperature',
  method: 'GET',
  observe: false,
  confirmable: true,  // CON message for reliability
  retry: 3,           // Number of retransmissions
  reset: true         // Reset RTO on each retry
})

// Set custom timeout (ms)
req.setOption('Block2', [new Buffer([0x00])])

req.on('response', (res) => {
  console.log('Response code:', res.code)
  console.log('Payload:', res.payload.toString())
})

req.on('timeout', () => {
  console.log('Request timed out after retries')
})

req.end()
Response received from server (or timeout after all retries).

Prevention

CoAP over UDP has built-in reliability (CON message with retransmission). Default retry count: 4, timeout starts at 2s, doubles each retry. Use confirmable (CON) for important requests. Non-confirmable (NON) is fire-and-forget. Implement CoAP over DTLS for security.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### What is CON vs NON?

CON (Confirmable) requires an ACK from the server with retransmission on loss. NON (Non-Confirmable) is fire-and-forget with no ACK.

How does CoAP handle retransmission?

Exponential backoff: start 2s, multiply by 1.5 each time, max 4 retries (total ~12s). Configurable in client libraries.

Can CoAP use TCP?

CoAP typically uses UDP. CoAP over TCP is defined in RFC 8323 but not widely adopted. Use MQTT if TCP reliability is needed.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro