Skip to Content
πŸŽ‰ Welcome to my notes πŸŽ‰
Node.jsNetworkingπŸš€ dgram Module - Fast & Fire-and-Forget UDP

πŸš€ dgram Module - Fast & Fire-and-Forget UDP

The dgram module provides an implementation of UDP (User Datagram Protocol) sockets.

UDP is a connectionless protocol. It sends packets of data (datagrams) without establishing a persistent connection and without any guarantee of delivery or order.

  • Analogy: Sending a postcard. You send it off without knowing for sure if or when it will arrive, or if multiple postcards will arrive in the order you sent them.

The trade-off for this lack of reliability is speed. UDP has very little overhead, making it much faster than TCP.

  • Use Cases: DNS, video and audio streaming, online gaming, and other applications where speed is more critical than perfect reliability.

βš™οΈ The Core dgram Workflow

The dgram module allows you to create UDP sockets to send and receive datagrams.

  • Creating a Socket: You start by calling dgram.createSocket('udp4') for IPv4 communication.
  • The Server: A UDP server doesn’t have β€œconnections.” Instead, it binds to a specific port using socket.bind(port) and listens for the 'message' event. This event fires whenever a datagram arrives from any client.
  • The Client: A UDP client doesn’t β€œconnect.” It simply uses socket.send(message, port, address) to fire off a datagram to a specific server address and port. The destination must be specified every time a message is sent.

πŸ’» Practical Example: A UDP Client & Server

Here is a simple client-server setup. The client will send a message, and the server will log it and send a confirmation back.

udp-server.js
const dgram = require("dgram"); // 1. Create a socket object. 'udp4' specifies IPv4. const server = dgram.createSocket("udp4"); const PORT = 41234; // 2. Fired when an error occurs. server.on("error", (err) => { console.log(`Server error:\n${err.stack}`); server.close(); }); // 3. Fired when a message arrives. // 'msg' is a Buffer, 'rinfo' has remote address info. server.on("message", (msg, rinfo) => { console.log(`Server got: ${msg} from ${rinfo.address}:${rinfo.port}`); // Send a response back to the client. const response = Buffer.from("Message Received"); server.send(response, rinfo.port, rinfo.address, (err) => { if (err) { console.log("Error sending response:", err); } else { console.log("Response sent successfully."); } }); }); // 4. Fired when the socket starts listening. server.on("listening", () => { const address = server.address(); console.log(`Server listening on ${address.address}:${address.port}`); }); // 5. Bind the server to a port. server.bind(PORT);
udp-client.js
const dgram = require("dgram"); const client = dgram.createSocket("udp4"); const message = Buffer.from("Hello, UDP Server!"); const SERVER_PORT = 41234; const SERVER_HOST = "127.0.0.1"; // localhost // 1. Send the message. client.send(message, SERVER_PORT, SERVER_HOST, (err) => { if (err) { console.log("Error sending message:", err); client.close(); } else { console.log("Message sent to server."); } }); // 2. Fired when a message (the response) arrives. client.on("message", (msg, rinfo) => { console.log(`Client got: ${msg} from ${rinfo.address}:${rinfo.port}`); client.close(); // Close the client after receiving the response. });
Last updated on