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:
net.createConnection(options[, connectListener])net.createConnection(path[, connectListener])for IPC connections.net.createConnection(port[, host][, connectListener])for TCP connections.
The net.connect() function is an alias to this function.
net.createConnection(options, connectListener?): net.Socket
Objectnew net.Socket([options]) call and the
socket.connect(options[, connectListener])
method.Functionnet.createConnection() functions. If supplied, will be added as
a listener for the 'connect' event on the returned socket once.net.SocketFor available options, see
new net.Socket([options])
and socket.connect(options[, connectListener]).
Additional options:
net.BoundSocketBoundSocket used as the
connection's source binding, honoring its local address and port. Adoption
consumes the bound socket (see ownership transfer).numbersocket.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:
const client = net.createConnection({ path: '/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)); }, }, });
net.createConnection(path, connectListener?): net.Socket
stringsocket.connect(path[, connectListener]).
See Identifying paths for IPC connections.Functionnet.createConnection() functions, an "once" listener for the
'connect' event on the initiating socket. Will be passed to
socket.connect(path[, connectListener]).net.SocketInitiates 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.
net.createConnection(port, host?, connectListener?): net.Socket
numbersocket.connect(port[, host][, connectListener]).stringsocket.connect(port[, host][, connectListener]).
Default: 'localhost'.Functionnet.createConnection() functions, an "once" listener for the
'connect' event on the initiating socket. Will be passed to
socket.connect(port[, host][, connectListener]).net.SocketInitiates 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.