UDP services
Build lightweight request/response or telemetry protocols where low overhead matters.
Node.js · Module
node:dgram provides UDP datagram sockets. Create a socket, bind it to receive message events, send datagrams, and close it when finished.
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.
import dgram from 'node:dgram';
const socket = dgram.createSocket(type[, callback]);| Name | Type | Description |
|---|---|---|
type | string | Address family and socket configuration; commonly udp4 or udp6. |
callback | function | Optional callback attached to the socket's message event. |
port/address | number/string | Local port and optional address used when binding the socket. |
A dgram.Socket object from dgram.createSocket(); socket.address() returns local address information including the address and port.
udp4 or udp6 when creating the socket.dgram.createSocket() rather than constructing dgram.Socket directly.send().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.
Build lightweight request/response or telemetry protocols where low overhead matters.
Discover devices or services on a local network using broadcast or multicast.
Send metrics, logs, or heartbeats without maintaining a stream connection.
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.
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.
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.Socketdgram.createSocket()socket.bind()socket.send()socket.close()socket.address()