Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Module

node:dgram

node:dgram provides UDP datagram sockets. Create a socket, bind it to receive message events, send datagrams, and close it when finished.

How it works

The node:dgram module creates UDP sockets using createSocket(). A socket can bind to an address and port to receive datagrams, emit message events with the payload and sender information, and send datagrams to a destination; sockets are closed when communication ends.

Syntax

import dgram from 'node:dgram';
const socket = dgram.createSocket(type[, callback]);

Parameters

NameTypeDescription
typestringAddress family and socket configuration; commonly udp4 or udp6.
callbackfunctionOptional callback attached to the socket's message event.
port/addressnumber/stringLocal port and optional address used when binding the socket.

Returns

A dgram.Socket object from dgram.createSocket(); socket.address() returns local address information including the address and port.

Key points

  • This module exposes UDP datagram networking for Node.js applications.
  • Use the factory function to obtain a socket instance.
  • Choose udp4 or udp6 when creating the socket.
  • Binding starts reception on the selected local endpoint.
  • The module is intended for datagram-based, connectionless communication rather than stream-oriented sockets.

Common mistakes

  • When sending through an unconnected socket, provide both the destination port and address.
  • Create sockets with dgram.createSocket() rather than constructing dgram.Socket directly.
  • For a connected socket, do not pass destination port and address to send().

Example: Send and receive a UDP datagram

import dgram from 'node:dgram';

const server = dgram.createSocket('udp4');
server.on('message', (message, remote) => {
  console.log(`${remote.address}:${remote.port} ${message}`);
  server.close();
});
server.bind(41234, '127.0.0.1');

const client = dgram.createSocket('udp4');
client.send('hello', 41234, '127.0.0.1', (error) => {
  if (error) throw error;
  client.close();
});

Creates a UDP server, receives one datagram, then sends a message to it and closes both sockets.

When to use it

UDP services

Build lightweight request/response or telemetry protocols where low overhead matters.

Network discovery

Discover devices or services on a local network using broadcast or multicast.

Datagram telemetry

Send metrics, logs, or heartbeats without maintaining a stream connection.

Interview questions

QWhat is the purpose of the node:dgram module?

node:dgram provides UDP datagram sockets. Use dgram.createSocket('udp4') or 'udp6', bind a socket to receive messages, and use send() to transmit datagrams.

Follow-up: How does sending differ for connected and connectionless sockets? A connectionless socket needs destination port and address; a connected socket uses its configured remote endpoint and must not receive those arguments.

QHow does a UDP socket receive data?

The message event fires when a datagram arrives. Its callback receives the message payload and an object containing information about the sender.

Follow-up: How can a receiver determine where the datagram came from? Read the sender information supplied in the event's remote-address object, including its address and port.

QHow are the lifecycle operations performed?

Call socket.bind() to begin listening for datagrams. The socket can then emit incoming messages, and socket.close() stops listening and closes the underlying socket.

Follow-up: What happens if no explicit local endpoint is supplied? Binding can use the all-interfaces address and a port selected by the system; the resulting endpoint can be inspected with socket.address().

  • dgram.Socket
  • dgram.createSocket()
  • socket.bind()
  • socket.send()
  • socket.close()
  • socket.address()

Source: Dgram.Socket. Checked 2026-10-15.