Managing Protocols with NodeJS

Managing Protocols with NodeJS

ยท

3 min read

So, I was reading about decentralized apps with a peer-to-peer protocol and while searching deep inside Dapps and P2PP, it struck in my mind, "How can we manage to control the type of protocol we use in our web apps?", so I searched about it and decided to write about it.

There are different protocols on which different things run such as TCP, IP, UDP, P2P, FTP, ICMP, etc. And each one of them has a different purpose. But when we create our backend using MERN how do we control these protocols?

For this purpose, we use NodeJS. We can install different packages using NPM to set our protocols and here I will be showing how to set TCP and UDP protocols.

What are TCP and UDP protocols?

Transmission Control Protocol (TCP) is used to make reliable network communications. Let's say you are transferring money to another place, when you click on send button there comes a message saying the transaction is successful. Hence, in TCP a message is received everytime a request is sent making sure it reached the destination

Whereas a User Datagram Protocol (UDP) is used to make unreliable network communications. This protocol is used in real-time communications like video/voice calls, real-time gaming, etc. Here we don't need to make sure if the message reached the destination or not.

Let's take an example here if you are talking on a video call and you said "I am going to the market" and the message reached was "I going to the market" and the word "am" is dropped then we don't want our app to send us a message that the word "am" is not delivered. So in these cases, UDP is used.

Setting up TCP connection

Following is the code to set up a TCP connection. image.png

Press F5 and select nodeJS as debugger and this will be shown in the console.

image.png

Now we have to enable telnet first, go to control panel and search for "Turn on and off windows features" and enable telnet and write the following command in command prompt and press enter.

image.png

This should appear on your command prompt screen :

image.png

Now as this is a TCP connection when you make a request you receive the message back, as we can see in the following image when I typed "hi" the same message was printed on my console.

image.png

Setting up UDP connection

Following is the code to set up a UDP connection and add a stopper to line 5.

image.png

Run the debugger as before but this time you must install Netcat in your machine.

You can download Netcat from here but you need to turn off your windows defender first, but don't worry it's safe and I downloaded it from the same site.

After downloading, add its path to the command prompt and write this command in the command prompt, and press Enter.

image.png

Hover on the word "msg" and you will see something like this:

image.png

Here as you can see we didn't receive any message and the command closed itself after sending the message unlike TCP and as we hover on "msg" we can see those numbers, those are the ASCII characters of h, i, space, carriage return, and line feed.

And this is my friends, how we set up our protocols using NodeJS. I hope you found it useful. Thank you very much for giving me your precious time, I hope you learned something new today ๐Ÿ˜€

ย