NodeJS UDP server

Both TCP & UDP exist in layer four transport layer OSI Model in this article we will the following:

  • UDP definition
  • UDP Advantages
  • UDP Disadvantages
  • When we use UDP
  • Applications of UDP

Do not miss this article NodeJS TCP Server which explains TCP definition and usage also simple Nodejs Script which create a TCP server listening for the incoming messages from client.

UDP

Stands for (User Datagram Protocol) is a communications protocol that is primarily used for establishing low-latency and loss-tolerating connections between applications on the internet. It speeds up transmissions by enabling the transfer of data before an agreement is provided by the receiving party.

Advantages

  • Smaller packets because there is no acknowledgement, no guaranteed delivery, and connection less so no additional headers added.
  • Less bandwidth because packets size are small.
  • Faster than TCP no wait for ordering packets or congestion control.
  • Stateless Means if the client sending information to the server and the server down for a time. When server become up again the client can continue sending information process.

Disadvantages of UDP

  • No Acknowledgement the server send data and do not care this data received or not.
  • No Guaranteed delivery.
  • Connection less there is no need to establish connection prior to data transfer. No physical connection the client does not know the server and the server does not know the client.
  • No Congestion Control. does not care when sending packets there are traffic or no traffic.
  • No Ordered Packets.
  • Security Because of Connection less the server does not know client any one know the port that is opened can send any stuff of data

When we use UDP?

  • Reduce the requirement of computer resources.
  • When using the Multicast or Broadcast to transfer.
  • The transmission of Real-time packets, mainly in multimedia applications.

Aplications of UDP

  • Normally used for real time applications
  • UDP is used for some routing update protocols like RIP(Routing Information Protocol).
  • Following implementations uses UDP as a transport layer protocol (NTP,DNS,DHCP,NNP,TFTP, RTSP, RIP, OSPF).
  • It is suitable protocol for multicasting.

Implementation

we will develop a simple UDP server in nodejs which listen on port 6000 and receives messages then displaying these messages in the console.

create a new directory UDP and inside this directory create a new file app.js

UDP/app.js

const datagram = require("dgram");
const socket = datagram.createSocket("udp4");
//listen incomming messages
socket.on("message",(msg,receInfo)=>{
    //display msg and sender information ip:port
    console.log(`I Have Already Received: ${msg} from ${receInfo.address}:${receInfo.port}`);
});
socket.bind(6000);

run this application by typing this command node app.js and open a new terminal then type this command echo "New Message from client" | nc -u 127.0.0.1 6000

The result put will be like these images below

UDP Client

NodeJS UDP Server