On this page

M

net.createConnection

History
net.createConnection(): void

A factory function, which creates a new net.Socket, immediately initiates connection with socket.connect(), then returns the net.Socket that starts the connection.

When the connection is established, a 'connect' event will be emitted on the returned socket. The last parameter connectListener, if supplied, will be added as a listener for the 'connect' event once.

Possible signatures:

The net.connect() function is an alias to this function.

M

net.createConnection

History
net.createConnection(options, connectListener?): net.Socket
Attributes
options:Object
Required. Will be passed to both the new net.Socket([options]) call and the socket.connect(options[, connectListener]) method.
connectListener:Function
Common parameter of the net.createConnection() functions. If supplied, will be added as a listener for the 'connect' event on the returned socket once.
Returns:net.Socket
The newly created socket used to start the connection.

For available options, see new net.Socket([options]) and socket.connect(options[, connectListener]).

Additional options:

Attributes
A pre-bound BoundSocket used as the connection's source binding, honoring its local address and port. Adoption consumes the bound socket (see ownership transfer).
timeout:number
If set, will be used to call socket.setTimeout(timeout) after the socket is created, but before it starts the connection.

Following is an example of a client of the echo server described in the net.createServer() section:

import net from 'node:net';
const client = net.createConnection({ port: 8124 }, () => {
  // 'connect' listener.
  console.log('connected to server!');
  client.write('world!\r\n');
});
client.on('data', (data) => {
  console.log(data.toString());
  client.end();
});
client.on('end', () => {
  console.log('disconnected from server');
});
const net = require('node:net');
const client = net.createConnection({ port: 8124 }, () => {
  // 'connect' listener.
  console.log('connected to server!');
  client.write('world!\r\n');
});
client.on('data', (data) => {
  console.log(data.toString());
  client.end();
});
client.on('end', () => {
  console.log('disconnected from server');
});

To connect on the socket /tmp/echo.sock:

Following is an example of a client using the port and onread option. In this case, the onread option will be only used to call new net.Socket([options]) and the port option will be used to call socket.connect(options[, connectListener]).

import net from 'node:net';
import { Buffer } from 'node:buffer';
net.createConnection({
  port: 8124,
  onread: {
    // Reuses a 4KiB Buffer for every read from the socket.
    buffer: Buffer.alloc(4 * 1024),
    callback: function(nread, buf) {
      // Received data is available in `buf` from 0 to `nread`.
      console.log(buf.toString('utf8', 0, nread));
    },
  },
});
const net = require('node:net');
net.createConnection({
  port: 8124,
  onread: {
    // Reuses a 4KiB Buffer for every read from the socket.
    buffer: Buffer.alloc(4 * 1024),
    callback: function(nread, buf) {
      // Received data is available in `buf` from 0 to `nread`.
      console.log(buf.toString('utf8', 0, nread));
    },
  },
});
M

net.createConnection

History
net.createConnection(path, connectListener?): net.Socket
Attributes
path:string
Path the socket should connect to. Will be passed to socket.connect(path[, connectListener]). See Identifying paths for IPC connections.
connectListener:Function
Common parameter of the net.createConnection() functions, an "once" listener for the 'connect' event on the initiating socket. Will be passed to socket.connect(path[, connectListener]).
Returns:net.Socket
The newly created socket used to start the connection.

Initiates an IPC connection.

This function creates a new net.Socket with all options set to default, immediately initiates connection with socket.connect(path[, connectListener]), then returns the net.Socket that starts the connection.

M

net.createConnection

History
net.createConnection(port, host?, connectListener?): net.Socket
Attributes
port:number
Port the socket should connect to. Will be passed to socket.connect(port[, host][, connectListener]).
host?:string
Host the socket should connect to. Will be passed to socket.connect(port[, host][, connectListener]). Default: 'localhost'.
connectListener:Function
Common parameter of the net.createConnection() functions, an "once" listener for the 'connect' event on the initiating socket. Will be passed to socket.connect(port[, host][, connectListener]).
Returns:net.Socket
The newly created socket used to start the connection.

Initiates a TCP connection.

This function creates a new net.Socket with all options set to default, immediately initiates connection with socket.connect(port[, host][, connectListener]), then returns the net.Socket that starts the connection.