{ "type": "module", "source": "doc/api/net.md", "modules": [ { "textRaw": "Net", "name": "net", "introduced_in": "v0.10.0", "desc": "\n
\n

Stability: 2 - Stable

\n
\n

The net module provides an asynchronous network API for creating stream-based\nTCP or IPC servers (net.createServer()) and clients\n(net.createConnection()).

\n

It can be accessed using:

\n
const net = require('net');\n
", "modules": [ { "textRaw": "IPC Support", "name": "ipc_support", "desc": "

The net module supports IPC with named pipes on Windows, and UNIX domain\nsockets on other operating systems.

", "modules": [ { "textRaw": "Identifying paths for IPC connections", "name": "identifying_paths_for_ipc_connections", "desc": "

net.connect(), net.createConnection(), server.listen() and\nsocket.connect() take a path parameter to identify IPC endpoints.

\n

On UNIX, the local domain is also known as the UNIX domain. The path is a\nfilesystem pathname. It gets truncated to sizeof(sockaddr_un.sun_path) - 1,\nwhich varies on different operating system between 91 and 107 bytes.\nThe typical values are 107 on Linux and 103 on macOS. The path is\nsubject to the same naming conventions and permissions checks as would be done\non file creation. If the UNIX domain socket (that is visible as a file system\npath) is created and used in conjunction with one of Node.js' API abstractions\nsuch as net.createServer(), it will be unlinked as part of\nserver.close(). On the other hand, if it is created and used outside of\nthese abstractions, the user will need to manually remove it. The same applies\nwhen the path was created by a Node.js API but the program crashes abruptly.\nIn short, a UNIX domain socket once successfully created will be visible in the\nfilesystem, and will persist until unlinked.

\n

On Windows, the local domain is implemented using a named pipe. The path must\nrefer to an entry in \\\\?\\pipe\\ or \\\\.\\pipe\\. Any characters are permitted,\nbut the latter may do some processing of pipe names, such as resolving ..\nsequences. Despite how it might look, the pipe namespace is flat. Pipes will\nnot persist. They are removed when the last reference to them is closed.\nUnlike UNIX domain sockets, Windows will close and remove the pipe when the\nowning process exits.

\n

JavaScript string escaping requires paths to be specified with extra backslash\nescaping such as:

\n
net.createServer().listen(\n  path.join('\\\\\\\\?\\\\pipe', process.cwd(), 'myctl'));\n
", "type": "module", "displayName": "Identifying paths for IPC connections" } ], "type": "module", "displayName": "IPC Support" } ], "classes": [ { "textRaw": "Class: net.Server", "type": "class", "name": "net.Server", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "

This class is used to create a TCP or IPC server.

", "events": [ { "textRaw": "Event: 'close'", "type": "event", "name": "close", "meta": { "added": [ "v0.5.0" ], "changes": [] }, "params": [], "desc": "

Emitted when the server closes. Note that if connections exist, this\nevent is not emitted until all connections are ended.

" }, { "textRaw": "Event: 'connection'", "type": "event", "name": "connection", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [ { "textRaw": "{net.Socket} The connection object", "type": "net.Socket", "desc": "The connection object" } ], "desc": "

Emitted when a new connection is made. socket is an instance of\nnet.Socket.

" }, { "textRaw": "Event: 'error'", "type": "event", "name": "error", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [ { "textRaw": "{Error}", "type": "Error" } ], "desc": "

Emitted when an error occurs. Unlike net.Socket, the 'close'\nevent will not be emitted directly following this event unless\nserver.close() is manually called. See the example in discussion of\nserver.listen().

" }, { "textRaw": "Event: 'listening'", "type": "event", "name": "listening", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [], "desc": "

Emitted when the server has been bound after calling server.listen().

" } ], "methods": [ { "textRaw": "server.address()", "type": "method", "name": "address", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Object|string}", "name": "return", "type": "Object|string" }, "params": [] } ], "desc": "

Returns the bound address, the address family name, and port of the server\nas reported by the operating system if listening on an IP socket\n(useful to find which port was assigned when getting an OS-assigned address):\n{ port: 12346, family: 'IPv4', address: '127.0.0.1' }.

\n

For a server listening on a pipe or UNIX domain socket, the name is returned\nas a string.

\n
const server = net.createServer((socket) => {\n  socket.end('goodbye\\n');\n}).on('error', (err) => {\n  // handle errors here\n  throw err;\n});\n\n// grab an arbitrary unused port.\nserver.listen(() => {\n  console.log('opened server on', server.address());\n});\n
\n

Don't call server.address() until the 'listening' event has been emitted.

" }, { "textRaw": "server.close([callback])", "type": "method", "name": "close", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`callback` {Function} Called when the server is closed", "name": "callback", "type": "Function", "desc": "Called when the server is closed", "optional": true } ] } ], "desc": "

Stops the server from accepting new connections and keeps existing\nconnections. This function is asynchronous, the server is finally closed\nwhen all connections are ended and the server emits a 'close' event.\nThe optional callback will be called once the 'close' event occurs. Unlike\nthat event, it will be called with an Error as its only argument if the server\nwas not open when it was closed.

" }, { "textRaw": "server.getConnections(callback)", "type": "method", "name": "getConnections", "meta": { "added": [ "v0.9.7" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function" } ] } ], "desc": "

Asynchronously get the number of concurrent connections on the server. Works\nwhen sockets were sent to forks.

\n

Callback should take two arguments err and count.

" }, { "textRaw": "server.listen()", "type": "method", "name": "listen", "signatures": [ { "params": [] } ], "desc": "

Start a server listening for connections. A net.Server can be a TCP or\nan IPC server depending on what it listens to.

\n

Possible signatures:

\n\n

This function is asynchronous. When the server starts listening, the\n'listening' event will be emitted. The last parameter callback\nwill be added as a listener for the 'listening' event.

\n

All listen() methods can take a backlog parameter to specify the maximum\nlength of the queue of pending connections. The actual length will be determined\nby the OS through sysctl settings such as tcp_max_syn_backlog and somaxconn\non Linux. The default value of this parameter is 511 (not 512).

\n

All net.Socket are set to SO_REUSEADDR (see socket(7) for\ndetails).

\n

The server.listen() method can be called again if and only if there was an\nerror during the first server.listen() call or server.close() has been\ncalled. Otherwise, an ERR_SERVER_ALREADY_LISTEN error will be thrown.

\n

One of the most common errors raised when listening is EADDRINUSE.\nThis happens when another server is already listening on the requested\nport/path/handle. One way to handle this would be to retry\nafter a certain amount of time:

\n
server.on('error', (e) => {\n  if (e.code === 'EADDRINUSE') {\n    console.log('Address in use, retrying...');\n    setTimeout(() => {\n      server.close();\n      server.listen(PORT, HOST);\n    }, 1000);\n  }\n});\n
", "methods": [ { "textRaw": "server.listen(handle[, backlog][, callback])", "type": "method", "name": "listen", "meta": { "added": [ "v0.5.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`handle` {Object}", "name": "handle", "type": "Object" }, { "textRaw": "`backlog` {number} Common parameter of [`server.listen()`][] functions", "name": "backlog", "type": "number", "desc": "Common parameter of [`server.listen()`][] functions", "optional": true }, { "textRaw": "`callback` {Function} Common parameter of [`server.listen()`][] functions", "name": "callback", "type": "Function", "desc": "Common parameter of [`server.listen()`][] functions", "optional": true } ] } ], "desc": "

Start a server listening for connections on a given handle that has\nalready been bound to a port, a UNIX domain socket, or a Windows named pipe.

\n

The handle object can be either a server, a socket (anything with an\nunderlying _handle member), or an object with an fd member that is a\nvalid file descriptor.

\n

Listening on a file descriptor is not supported on Windows.

" }, { "textRaw": "server.listen(options[, callback])", "type": "method", "name": "listen", "meta": { "added": [ "v0.11.14" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`options` {Object} Required. Supports the following properties:", "name": "options", "type": "Object", "desc": "Required. Supports the following properties:", "options": [ { "textRaw": "`port` {number}", "name": "port", "type": "number" }, { "textRaw": "`host` {string}", "name": "host", "type": "string" }, { "textRaw": "`path` {string} Will be ignored if `port` is specified. See [Identifying paths for IPC connections][].", "name": "path", "type": "string", "desc": "Will be ignored if `port` is specified. See [Identifying paths for IPC connections][]." }, { "textRaw": "`backlog` {number} Common parameter of [`server.listen()`][] functions.", "name": "backlog", "type": "number", "desc": "Common parameter of [`server.listen()`][] functions." }, { "textRaw": "`exclusive` {boolean} **Default:** `false`", "name": "exclusive", "type": "boolean", "default": "`false`" }, { "textRaw": "`readableAll` {boolean} For IPC servers makes the pipe readable for all users. **Default:** `false`", "name": "readableAll", "type": "boolean", "default": "`false`", "desc": "For IPC servers makes the pipe readable for all users." }, { "textRaw": "`writableAll` {boolean} For IPC servers makes the pipe writable for all users. **Default:** `false`", "name": "writableAll", "type": "boolean", "default": "`false`", "desc": "For IPC servers makes the pipe writable for all users." } ] }, { "textRaw": "`callback` {Function} Common parameter of [`server.listen()`][] functions.", "name": "callback", "type": "Function", "desc": "Common parameter of [`server.listen()`][] functions.", "optional": true } ] } ], "desc": "

If port is specified, it behaves the same as\n\nserver.listen([port[, host[, backlog]]][, callback]).\nOtherwise, if path is specified, it behaves the same as\nserver.listen(path[, backlog][, callback]).\nIf none of them is specified, an error will be thrown.

\n

If exclusive is false (default), then cluster workers will use the same\nunderlying handle, allowing connection handling duties to be shared. When\nexclusive is true, the handle is not shared, and attempted port sharing\nresults in an error. An example which listens on an exclusive port is\nshown below.

\n
server.listen({\n  host: 'localhost',\n  port: 80,\n  exclusive: true\n});\n
\n

Starting an IPC server as root may cause the server path to be inaccessible for\nunprivileged users. Using readableAll and writableAll will make the server\naccessible for all users.

" }, { "textRaw": "server.listen(path[, backlog][, callback])", "type": "method", "name": "listen", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`path` {string} Path the server should listen to. See [Identifying paths for IPC connections][].", "name": "path", "type": "string", "desc": "Path the server should listen to. See [Identifying paths for IPC connections][]." }, { "textRaw": "`backlog` {number} Common parameter of [`server.listen()`][] functions.", "name": "backlog", "type": "number", "desc": "Common parameter of [`server.listen()`][] functions.", "optional": true }, { "textRaw": "`callback` {Function} Common parameter of [`server.listen()`][] functions.", "name": "callback", "type": "Function", "desc": "Common parameter of [`server.listen()`][] functions.", "optional": true } ] } ], "desc": "

Start an IPC server listening for connections on the given path.

" }, { "textRaw": "server.listen([port[, host[, backlog]]][, callback])", "type": "method", "name": "listen", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`port` {number}", "name": "port", "type": "number", "optional": true }, { "textRaw": "`host` {string}", "name": "host", "type": "string", "optional": true }, { "textRaw": "`backlog` {number} Common parameter of [`server.listen()`][] functions.", "name": "backlog", "type": "number", "desc": "Common parameter of [`server.listen()`][] functions.", "optional": true }, { "textRaw": "`callback` {Function} Common parameter of [`server.listen()`][] functions.", "name": "callback", "type": "Function", "desc": "Common parameter of [`server.listen()`][] functions.", "optional": true } ] } ], "desc": "

Start a TCP server listening for connections on the given port and host.

\n

If port is omitted or is 0, the operating system will assign an arbitrary\nunused port, which can be retrieved by using server.address().port\nafter the 'listening' event has been emitted.

\n

If host is omitted, the server will accept connections on the\nunspecified IPv6 address (::) when IPv6 is available, or the\nunspecified IPv4 address (0.0.0.0) otherwise.

\n

In most operating systems, listening to the unspecified IPv6 address (::)\nmay cause the net.Server to also listen on the unspecified IPv4 address\n(0.0.0.0).

" } ] }, { "textRaw": "server.ref()", "type": "method", "name": "ref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [] } ], "desc": "

Opposite of unref(), calling ref() on a previously unrefed server will\nnot let the program exit if it's the only server left (the default behavior).\nIf the server is refed calling ref() again will have no effect.

" }, { "textRaw": "server.unref()", "type": "method", "name": "unref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [] } ], "desc": "

Calling unref() on a server will allow the program to exit if this is the only\nactive server in the event system. If the server is already unrefed calling\nunref() again will have no effect.

" } ], "properties": [ { "textRaw": "server.connections", "name": "connections", "meta": { "added": [ "v0.2.0" ], "deprecated": [ "v0.9.7" ], "changes": [] }, "stability": 0, "stabilityText": "Deprecated: Use [`server.getConnections()`][] instead.", "desc": "

The number of concurrent connections on the server.

\n

This becomes null when sending a socket to a child with\nchild_process.fork(). To poll forks and get current number of active\nconnections, use asynchronous server.getConnections() instead.

" }, { "textRaw": "`listening` {boolean} Indicates whether or not the server is listening for connections.", "type": "boolean", "name": "listening", "meta": { "added": [ "v5.7.0" ], "changes": [] }, "desc": "Indicates whether or not the server is listening for connections." }, { "textRaw": "server.maxConnections", "name": "maxConnections", "meta": { "added": [ "v0.2.0" ], "changes": [] }, "desc": "

Set this property to reject connections when the server's connection count gets\nhigh.

\n

It is not recommended to use this option once a socket has been sent to a child\nwith child_process.fork().

" } ], "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`options` {Object} See [`net.createServer([options][, connectionListener])`][`net.createServer()`].", "name": "options", "type": "Object", "desc": "See [`net.createServer([options][, connectionListener])`][`net.createServer()`].", "optional": true }, { "textRaw": "`connectionListener` {Function} Automatically set as a listener for the [`'connection'`][] event.", "name": "connectionListener", "type": "Function", "desc": "Automatically set as a listener for the [`'connection'`][] event.", "optional": true } ], "desc": "

net.Server is an EventEmitter with the following events:

" } ] }, { "textRaw": "Class: net.Socket", "type": "class", "name": "net.Socket", "meta": { "added": [ "v0.3.4" ], "changes": [] }, "desc": "

This class is an abstraction of a TCP socket or a streaming IPC endpoint\n(uses named pipes on Windows, and UNIX domain sockets otherwise). A\nnet.Socket is also a duplex stream, so it can be both readable and\nwritable, and it is also an EventEmitter.

\n

A net.Socket can be created by the user and used directly to interact with\na server. For example, it is returned by net.createConnection(),\nso the user can use it to talk to the server.

\n

It can also be created by Node.js and passed to the user when a connection\nis received. For example, it is passed to the listeners of a\n'connection' event emitted on a net.Server, so the user can use\nit to interact with the client.

", "events": [ { "textRaw": "Event: 'close'", "type": "event", "name": "close", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [ { "textRaw": "`hadError` {boolean} `true` if the socket had a transmission error.", "name": "hadError", "type": "boolean", "desc": "`true` if the socket had a transmission error." } ], "desc": "

Emitted once the socket is fully closed. The argument hadError is a boolean\nwhich says if the socket was closed due to a transmission error.

" }, { "textRaw": "Event: 'connect'", "type": "event", "name": "connect", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [], "desc": "

Emitted when a socket connection is successfully established.\nSee net.createConnection().

" }, { "textRaw": "Event: 'data'", "type": "event", "name": "data", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [ { "textRaw": "{Buffer|string}", "type": "Buffer|string" } ], "desc": "

Emitted when data is received. The argument data will be a Buffer or\nString. Encoding of data is set by socket.setEncoding().

\n

Note that the data will be lost if there is no listener when a Socket\nemits a 'data' event.

" }, { "textRaw": "Event: 'drain'", "type": "event", "name": "drain", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [], "desc": "

Emitted when the write buffer becomes empty. Can be used to throttle uploads.

\n

See also: the return values of socket.write().

" }, { "textRaw": "Event: 'end'", "type": "event", "name": "end", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [], "desc": "

Emitted when the other end of the socket sends a FIN packet, thus ending the\nreadable side of the socket.

\n

By default (allowHalfOpen is false) the socket will send a FIN packet\nback and destroy its file descriptor once it has written out its pending\nwrite queue. However, if allowHalfOpen is set to true, the socket will\nnot automatically end() its writable side, allowing the\nuser to write arbitrary amounts of data. The user must call\nend() explicitly to close the connection (i.e. sending a\nFIN packet back).

" }, { "textRaw": "Event: 'error'", "type": "event", "name": "error", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [ { "textRaw": "{Error}", "type": "Error" } ], "desc": "

Emitted when an error occurs. The 'close' event will be called directly\nfollowing this event.

" }, { "textRaw": "Event: 'lookup'", "type": "event", "name": "lookup", "meta": { "added": [ "v0.11.3" ], "changes": [ { "version": "v5.10.0", "pr-url": "https://github.com/nodejs/node/pull/5598", "description": "The `host` parameter is supported now." } ] }, "params": [], "desc": "

Emitted after resolving the hostname but before connecting.\nNot applicable to UNIX sockets.

\n" }, { "textRaw": "Event: 'ready'", "type": "event", "name": "ready", "meta": { "added": [ "v9.11.0" ], "changes": [] }, "params": [], "desc": "

Emitted when a socket is ready to be used.

\n

Triggered immediately after 'connect'.

" }, { "textRaw": "Event: 'timeout'", "type": "event", "name": "timeout", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [], "desc": "

Emitted if the socket times out from inactivity. This is only to notify that\nthe socket has been idle. The user must manually close the connection.

\n

See also: socket.setTimeout().

" } ], "methods": [ { "textRaw": "socket.address()", "type": "method", "name": "address", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Object}", "name": "return", "type": "Object" }, "params": [] } ], "desc": "

Returns the bound address, the address family name and port of the\nsocket as reported by the operating system:\n{ port: 12346, family: 'IPv4', address: '127.0.0.1' }

" }, { "textRaw": "socket.connect()", "type": "method", "name": "connect", "signatures": [ { "params": [] } ], "desc": "

Initiate a connection on a given socket.

\n

Possible signatures:

\n\n

This function is asynchronous. When the connection is established, the\n'connect' event will be emitted. If there is a problem connecting,\ninstead of a 'connect' event, an 'error' event will be emitted with\nthe error passed to the 'error' listener.\nThe last parameter connectListener, if supplied, will be added as a listener\nfor the 'connect' event once.

", "methods": [ { "textRaw": "socket.connect(options[, connectListener])", "type": "method", "name": "connect", "meta": { "added": [ "v0.1.90" ], "changes": [ { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/6021", "description": "The `hints` option defaults to `0` in all cases now. Previously, in the absence of the `family` option it would default to `dns.ADDRCONFIG | dns.V4MAPPED`." }, { "version": "v5.11.0", "pr-url": "https://github.com/nodejs/node/pull/6000", "description": "The `hints` option is supported now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object" }, { "textRaw": "`connectListener` {Function} Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once.", "name": "connectListener", "type": "Function", "desc": "Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once.", "optional": true } ] } ], "desc": "

Initiate a connection on a given socket. Normally this method is not needed,\nthe socket should be created and opened with net.createConnection(). Use\nthis only when implementing a custom Socket.

\n

For TCP connections, available options are:

\n\n

For IPC connections, available options are:

\n" }, { "textRaw": "socket.connect(path[, connectListener])", "type": "method", "name": "connect", "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`path` {string} Path the client should connect to. See [Identifying paths for IPC connections][].", "name": "path", "type": "string", "desc": "Path the client should connect to. See [Identifying paths for IPC connections][]." }, { "textRaw": "`connectListener` {Function} Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once.", "name": "connectListener", "type": "Function", "desc": "Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once.", "optional": true } ] } ], "desc": "

Initiate an IPC connection on the given socket.

\n

Alias to\nsocket.connect(options[, connectListener])\ncalled with { path: path } as options.

" }, { "textRaw": "socket.connect(port[, host][, connectListener])", "type": "method", "name": "connect", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`port` {number} Port the client should connect to.", "name": "port", "type": "number", "desc": "Port the client should connect to." }, { "textRaw": "`host` {string} Host the client should connect to.", "name": "host", "type": "string", "desc": "Host the client should connect to.", "optional": true }, { "textRaw": "`connectListener` {Function} Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once.", "name": "connectListener", "type": "Function", "desc": "Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once.", "optional": true } ] } ], "desc": "

Initiate a TCP connection on the given socket.

\n

Alias to\nsocket.connect(options[, connectListener])\ncalled with {port: port, host: host} as options.

" } ] }, { "textRaw": "socket.destroy([exception])", "type": "method", "name": "destroy", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket}", "name": "return", "type": "net.Socket" }, "params": [ { "textRaw": "`exception` {Object}", "name": "exception", "type": "Object", "optional": true } ] } ], "desc": "

Ensures that no more I/O activity happens on this socket. Only necessary in\ncase of errors (parse error or so).

\n

If exception is specified, an 'error' event will be emitted and any\nlisteners for that event will receive exception as an argument.

" }, { "textRaw": "socket.end([data][, encoding][, callback])", "type": "method", "name": "end", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`data` {string|Buffer|Uint8Array}", "name": "data", "type": "string|Buffer|Uint8Array", "optional": true }, { "textRaw": "`encoding` {string} Only used when data is `string`. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "Only used when data is `string`.", "optional": true }, { "textRaw": "`callback` {Function} Optional callback for when the socket is finished.", "name": "callback", "type": "Function", "desc": "Optional callback for when the socket is finished.", "optional": true } ] } ], "desc": "

Half-closes the socket. i.e., it sends a FIN packet. It is possible the\nserver will still send some data.

\n

If data is specified, it is equivalent to calling\nsocket.write(data, encoding) followed by socket.end().

" }, { "textRaw": "socket.pause()", "type": "method", "name": "pause", "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [] } ], "desc": "

Pauses the reading of data. That is, 'data' events will not be emitted.\nUseful to throttle back an upload.

" }, { "textRaw": "socket.ref()", "type": "method", "name": "ref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [] } ], "desc": "

Opposite of unref(), calling ref() on a previously unrefed socket will\nnot let the program exit if it's the only socket left (the default behavior).\nIf the socket is refed calling ref again will have no effect.

" }, { "textRaw": "socket.resume()", "type": "method", "name": "resume", "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [] } ], "desc": "

Resumes reading after a call to socket.pause().

" }, { "textRaw": "socket.setEncoding([encoding])", "type": "method", "name": "setEncoding", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`encoding` {string}", "name": "encoding", "type": "string", "optional": true } ] } ], "desc": "

Set the encoding for the socket as a Readable Stream. See\nreadable.setEncoding() for more information.

" }, { "textRaw": "socket.setKeepAlive([enable][, initialDelay])", "type": "method", "name": "setKeepAlive", "meta": { "added": [ "v0.1.92" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`enable` {boolean} **Default:** `false`", "name": "enable", "type": "boolean", "default": "`false`", "optional": true }, { "textRaw": "`initialDelay` {number} **Default:** `0`", "name": "initialDelay", "type": "number", "default": "`0`", "optional": true } ] } ], "desc": "

Enable/disable keep-alive functionality, and optionally set the initial\ndelay before the first keepalive probe is sent on an idle socket.

\n

Set initialDelay (in milliseconds) to set the delay between the last\ndata packet received and the first keepalive probe. Setting 0 for\ninitialDelay will leave the value unchanged from the default\n(or previous) setting.

" }, { "textRaw": "socket.setNoDelay([noDelay])", "type": "method", "name": "setNoDelay", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`noDelay` {boolean} **Default:** `true`", "name": "noDelay", "type": "boolean", "default": "`true`", "optional": true } ] } ], "desc": "

Disables the Nagle algorithm. By default TCP connections use the Nagle\nalgorithm, they buffer data before sending it off. Setting true for\nnoDelay will immediately fire off data each time socket.write() is called.

" }, { "textRaw": "socket.setTimeout(timeout[, callback])", "type": "method", "name": "setTimeout", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`timeout` {number}", "name": "timeout", "type": "number" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "optional": true } ] } ], "desc": "

Sets the socket to timeout after timeout milliseconds of inactivity on\nthe socket. By default net.Socket do not have a timeout.

\n

When an idle timeout is triggered the socket will receive a 'timeout'\nevent but the connection will not be severed. The user must manually call\nsocket.end() or socket.destroy() to end the connection.

\n
socket.setTimeout(3000);\nsocket.on('timeout', () => {\n  console.log('socket timeout');\n  socket.end();\n});\n
\n

If timeout is 0, then the existing idle timeout is disabled.

\n

The optional callback parameter will be added as a one-time listener for the\n'timeout' event.

" }, { "textRaw": "socket.unref()", "type": "method", "name": "unref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [] } ], "desc": "

Calling unref() on a socket will allow the program to exit if this is the only\nactive socket in the event system. If the socket is already unrefed calling\nunref() again will have no effect.

" }, { "textRaw": "socket.write(data[, encoding][, callback])", "type": "method", "name": "write", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`data` {string|Buffer|Uint8Array}", "name": "data", "type": "string|Buffer|Uint8Array" }, { "textRaw": "`encoding` {string} Only used when data is `string`. **Default:** `utf8`.", "name": "encoding", "type": "string", "default": "`utf8`", "desc": "Only used when data is `string`.", "optional": true }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "optional": true } ] } ], "desc": "

Sends data on the socket. The second parameter specifies the encoding in the\ncase of a string — it defaults to UTF8 encoding.

\n

Returns true if the entire data was flushed successfully to the kernel\nbuffer. Returns false if all or part of the data was queued in user memory.\n'drain' will be emitted when the buffer is again free.

\n

The optional callback parameter will be executed when the data is finally\nwritten out - this may not be immediately.

\n

See Writable stream write() method for more\ninformation.

" } ], "properties": [ { "textRaw": "socket.bufferSize", "name": "bufferSize", "meta": { "added": [ "v0.3.8" ], "changes": [] }, "desc": "

net.Socket has the property that socket.write() always works. This is to\nhelp users get up and running quickly. The computer cannot always keep up\nwith the amount of data that is written to a socket - the network connection\nsimply might be too slow. Node.js will internally queue up the data written to a\nsocket and send it out over the wire when it is possible. (Internally it is\npolling on the socket's file descriptor for being writable).

\n

The consequence of this internal buffering is that memory may grow. This\nproperty shows the number of characters currently buffered to be written.\n(Number of characters is approximately equal to the number of bytes to be\nwritten, but the buffer may contain strings, and the strings are lazily\nencoded, so the exact number of bytes is not known.)

\n

Users who experience large or growing bufferSize should attempt to\n\"throttle\" the data flows in their program with\nsocket.pause() and socket.resume().

" }, { "textRaw": "socket.bytesRead", "name": "bytesRead", "meta": { "added": [ "v0.5.3" ], "changes": [] }, "desc": "

The amount of received bytes.

" }, { "textRaw": "socket.bytesWritten", "name": "bytesWritten", "meta": { "added": [ "v0.5.3" ], "changes": [] }, "desc": "

The amount of bytes sent.

" }, { "textRaw": "socket.connecting", "name": "connecting", "meta": { "added": [ "v6.1.0" ], "changes": [] }, "desc": "

If true,\nsocket.connect(options[, connectListener]) was\ncalled and has not yet finished. It will stay true until the socket becomes\nconnected, then it is set to false and the 'connect' event is emitted. Note\nthat the\nsocket.connect(options[, connectListener])\ncallback is a listener for the 'connect' event.

" }, { "textRaw": "`destroyed` {boolean} Indicates if the connection is destroyed or not. Once a connection is destroyed no further data can be transferred using it.", "type": "boolean", "name": "destroyed", "desc": "Indicates if the connection is destroyed or not. Once a connection is destroyed no further data can be transferred using it." }, { "textRaw": "socket.localAddress", "name": "localAddress", "meta": { "added": [ "v0.9.6" ], "changes": [] }, "desc": "

The string representation of the local IP address the remote client is\nconnecting on. For example, in a server listening on '0.0.0.0', if a client\nconnects on '192.168.1.1', the value of socket.localAddress would be\n'192.168.1.1'.

" }, { "textRaw": "socket.localPort", "name": "localPort", "meta": { "added": [ "v0.9.6" ], "changes": [] }, "desc": "

The numeric representation of the local port. For example, 80 or 21.

" }, { "textRaw": "`pending` {boolean}", "type": "boolean", "name": "pending", "meta": { "added": [ "v10.16.0" ], "changes": [] }, "desc": "

This is true if the socket is not connected yet, either because .connect()\nhas not yet been called or because it is still in the process of connecting\n(see socket.connecting).

" }, { "textRaw": "socket.remoteAddress", "name": "remoteAddress", "meta": { "added": [ "v0.5.10" ], "changes": [] }, "desc": "

The string representation of the remote IP address. For example,\n'74.125.127.100' or '2001:4860:a005::68'. Value may be undefined if\nthe socket is destroyed (for example, if the client disconnected).

" }, { "textRaw": "socket.remoteFamily", "name": "remoteFamily", "meta": { "added": [ "v0.11.14" ], "changes": [] }, "desc": "

The string representation of the remote IP family. 'IPv4' or 'IPv6'.

" }, { "textRaw": "socket.remotePort", "name": "remotePort", "meta": { "added": [ "v0.5.10" ], "changes": [] }, "desc": "

The numeric representation of the remote port. For example, 80 or 21.

" } ], "signatures": [ { "return": { "textRaw": "Returns: {net.Socket}", "name": "return", "type": "net.Socket" }, "params": [ { "textRaw": "`options` {Object} Available options are:", "name": "options", "type": "Object", "desc": "Available options are:", "options": [ { "textRaw": "`fd` {number} If specified, wrap around an existing socket with the given file descriptor, otherwise a new socket will be created.", "name": "fd", "type": "number", "desc": "If specified, wrap around an existing socket with the given file descriptor, otherwise a new socket will be created." }, { "textRaw": "`allowHalfOpen` {boolean} Indicates whether half-opened TCP connections are allowed. See [`net.createServer()`][] and the [`'end'`][] event for details. **Default:** `false`.", "name": "allowHalfOpen", "type": "boolean", "default": "`false`", "desc": "Indicates whether half-opened TCP connections are allowed. See [`net.createServer()`][] and the [`'end'`][] event for details." }, { "textRaw": "`readable` {boolean} Allow reads on the socket when an `fd` is passed, otherwise ignored. **Default:** `false`.", "name": "readable", "type": "boolean", "default": "`false`", "desc": "Allow reads on the socket when an `fd` is passed, otherwise ignored." }, { "textRaw": "`writable` {boolean} Allow writes on the socket when an `fd` is passed, otherwise ignored. **Default:** `false`.", "name": "writable", "type": "boolean", "default": "`false`", "desc": "Allow writes on the socket when an `fd` is passed, otherwise ignored." } ], "optional": true } ], "desc": "

Creates a new socket object.

\n

The newly created socket can be either a TCP socket or a streaming IPC\nendpoint, depending on what it connect() to.

" } ] } ], "methods": [ { "textRaw": "net.connect()", "type": "method", "name": "connect", "signatures": [ { "params": [] } ], "desc": "

Aliases to\nnet.createConnection().

\n

Possible signatures:

\n", "methods": [ { "textRaw": "net.connect(options[, connectListener])", "type": "method", "name": "connect", "meta": { "added": [ "v0.7.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object" }, { "textRaw": "`connectListener` {Function}", "name": "connectListener", "type": "Function", "optional": true } ] } ], "desc": "

Alias to\nnet.createConnection(options[, connectListener]).

" }, { "textRaw": "net.connect(path[, connectListener])", "type": "method", "name": "connect", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`path` {string}", "name": "path", "type": "string" }, { "textRaw": "`connectListener` {Function}", "name": "connectListener", "type": "Function", "optional": true } ] } ], "desc": "

Alias to\nnet.createConnection(path[, connectListener]).

" }, { "textRaw": "net.connect(port[, host][, connectListener])", "type": "method", "name": "connect", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`port` {number}", "name": "port", "type": "number" }, { "textRaw": "`host` {string}", "name": "host", "type": "string", "optional": true }, { "textRaw": "`connectListener` {Function}", "name": "connectListener", "type": "Function", "optional": true } ] } ], "desc": "

Alias to\nnet.createConnection(port[, host][, connectListener]).

" } ] }, { "textRaw": "net.createConnection()", "type": "method", "name": "createConnection", "signatures": [ { "params": [] } ], "desc": "

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

\n

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

\n

Possible signatures:

\n\n

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

", "methods": [ { "textRaw": "net.createConnection(options[, connectListener])", "type": "method", "name": "createConnection", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The newly created socket used to start the connection.", "name": "return", "type": "net.Socket", "desc": "The newly created socket used to start the connection." }, "params": [ { "textRaw": "`options` {Object} Required. Will be passed to both the [`new net.Socket([options])`][`new net.Socket(options)`] call and the [`socket.connect(options[, connectListener])`][`socket.connect(options)`] method.", "name": "options", "type": "Object", "desc": "Required. Will be passed to both the [`new net.Socket([options])`][`new net.Socket(options)`] call and the [`socket.connect(options[, connectListener])`][`socket.connect(options)`] method." }, { "textRaw": "`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.", "name": "connectListener", "type": "Function", "desc": "Common parameter of the [`net.createConnection()`][] functions. If supplied, will be added as a listener for the [`'connect'`][] event on the returned socket once.", "optional": true } ] } ], "desc": "

For available options, see\nnew net.Socket([options])\nand socket.connect(options[, connectListener]).

\n

Additional options:

\n\n

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

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

To connect on the socket /tmp/echo.sock the second line would just be\nchanged to:

\n
const client = net.createConnection({ path: '/tmp/echo.sock' });\n
" }, { "textRaw": "net.createConnection(path[, connectListener])", "type": "method", "name": "createConnection", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The newly created socket used to start the connection.", "name": "return", "type": "net.Socket", "desc": "The newly created socket used to start the connection." }, "params": [ { "textRaw": "`path` {string} Path the socket should connect to. Will be passed to [`socket.connect(path[, connectListener])`][`socket.connect(path)`]. See [Identifying paths for IPC connections][].", "name": "path", "type": "string", "desc": "Path the socket should connect to. Will be passed to [`socket.connect(path[, connectListener])`][`socket.connect(path)`]. See [Identifying paths for IPC connections][]." }, { "textRaw": "`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])`][`socket.connect(path)`].", "name": "connectListener", "type": "Function", "desc": "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])`][`socket.connect(path)`].", "optional": true } ] } ], "desc": "

Initiates an IPC connection.

\n

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

" }, { "textRaw": "net.createConnection(port[, host][, connectListener])", "type": "method", "name": "createConnection", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The newly created socket used to start the connection.", "name": "return", "type": "net.Socket", "desc": "The newly created socket used to start the connection." }, "params": [ { "textRaw": "`port` {number} Port the socket should connect to. Will be passed to [`socket.connect(port[, host][, connectListener])`][`socket.connect(port, host)`].", "name": "port", "type": "number", "desc": "Port the socket should connect to. Will be passed to [`socket.connect(port[, host][, connectListener])`][`socket.connect(port, host)`]." }, { "textRaw": "`host` {string} Host the socket should connect to. Will be passed to [`socket.connect(port[, host][, connectListener])`][`socket.connect(port, host)`]. **Default:** `'localhost'`.", "name": "host", "type": "string", "default": "`'localhost'`", "desc": "Host the socket should connect to. Will be passed to [`socket.connect(port[, host][, connectListener])`][`socket.connect(port, host)`].", "optional": true }, { "textRaw": "`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])`][`socket.connect(port, host)`].", "name": "connectListener", "type": "Function", "desc": "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])`][`socket.connect(port, host)`].", "optional": true } ] } ], "desc": "

Initiates a TCP connection.

\n

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

" } ] }, { "textRaw": "net.createServer([options][, connectionListener])", "type": "method", "name": "createServer", "meta": { "added": [ "v0.5.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`allowHalfOpen` {boolean} Indicates whether half-opened TCP connections are allowed. **Default:** `false`.", "name": "allowHalfOpen", "type": "boolean", "default": "`false`", "desc": "Indicates whether half-opened TCP connections are allowed." }, { "textRaw": "`pauseOnConnect` {boolean} Indicates whether the socket should be paused on incoming connections. **Default:** `false`.", "name": "pauseOnConnect", "type": "boolean", "default": "`false`", "desc": "Indicates whether the socket should be paused on incoming connections." } ], "optional": true }, { "textRaw": "`connectionListener` {Function} Automatically set as a listener for the [`'connection'`][] event.", "name": "connectionListener", "type": "Function", "desc": "Automatically set as a listener for the [`'connection'`][] event.", "optional": true } ] } ], "desc": "

Creates a new TCP or IPC server.

\n

If allowHalfOpen is set to true, when the other end of the socket\nsends a FIN packet, the server will only send a FIN packet back when\nsocket.end() is explicitly called, until then the connection is\nhalf-closed (non-readable but still writable). See 'end' event\nand RFC 1122 (section 4.2.2.13) for more information.

\n

If pauseOnConnect is set to true, then the socket associated with each\nincoming connection will be paused, and no data will be read from its handle.\nThis allows connections to be passed between processes without any data being\nread by the original process. To begin reading data from a paused socket, call\nsocket.resume().

\n

The server can be a TCP server or an IPC server, depending on what it\nlisten() to.

\n

Here is an example of an TCP echo server which listens for connections\non port 8124:

\n
const net = require('net');\nconst server = net.createServer((c) => {\n  // 'connection' listener\n  console.log('client connected');\n  c.on('end', () => {\n    console.log('client disconnected');\n  });\n  c.write('hello\\r\\n');\n  c.pipe(c);\n});\nserver.on('error', (err) => {\n  throw err;\n});\nserver.listen(8124, () => {\n  console.log('server bound');\n});\n
\n

Test this by using telnet:

\n
$ telnet localhost 8124\n
\n

To listen on the socket /tmp/echo.sock the third line from the last would\njust be changed to:

\n
server.listen('/tmp/echo.sock', () => {\n  console.log('server bound');\n});\n
\n

Use nc to connect to a UNIX domain socket server:

\n
$ nc -U /tmp/echo.sock\n
" }, { "textRaw": "net.isIP(input)", "type": "method", "name": "isIP", "meta": { "added": [ "v0.3.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`input` {string}", "name": "input", "type": "string" } ] } ], "desc": "

Tests if input is an IP address. Returns 0 for invalid strings,\nreturns 4 for IP version 4 addresses, and returns 6 for IP version 6\naddresses.

" }, { "textRaw": "net.isIPv4(input)", "type": "method", "name": "isIPv4", "meta": { "added": [ "v0.3.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`input` {string}", "name": "input", "type": "string" } ] } ], "desc": "

Returns true if input is a version 4 IP address, otherwise returns false.

" }, { "textRaw": "net.isIPv6(input)", "type": "method", "name": "isIPv6", "meta": { "added": [ "v0.3.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`input` {string}", "name": "input", "type": "string" } ] } ], "desc": "

Returns true if input is a version 6 IP address, otherwise returns false.

" } ], "type": "module", "displayName": "Net" } ] }