Socket
objects are created on both the socket server and the socket client and allow data to be written and read back and forth between them. The Socket
object implements the Duplex
stream, so it provides all the functionality that Writable
and Readable
streams provide. For example, you can use the write()
method to stream writes of data to the server or client and a data
event handler to stream data from the server or client.
On the socket client, the Socket
object is created internally when you call net.connect()
or net.createConnection()
. This object is intended to represent the socket connection to the server. You use the Socket
object to monitor the connection, send data to the server, and handle the response from the server. There is no explicit client object in the Node.js net
module because the Socket
object acts as the full client, allowing you to send/receive data and terminate the connection.
On the socket server, the Socket
object is created when a client connects to the server and is passed to the connection event handler. This object is intended to represent the socket connection to the client. On the server, you use the Socket
object to monitor the client connection as well as send and receive data to and from the client.
To create a Socket
object, you use one of the following methods:
net.connect(options, [connectionListener])
net.createConnection(options, [connectionListener])
net.connect(port, [host], [connectListener])
net.createConnection(port, [host], [connectListener])
net.connect(path, [connectListener])
net.createConnection(path, [connectListener])
All the calls will return a Socket
object; the only difference is the first parameters they accept. The final parameter for all of them is a callback function that is executed when a connection is opened to the server. Notice that for each method, there is a net.connect()
form and a net.createConnection()
form. These work exactly the same way.
The first way to create a Socket
object is to pass an options
parameter, which is an object that contains properties that define the socket connection. Table 8.1 lists the properties that can be specified when creating the Socket
object. The second method accepts port
and host
values, described in Table 8.1, as direct parameters. The third option accepts a path
parameter that specifies a file system location that is a Unix socket to use when creating the Socket
object.
Once the Socket
object is created, it provides several events that are emitted during the life cycle of the connection to the server. For example, the connect
event is triggered when the socket connects, the data
event is emitted when there is data in the Readable
stream ready to be read, and the close
event is emitted when the connection to the server is closed. As you implement your socket server, you can register callbacks to be executed when these events are emitted to handle opening and closing the socket, reading and writing data, etc. Table 8.2 lists the events that can be triggered on Socket
objects.
The Socket
object also includes several methods that allow you to do things like read from and write to the socket as well as pause or end data flow. Many of these are inherited from the Duplex
stream objects, so they should be familiar to you (see Chapter 5, “Handling Data I/O in Node.js”). Table 8.3 lists the methods available on Socket
objects.
Socket
objects also provide several properties that you can access to get information about the object—for example, the address and port the socket is communicating on, the amount of data being written, and the buffer size. Table 8.4 lists the properties available on Socket
objects.
To illustrate flowing data across a Socket
object, the following code shows the basics of implementing the Socket
object on a client:
var net = require('net'),
var client = net.connect({port: 8107, host:'localhost'}, function() {
console.log('Client connected'),
client.write('Some Data
'),
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('Client disconnected'),
});
Notice that the net.connect()
method is called using an optional object containing a port
and host
attribute. The connect
callback function logs a message and then writes some data out to the server. To handle data coming back from the server, the on.data()
event handler is implemented. To handle the closure of the socket, the on('end')
event handler is implemented.