Net module method streaming types in node.js

The tutorial provides the node.js Net module details. The node.js Net module provides the developers to write Asynchronous API streaming using sockets in node.js for integrating client and server applications or chat applications.

Node.js Net streaming types

  • Asynchronous API Streaming using TCP or IPC Servers (net.createServer)
  • Asynchronous API Streaming using Clients (net.createConnection)

Let’s discuss the available classes with methods which can be used for Net module

Class – net.server

The Class net.server is used to create TCP or IPC Server. It includes the below arguments

<object> – net.createServer([options][, connectionListener])

connectionListener <function> – sets the connection ‘event’

Return <net.server> where net.server supports the below given eventEmitter

net.createServer([options][, connectionListener])

  • options allowHalfOpen – Default value is false , depicts if half-opened TCP connections are allowed or not.
  • pauseOnConnect – Default value is false , depicts if the socket should be paused on incoming connections or not.
  • connectionListener – sets the listener for the ‘connection’ event.
    Returns: net.server
net.server eventEmitter Namenet.server eventEmitter Description
Event:‘close’enables that the server is closed. This event will not enable
if connection exists
Event:‘connection’enables when the connection is created. Uses <net.socket>
as the connection object
Event:‘error’enables when the error is occurred. This event will not enable
until server.close() is invoked.
Event:‘listening’ enables when the server is bounded after invoking server.listen()

server.address() of node.js net.Server class

The below given is the server.address( ) example using the node.js net module

const net = require('net');  
const server = net.createServer((socket) => {
  socket.end('socket is closed \n');
}).on('error', (err) => {
  // Handle errors here.
  throw err;
});
// retrive unused port.
server.listen(() => {
  console.log('opened server on', server.address());
});

server.close ([callback])

The callback function is invoked when the server is closed

Returns <net.server>

server.connections

Provides the list of number of connections available on the server.

Return <integer> / Null

server.getConnections([callback])

The callback function is invoked when socket is sent to forks and returns the number of connections on the server asynchronously.

Returns <net.server>

server.listen()

Enables when the server starts listening for connections. The server.listen() can listen to the TCP or IPC Server.

The below given are the possible sever.listen() signatures:

  • server.listen(handle[, backlog][, callback])
  • server.listen(options[, callback])
  • server.listen(path[, backlog][, callback]) for IPC servers
  • server.listen([port[, host[, backlog]]][, callback]) for TCP servers

server.listen() – Points to consider:

  • All server.listen() methods are invoked asynchronously.
  • Callback parameters acts as the listener for the ‘listening’ event
  • backlog parameter specifies the maximum length of the queue of pending connections
  • The server.listen() method can be invoked again in case of an error during the first server.listen() call or in case server.close() has been inititated. The error ERR_SERVER_ALREADY_LISTEN is thrown is the server is already listening and has been invoked again.

class – net.connect()

The net.connect() is an alias to the net.createConnection() .The below given are the possible net.connect signatures:

  • net.connect(options[, connectListener])
  • net.connect(path[, connectListener]) for IPC connections.
  • net.connect(port[, host][, connectListener]) for TCP connections.

net.connect(options[, connectListener])

The method is an alias to the net.createConnection(options[, connectListener]). The methods incudes the below given parameters

  • options
  • connectListener <function>
  • Returns: <net.socket>

net.connect(port[, host][, connectListener])

The method is an alias to the net.createConnection(port[, host][, connectListener]). The methods incudes the below given parameters

  • port <number>
  • host
  • connectListener <function>
  • Returns: <net.socket>

net.connect(path[, connectListener])

The method is an alias to the net.createConnection(path[, connectListener]). The methods incudes the below given parameters:

  • options
  • connectListener <function>
  • Returns: <net.socket>

Class – net.createConnection

The net.createConnection() is used to create the new socket connection. The server.connect( ) returns the net.socket <object> when the connected is started. On the connection establishment, the ‘connect’ event will be emitted. connectListener is the ‘listening’ event for the connection.

The below given are the possible net.createConnection method signatures:

  • net.createConnection(options[, connectListener])
  • net.createConnection(path[, connectListener]) for IPC connections.
  • net.createConnection(port[, host][, connectListener]) for TCP connections.

net.createConnection(options[, connectListener])

The methods incudes the below given parameters:

  • options Required. passed to both the new net.Socket([options]) call and the socket.connect(options[, connectListener]) method.
  • connectListener Common parameter of the net.createConnection(), sets the listener for the ‘connection’ event
  • Returns: //new socket connection
const net = require('net');
const client = net.createConnection({ port: 7001 }, () => {
  // invoking the 'connection' listener.
  console.log('Server is connected successfully....');
  client.write('Sample program for socket creation throug client ..\r\n');
});
client.on('data', (data) => {
  console.log(data.toString());
  client.end();
});
client.on('end', () => {
  console.log('server closed successfully...');
});

In case of server connection error , the below given error should be given by the client program