Node.js UDP Sockets
UDP sends datagrams rather than a reliable byte stream. A received message can preserve one datagram boundary, but delivery and ordering are not guaranteed by the protocol.
How it works
The loopback fixture sends a single short status packet and waits for its actual receipt with a timeout. It closes both owned sockets in finally. A send callback means the local send operation completed, not that a remote application processed the message. A real device protocol may need sequence numbers, acknowledgements, deduplication and a policy for packets that never arrive.
Run the code
Run this example locally with Node.js. The exact sources are reviewed on Node 22.15.1 and Node 24.22.1 unless a specialised lab states otherwise. Save the complete source as udp.mjs in a separate empty exercises folder and run node udp.mjs. There is no Node.js browser runner on Bhylu; the plain JavaScript playground does not provide these filesystem, server or process APIs.
Download the complete source. Ordinary Node scripts need no Apache, PHP or MySQL service. File examples own their temporary folders; network fixtures use loopback and available temporary ports.
Example
import dgram from 'node:dgram';
import { once } from 'node:events';
const receiver = dgram.createSocket('udp4'),
sender = dgram.createSocket('udp4');
receiver.bind(0, '127.0.0.1');
await once(receiver, 'listening');
try {
const received = once(receiver, 'message', { signal: AbortSignal.timeout(2000) });
await new Promise((resolve, reject) =>
sender.send(
Buffer.from('kit:ready'),
receiver.address().port,
'127.0.0.1',
(error) => (error ? reject(error) : resolve()),
),
);
const [message] = await received;
console.log(message.toString());
} finally {
sender.close();
receiver.close();
}
Expected result
kit:ready
A packet is actually received in this local test. That successful observation does not establish internet reliability, loss rates or delivery guarantees for a deployed device network. Keep the distinction between a demonstrated API call and a protocol promise.
Use the Node.js API reference for the precise version contract.