Handling duplex streams over TCP
Video: Handling duplex streams over TCPWhile 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.
Viewers: in countries Watching now:
- Why use Node.js?
- Installing Node.js
- Understanding the event loop
- Initializing Node.js projects
- Creating modules with getters and setters
- Starting Express applications
- Testing your code
- Working with sessions and databases
- Building command-line tools
- Emitting events and attaching listeners
- Controlling readable streams
Handling duplex streams over TCP
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.
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
- Q: How can I install the new version of the Express command-line tool?
- A: Newer versions of the Express command-line tool can be installed using the command "npm install -g express-generator." But before doing this, you should first remove any older version of the module, using the command "npm uninstall -g express."
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.