QA
Intermediate LevelNode JS

How do you send and receive connectionless UDP datagram packets using the node:dgram module?

❓ Interview Prompt: import dgram from "node:dgram"; const server = dgram.createSocket("udp4"); server.on("message", (msg, rinfo) => ...); server.bind(41234);. Fast, connectionless protocol used for DNS, video streaming, and IoT metrics.

The 'node:dgram' module implements connectionless UDP Datagram sockets:

Use Cases:

High-speed metrics collection (StatsD), DNS queries, video streaming, and IoT sensors where speed is prioritized over delivery guarantees.

Code Implementation Example

javascript
import dgram from 'node:dgram';

const server = dgram.createSocket('udp4');
server.on('message', (msg, rinfo) => {
  console.log('Received UDP packet from ${rinfo.address}:${rinfo.port}: ${msg}');
});
server.bind(41234);
💡Creating a UDP datagram socket server using node:dgram.
🎯 Interview Key Takeaway

Use node:dgram for connectionless UDP socket communication in high-speed telemetry and metrics streaming.

#dgram#UDP#Datagrams#Networking#Intermediate
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?