UDP
Learn how the UDP module works in Node.js.
We'll cover the following
UDP
Alongside TCP,
The dgram
module
The dgram
module provides an implementation of UDP Datagram sockets. These sockets are required for UDP communication.
const dgram = require("dgram"); const server = dgram.createSocket("udp4"); const port = 3500; server.on("message", (data, rinfo) => { console.log(`Msg from client at port: ${rinfo.port}: ${data}`); server.send("Hello from server", rinfo.port, "localhost"); }); server.on("listening", function () { console.log("Server is listening on port", port); }); server.on("close", function (err) { if (err) { console.log("Client disconnected due to error"); } else { console.log("Client disconnected"); } server.close(); }); server.bind(port);
The server-side
- We import the
dgram
module on line 1. - The
createSocket
method on line 3 creates a UDP socket. Atype
must be passed; we are usingudp4
in our case. The socket returned is an object of theEventEmitter
class. This allows us to use theon
method to handle different events. - Declaring our
port
early in the code and using the variable name will make changing it later on easier. We have set theport
to 3500 on line 4. - The
message
event is fired when we receive a message from another socket. It returns thedata
and anrinfo
object. We are outputting the data on line 7.rinfo
has a lot of useful attributes; we userinfo.port
to send a message back to our client. - We send data on the socket by using the
send
method. We pass it a string of our choice, the port, and the of the recipient. This is done on line 8.IP address localhost in our case - The
listening
event is fired when thesocket
object starts to listen for incoming connections. We are using this event to output a string to let us know that the server is up. This is done on line 11. - The
close
event is fired when the socket is fully closed. If the socket closes due to an error, anError
object is returned. We are handling this event on line 15. - Finally, to start the server, we use the
bind()
method. This fires the listening event we wrote earlier. It is passed a port number. Now our server is live and waiting for new connections.
The client-side
- Our client-side looks very similar to the server-side since we are using the
dgram.createSocket
method again to make our socket. - As you can see, once our
socket
object is created, we can start sending data to our server right away. We do not need to connect to the server as we did in our TCP example. We are using thesend
method as we did in our server; however, here we are also checking for errors. - The only way to know for sure that the datagram has been sent is by using a callback. In case of an error, it will be passed as the first argument to the callback. If a callback is not given, the error is emitted as an
error
event on the socket object. - The
data
andend
methods are used in a similar manner as we have used on the server-side. Theend
method is similar to theclose
method in terms of functionality.
Let’s make them talk
Using the readline
module, once again, we can achieve the same functionality as we did with our TCP system. Let’s see how that might look with UDP.
const dgram = require("dgram"); const readline = require("readline"); const server = dgram.createSocket("udp4"); const port = 3500; var rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: "", }); let clientSocket = 0; server.on("message", (data, rinfo) => { console.log(`Msg from client at port: ${rinfo.port}: ${data}`); clientSocket = rinfo.port; }); server.on("listening", function () { console.log("Server is listening on port", port); }); server.on("close", function (err) { if (err) { console.log("Client disconnected due to error"); } else { console.log("Client disconnected"); } server.close(); }); server.bind(port); rl.prompt(); rl.on("line", function (line) { server.send(line, clientSocket, "localhost"); rl.prompt(); });
The server-side
- We import the
readline
module on line 2. - We use the
createInterface
method to use our streams. - We define a variable
clientSocket
on line 13. This is initially set to0
. We will set this to the port number that we use to communicate with the client once it connects. - Once the client connects, we assign
clientSocket
the value ofrinfo.port
on line 17. - We use the
on
method to add a listener for theline
event. However, we use thesend
method instead of thewrite
method we used for TCP. When sending on the socket, we use theclientSocket
variable we set earlier.
The client-side
We create an interface and use the same methods as we used in server.js
to send text from the console to the server.
Repetition?
This lesson may seem very similar to the previous lesson. You might be wondering if it was the same lesson again. Both TCP and UDP can be implemented in a similar way in Node.js. We did this so you can better understand the slight differences between the two. TCP takes a connection-oriented approach and ensures that packets are sent and received in order, unlike UDP, which takes a connectionless approach, offering no guarantee that the sent packets will be received on the other end. UDP, however, is often faster than TCP and is used in services like video calls and online gaming, where it is necessary to send packets as soon as possible, even if a few are dropped along the way.
Get hands-on with 1300+ tech skills courses.