Comprehensive Guide to UDP/Datagram Sockets in Node.js

The node:dgram module provides a full implementation of UDP (User Datagram Protocol) sockets in Node.js. . UDP is a lightweight, connectionless protocol ideal for real‑time applications such as VoIP, gaming, telemetry, IoT, and multicast/broadcast systems. This module exposes APIs for creating sockets, sending and receiving datagrams, joining multicast groups, managing buffer sizes, and controlling low‑level socket behavior.

UDP / Datagramdgram.Socketsocket.bind / socket.sendMulticast / BroadcastTTL / Buffer sizes

~2 min read · Updated Nov 2, 2026

1. Introduction


The node:dgram module enables UDP networking in Node.js. It supports IPv4 and IPv6, multicast, broadcast, and both connectionless and connected UDP modes.


2. Creating a UDP Socket


const dgram = require('node:dgram');
const server = dgram.createSocket('udp4');

3. Core Events


3.1 'error'


Emitted when any socket error occurs.

3.2 'message'


Emitted when a datagram is received.

3.3 'listening'


Emitted when the socket is bound and ready.

3.4 'close'


Emitted after socket.close() is called.

3.5 'connect'


Emitted after a successful socket.connect() call.


4. Binding a Socket


Binding makes the socket ready to receive datagrams.

socket.bind(41234);

5. Sending Datagrams


UDP supports sending Buffers, strings, TypedArrays, DataViews, or arrays of Buffers.


5.1 Connectionless Send


client.send(message, 41234, 'localhost');

5.2 Connected UDP


client.connect(41234, 'localhost', () => {
  client.send(message);
});

6. Multicast Support


6.1 Joining a Multicast Group


socket.addMembership('224.0.0.114');

6.2 Source‑Specific Multicast


socket.addSourceSpecificMembership(source, group);

6.3 Leaving Multicast Groups


socket.dropMembership(address);
socket.dropSourceSpecificMembership(source, group);

7. Broadcast Support


socket.setBroadcast(true);

8. Buffer and Queue Management


  • socket.getRecvBufferSize()
  • socket.getSendBufferSize()
  • socket.setRecvBufferSize(size)
  • socket.setSendBufferSize(size)
  • socket.getSendQueueSize()
  • socket.getSendQueueCount()

9. TTL and Multicast TTL


  • socket.setTTL(ttl): For unicast.
  • socket.setMulticastTTL(ttl): For multicast.

10. Multicast Interface Selection


Specify outgoing interface for multicast traffic.

socket.setMulticastInterface('10.0.0.2'); // IPv4
socket.setMulticastInterface('::%eth0');  // IPv6

11. Unref and Ref


Control whether the socket keeps the Node.js process alive.

socket.unref();
socket.ref();

12. Blocklists


Block inbound or outbound traffic to specific IPs or ranges.

receiveBlockList: new net.BlockList()
sendBlockList: new net.BlockList()

13. AbortSignal Support


Abort a socket using AbortController.

const controller = new AbortController();
const server = dgram.createSocket({ type: 'udp4', signal: controller.signal });
controller.abort();

14. Datagram Size Limitations


UDP datagrams are limited by MTU and protocol constraints:

  • Max theoretical payload: ~65 KB
  • IPv4 recommended MTU: 576 bytes
  • IPv6 minimum MTU: 1280 bytes

15. dgram.createSocket()


Supports options such as:

  • type: 'udp4' or 'udp6'
  • reuseAddr / reusePort
  • ipv6Only
  • recvBufferSize / sendBufferSize
  • lookup custom DNS resolver
  • signal AbortSignal
  • receiveBlockList / sendBlockList

Conclusion


The node:dgram module provides a complete, low‑level API for building high‑performance UDP applications in Node.js. With support for multicast, broadcast, connected UDP, buffer management, TTL control, blocklists, and AbortSignal integration, it is ideal for real‑time systems, distributed applications, and network‑level tooling.


Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

Comprehensive Guide to Trace Events and TTY in Node.js

Node.js provides two powerful low‑level modules for diagnostics and terminal interaction: node:trace_events — a high‑resolution tracing system that captures internal activity from V8, Node.js core, and userland code. It is essential for profiling, performance analysis, and deep debugging. node:tty — a module that exposes TTY (terminal) interfaces, enabling advanced CLI tools, raw input handling, cursor control, color detection, and terminal resizing. This guide explains both modules in a clean, structured, and practical way.

Continue