Net
Understand the workings of the net module in Node.js.
We'll cover the following
TCP
One of the most common transport layer protocols is
net
The net
module provides an asynchronous network API for creating stream-based TCP or
var net = require("net"); var server = net.createServer(); const port = 3500; server.on("connection", function (socket) { console.log("Client connected from", socket.remoteAddress, socket.remotePort); socket.write("Hello from the server!"); socket.on("data", function (data) { console.log("Msg from client:", data.toString()); }); socket.on("close", function (err) { if (err) { console.log("Client disconnected due to error"); } else { console.log("Client disconnected"); } }); }); server.on("listening", function () { console.log("Server is listening on port", port); }); server.listen(port);
This simple TCP server presents a lot of useful methods of the net
module. Let’s explore them further.
The server-side
- We import the
net
module on line 1. - The
createServer
method on line 3, as the name suggests, creates a server object. This server is an object of theEventEmitter
class; hence, we can use the.on
method to handle different events. - Declaring our
port
early in the code and using the variable name will make changing later on easier. We have set theport
to 3500 on line 4. - The most important event that can be raised is the
connection
event. This event fires when a new connection to the server is made. As you can see on line 6, this event returns an object that we namesocket
. Thissocket
object is an instance ofnet.Socket
. - The
net.Socket
also belongs to theEventEmitter
class. It also has a few methods and events that can come in handy for creating a server. - The
remoteAddress
andremotePort
are two attributes of thenet.Socket
object that we use to output the IP address and the port of the incoming connection on line 7. - We can send data on the socket by using the
write
method. We pass it a string of our choice, and it is sent to the client. This is done on line 8. - The
data
event is fired when data from a client is received. We are outputting the data on line 10. - The
close
event is fired when a socket is fully closed. This indicates that communication has ended. If the socket close due to an error, thehadError
boolean is set to true. We are outputting both cases on line 16 and line 18. - For the
server
object, thelistening
event is fired when the server 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 23. - Finally, to start the server, we use the
.listen()
method. It is passed a port number. Now our server is live and waiting for new connections.
The client-side
- Looking from the other side, the client-side, another useful method is the
net.connect()
method. We pass it the port number that our server is listening on, 3500 in our case. This method also returns anet.Socket
object like ourconnection
event did on the server-side. This is done on line 8. - 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
Now that our client and server are able to connect with each other, we can use the readline
module to send some text from one console to the other over the TCP socket. Let’s see how that can be done.
const net = require("net"); const readline = require("readline"); var server = net.createServer(); const port = 3500; var rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: "", }); server.on("connection", function (socket) { console.log("Client connected from", socket.remoteAddress, socket.remotePort); socket.write("Hello from the server!"); socket.on("data", function (data) { console.log("Msg from client:", data.toString()); }); rl.prompt(); rl.on("line", function (line) { socket.write(line); rl.prompt(); }); socket.on("close", function (err) { if (err) { console.log("Client disconnected due to error"); } else { console.log("Client disconnected"); } }); }); server.on("listening", function () { console.log("Server is listening on port", port); }); server.listen(port);
The server-side
- We import the
readline
module on line 2. - We use the
createInterface
method to use our streams. - Instead of using
setPrompt
, we set it while creating our interface. - We have set it to be
''
, which is just an empty string. This way, the prompt does not interfere with the incoming text from the socket. This is done on line 10. - We use the
on
method to assign a listener to theline
event. The listener function callback writes the data from the input stream to the socket. This is done on line 24. We then use theprompt
method again to ensure that we are ready to read data from the input stream again.
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.
Get hands-on with 1300+ tech skills courses.