Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
While some streams are exclusively writable and some are exclusively readable, there are others that can be both read from and written to. These are known as duplex streams. In this video we'll read from and write to a duplex stream. Let's go to the exercise files. Go to chapter nine and video five. And then copy tcp.js to the desktop. Now, let's take a look at the code. On line one, we're requiring the net module. This is another core module that comes with node, so there's nothing we have to install.
We're also are requiring the File System module. Next, we're creating a server by using the Create Server method of the Net module. It takes a call-back function that controls how the server works. On line six, we're creating a writable stream to eli.log. Then on line eight, we're logging out that a connection has been established. Next on line ten, we're defining a listener for the end event. When the end event happens, we're going to log to the console that the connection ended.
On lines 14 through 16, we're going to write three lines to the TCP connection. It's a duplex stream, so we can write to it or read from it. Finally, on line 18 we have an interesting piece of code. We're reading everything from the connection then piping it back into the same connection. So this way we're using the connection as a readable and a writable stream at the same time. Now furthermore, we're also piping the data into the writable log file stream.
So this one line of code is piping all the data back into the same source stream and then into a separate stream. Finally, down on line 22, we start this server by calling the listen method It takes the port number as the first argument and then a callback function that executes as soon as the server is ready. And when that happens, we're just logging a message to the console. So let's try this code out. We're going to need to open two terminal windows for this. In the first terminal window, we're going to run the code. So change directory to the Desktop.
That's with a capital D, and then type in node > space > tcp.js, so it's now telling us that the server is ready on port seven, seven, seven, seven. Now, you can either open up a new window or a new tab. I prefer new tabs so I can just switch back and forth like this, so now we're going to connect to this TCP server. We are going to use the telnet command, so I am going to type in telnet, and then a space, local host, and then another space, and then seven, seven, seven, seven as the port number.
So now I am going to type a message, and it's going to echo it back to me, and then it's also going to log it. It tells me that the escape character is control end bracket. So I'll just press that now. And then, I'll type in quit. So back here, it logged that the connection was established when we connected. And it also logged that the connection ended when we disconnected. I'm going to press ctrl + c here. And now, let's take a look at the eli.log file. This got created for us automatically. And this log file has our message.
The pipes read the data from the connection and then wrote it back out to the connection and then finally wrote it to this log file. Data can be read from and written to duplex streams. Duplex streams can pipe data back on to themselves as well as pipe to multiple streams at once. In the next chapter, we'll look at ways you can further your node developing skills.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101314 Viewers
61 Video lessons · 88071 Viewers
71 Video lessons · 71925 Viewers
56 Video lessons · 103750 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.