Create a TCP server using the Node.js Net Module

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 6 years ago

In this lesson, you'll learn how to include the built-in node.js 'net' module in your application and create a TCP server. You will learn about the different event listeners used, how to specify specific ports and addresses for your server, and identify where each section of your code is used in the server lifecycle.

You would use the TCP module in scenarios where you want to deal with the raw TCP data yourself- for instance, creating your own messaging platform over TCP.

[00:02] The node net module provides an asynchronous wrapper for the network and we include that with a require statement. With that included, we can create a server object, which has a callback with a parameter of C.

[00:27] Then, right away, we want to write out, to the console, a message that a client has connected.

[00:33] The create server uses event listeners to control its operation, so we need to listen for those. The first one that we want to listen for is the data listener. When we receive that we're going to have another call back that takes a parameter D and it's going to write out data received. Then it will also write out the data that we received. Since that comes in as a buffer, we're going to use the two-string method.

[01:07] The other listener we want to listen for is the end listener. Again, taking a call back and it's going to write out, to the console, that our client has disconnected.

[01:35] That's all we need to do to get a basic server up and running, except to actually turn it on. To do that, we're going to use the server.listen. That takes a couple of parameters to make it work. The first one is the TCP port that it's going to listen on. We're going to use port 3000.

[01:54] Next is an optional parameter, which is the IP address that you want it to listen on. You can provide a specific IP address here or you can provide 0000and that's going to cause it to bind to all interfaces on this server. That also happens to be your default, so if you provide nothing at all, it's going to bind on all interfaces.

[02:18] Another thing you can here is access environment variables on your server. For instance, if you had an environment variable called address, you could grab that here and it would bind to whatever IP address was stored in that environment variable.

[02:34] For our demo, we can just accept the defaults and then we can call a call back and that call back is just going to write out to the console that the server has started. That's enough to get us going.

[02:54] If we save that we can actually start our server up by calling the node command and then providing the name of the file we were just editing. You see, right away, we get server started printed out to the console, which was triggered in our listen method here.

[03:13] Now let's use Telnet to connect to local host on port 3000. As soon as we connect, we get the client connected message, which was fired from right here. Then as we type in data, that data is received and printed out to the console, and that was a result of receiving data through the data listener.

[03:44] Then finally, if we exit our Telnet session, we get our client disconnected message, which was fired from the end event listener.