Skip to content

CoAP Block-Wise Transfer Fails — Complete Guide

DodaTech Updated 2026-06-26 1 min read

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

The Problem

Large CoAP payloads fail to transfer completely using Block-wise transfer.

Quick Fix

Wrong

res.end(largePayload)  # Exceeds UDP datagram size (64 KB)
Response truncated at the UDP datagram boundary.
const coap = require('coap')

// Server handles block-wise
const server = coap.createServer()

server.on('request', (req, res) => {
  // Large response (e.g., firmware image)
  const firmware = getFirmwareBinary()
  
  // CoAP library handles Block2 automatically
  // Client MUST request with Block2 option
  res.setOption('Block2', [new Buffer([0x00])])
  res.code = '2.05'
  res.end(firmware)
})

server.listen(5683)

// Client requests with block-wise
const req = coap.request({
  pathname: '/firmware',
  block2: { szx: 2 }  // Request 1024-byte blocks
})

let received = Buffer.alloc(0)

req.on('response', (res) => {
  received = Buffer.concat([received, res.payload])
  
  if (res.code === '2.05' && res.headers['block2'].more) {
    // Request next block
    req.emit('request', req)
  } else {
    console.log('Firmware received:', received.length, 'bytes')
  }
})
Large firmware (e.g., 100 KB) transferred in 1 KB blocks.

Prevention

CoAP Block-wise transfer (RFC 7959) splits large payloads into 2^SZX byte blocks (16, 32, 64, 128, 256, 512, 1024). Block1: request body (POST/PUT). Block2: response body (GET). The MORE flag indicates more blocks follow. MAX_BLOCK_SZX is typically 6 (1024 bytes).

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

FAQ

### What is SZX?

Block size exponent: SZX 0 = 16 bytes, 1 = 32, 2 = 64, 3 = 128, 4 = 256, 5 = 512, 6 = 1024. Max: 1024 bytes per block.

How does the client know there are more blocks?

The Block2 option has a MORE (M) bit. If M=1, it's not the last block. The client requests the next block by incrementing the NUM field.

What is Block1 vs Block2?

Block1 controls request body transfer (POST/PUT). Block2 controls response body transfer (response to GET). Both follow the same mechanism.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro