{ "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 a length of\nsizeof(sockaddr_un.sun_path) - 1, which varies 91 and 107 bytes depending on\nthe operating system. The typical values are 107 on Linux and 103 on macOS. The\npath is subject to the same naming conventions and permissions checks as would\nbe done on file creation. If the Unix domain socket (that is visible as a file\nsystem path) is created and used in conjunction with one of Node.js' API\nabstractions such 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" }, { "textRaw": "Class: `net.Server`", "name": "class:_`net.server`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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

", "modules": [ { "textRaw": "`new net.Server([options][, connectionListener])`", "name": "`new_net.server([options][,_connectionlistener])`", "desc": "\n

net.Server is an EventEmitter with the following events:

", "type": "module", "displayName": "`new net.Server([options][, connectionListener])`" }, { "textRaw": "Event: `'close'`", "name": "event:_`'close'`", "meta": { "added": [ "v0.5.0" ], "changes": [] }, "desc": "

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

", "type": "module", "displayName": "Event: `'close'`" }, { "textRaw": "Event: `'connection'`", "name": "event:_`'connection'`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "Event: `'connection'`" }, { "textRaw": "Event: `'error'`", "name": "event:_`'error'`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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().

", "type": "module", "displayName": "Event: `'error'`" }, { "textRaw": "Event: `'listening'`", "name": "event:_`'listening'`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "

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

", "type": "module", "displayName": "Event: `'listening'`" }, { "textRaw": "`server.address()`", "name": "`server.address()`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`server.address()`" }, { "textRaw": "`server.close([callback])`", "name": "`server.close([callback])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`server.close([callback])`" }, { "textRaw": "`server.connections`", "name": "`server.connections`", "meta": { "added": [ "v0.2.0" ], "deprecated": [ "v0.9.7" ], "changes": [] }, "stability": 0, "stabilityText": "Deprecated: Use [`server.getConnections()`][] instead.", "desc": "\n

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.

", "type": "module", "displayName": "`server.connections`" }, { "textRaw": "`server.getConnections(callback)`", "name": "`server.getconnections(callback)`", "meta": { "added": [ "v0.9.7" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`server.getConnections(callback)`" }, { "textRaw": "`server.listen()`", "name": "`server.listen()`", "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
", "modules": [ { "textRaw": "`server.listen(handle[, backlog][, callback])`", "name": "`server.listen(handle[,_backlog][,_callback])`", "meta": { "added": [ "v0.5.10" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`server.listen(handle[, backlog][, callback])`" }, { "textRaw": "`server.listen(options[, callback])`", "name": "`server.listen(options[,_callback])`", "meta": { "added": [ "v0.11.14" ], "changes": [ { "version": "v11.4.0", "pr-url": "https://github.com/nodejs/node/pull/23798", "description": "The `ipv6Only` option is supported." } ] }, "desc": "\n

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.

", "type": "module", "displayName": "`server.listen(options[, callback])`" }, { "textRaw": "`server.listen(path[, backlog][, callback])`", "name": "`server.listen(path[,_backlog][,_callback])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "`server.listen(path[, backlog][, callback])`" }, { "textRaw": "`server.listen([port[, host[, backlog]]][, callback])`", "name": "`server.listen([port[,_host[,_backlog]]][,_callback])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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).

", "type": "module", "displayName": "`server.listen([port[, host[, backlog]]][, callback])`" } ], "type": "module", "displayName": "`server.listen()`" }, { "textRaw": "`server.listening`", "name": "`server.listening`", "meta": { "added": [ "v5.7.0" ], "changes": [] }, "desc": "", "type": "module", "displayName": "`server.listening`" }, { "textRaw": "`server.maxConnections`", "name": "`server.maxconnections`", "meta": { "added": [ "v0.2.0" ], "changes": [] }, "desc": "\n

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().

", "type": "module", "displayName": "`server.maxConnections`" }, { "textRaw": "`server.ref()`", "name": "`server.ref()`", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`server.ref()`" }, { "textRaw": "`server.unref()`", "name": "`server.unref()`", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`server.unref()`" } ], "type": "module", "displayName": "Class: `net.Server`" }, { "textRaw": "Class: `net.Socket`", "name": "class:_`net.socket`", "meta": { "added": [ "v0.3.4" ], "changes": [] }, "desc": "\n

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). It is also\nan 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.

", "modules": [ { "textRaw": "`new net.Socket([options])`", "name": "`new_net.socket([options])`", "meta": { "added": [ "v0.3.4" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`new net.Socket([options])`" }, { "textRaw": "Event: `'close'`", "name": "event:_`'close'`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "Event: `'close'`" }, { "textRaw": "Event: `'connect'`", "name": "event:_`'connect'`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "

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

", "type": "module", "displayName": "Event: `'connect'`" }, { "textRaw": "Event: `'data'`", "name": "event:_`'data'`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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

\n

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

", "type": "module", "displayName": "Event: `'data'`" }, { "textRaw": "Event: `'drain'`", "name": "event:_`'drain'`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "

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

\n

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

", "type": "module", "displayName": "Event: `'drain'`" }, { "textRaw": "Event: `'end'`", "name": "event:_`'end'`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "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).

", "type": "module", "displayName": "Event: `'end'`" }, { "textRaw": "Event: `'error'`", "name": "event:_`'error'`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "Event: `'error'`" }, { "textRaw": "Event: `'lookup'`", "name": "event:_`'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." } ] }, "desc": "

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

\n", "type": "module", "displayName": "Event: `'lookup'`" }, { "textRaw": "Event: `'ready'`", "name": "event:_`'ready'`", "meta": { "added": [ "v9.11.0" ], "changes": [] }, "desc": "

Emitted when a socket is ready to be used.

\n

Triggered immediately after 'connect'.

", "type": "module", "displayName": "Event: `'ready'`" }, { "textRaw": "Event: `'timeout'`", "name": "event:_`'timeout'`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "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().

", "type": "module", "displayName": "Event: `'timeout'`" }, { "textRaw": "`socket.address()`", "name": "`socket.address()`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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' }

", "type": "module", "displayName": "`socket.address()`" }, { "textRaw": "`socket.bufferSize`", "name": "`socket.buffersize`", "meta": { "added": [ "v0.3.8" ], "changes": [] }, "desc": "\n

This property shows the number of characters buffered for writing. The buffer\nmay contain strings whose length after encoding is not yet known. So this number\nis only an approximation of the number of bytes in the buffer.

\n

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.

\n

The consequence of this internal buffering is that memory may grow.\nUsers who experience large or growing bufferSize should attempt to\n\"throttle\" the data flows in their program with\nsocket.pause() and socket.resume().

", "type": "module", "displayName": "`socket.bufferSize`" }, { "textRaw": "`socket.bytesRead`", "name": "`socket.bytesread`", "meta": { "added": [ "v0.5.3" ], "changes": [] }, "desc": "\n

The amount of received bytes.

", "type": "module", "displayName": "`socket.bytesRead`" }, { "textRaw": "`socket.bytesWritten`", "name": "`socket.byteswritten`", "meta": { "added": [ "v0.5.3" ], "changes": [] }, "desc": "\n

The amount of bytes sent.

", "type": "module", "displayName": "`socket.bytesWritten`" }, { "textRaw": "`socket.connect()`", "name": "`socket.connect()`", "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.

", "modules": [ { "textRaw": "`socket.connect(options[, connectListener])`", "name": "`socket.connect(options[,_connectlistener])`", "meta": { "added": [ "v0.1.90" ], "changes": [ { "version": "v12.10.0", "pr-url": "https://github.com/nodejs/node/pull/25436", "description": "Added `onread` option." }, { "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." } ] }, "desc": "\n

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\n

For both types, available options include:

\n\n

Following is an example of a client using the onread option:

\n
const net = require('net');\nnet.connect({\n  port: 80,\n  onread: {\n    // Reuses a 4KiB Buffer for every read from the socket.\n    buffer: Buffer.alloc(4 * 1024),\n    callback: function(nread, buf) {\n      // Received data is available in `buf` from 0 to `nread`.\n      console.log(buf.toString('utf8', 0, nread));\n    }\n  }\n});\n
", "type": "module", "displayName": "`socket.connect(options[, connectListener])`" }, { "textRaw": "`socket.connect(path[, connectListener])`", "name": "`socket.connect(path[,_connectlistener])`", "desc": "\n

Initiate an IPC connection on the given socket.

\n

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

", "type": "module", "displayName": "`socket.connect(path[, connectListener])`" }, { "textRaw": "`socket.connect(port[, host][, connectListener])`", "name": "`socket.connect(port[,_host][,_connectlistener])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

Initiate a TCP connection on the given socket.

\n

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

", "type": "module", "displayName": "`socket.connect(port[, host][, connectListener])`" } ], "type": "module", "displayName": "`socket.connect()`" }, { "textRaw": "`socket.connecting`", "name": "`socket.connecting`", "meta": { "added": [ "v6.1.0" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`socket.connecting`" }, { "textRaw": "`socket.destroy([exception])`", "name": "`socket.destroy([exception])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`socket.destroy([exception])`" }, { "textRaw": "`socket.destroyed`", "name": "`socket.destroyed`", "desc": "", "type": "module", "displayName": "`socket.destroyed`" }, { "textRaw": "`socket.end([data[, encoding]][, callback])`", "name": "`socket.end([data[,_encoding]][,_callback])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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().

", "type": "module", "displayName": "`socket.end([data[, encoding]][, callback])`" }, { "textRaw": "`socket.localAddress`", "name": "`socket.localaddress`", "meta": { "added": [ "v0.9.6" ], "changes": [] }, "desc": "\n

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'.

", "type": "module", "displayName": "`socket.localAddress`" }, { "textRaw": "`socket.localPort`", "name": "`socket.localport`", "meta": { "added": [ "v0.9.6" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "`socket.localPort`" }, { "textRaw": "`socket.pause()`", "name": "`socket.pause()`", "desc": "\n

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

", "type": "module", "displayName": "`socket.pause()`" }, { "textRaw": "`socket.pending`", "name": "`socket.pending`", "meta": { "added": [ "v11.2.0" ], "changes": [] }, "desc": "\n

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).

", "type": "module", "displayName": "`socket.pending`" }, { "textRaw": "`socket.ref()`", "name": "`socket.ref()`", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`socket.ref()`" }, { "textRaw": "`socket.remoteAddress`", "name": "`socket.remoteaddress`", "meta": { "added": [ "v0.5.10" ], "changes": [] }, "desc": "\n

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).

", "type": "module", "displayName": "`socket.remoteAddress`" }, { "textRaw": "`socket.remoteFamily`", "name": "`socket.remotefamily`", "meta": { "added": [ "v0.11.14" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "`socket.remoteFamily`" }, { "textRaw": "`socket.remotePort`", "name": "`socket.remoteport`", "meta": { "added": [ "v0.5.10" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "`socket.remotePort`" }, { "textRaw": "`socket.resume()`", "name": "`socket.resume()`", "desc": "\n

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

", "type": "module", "displayName": "`socket.resume()`" }, { "textRaw": "`socket.setEncoding([encoding])`", "name": "`socket.setencoding([encoding])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "`socket.setEncoding([encoding])`" }, { "textRaw": "`socket.setKeepAlive([enable][, initialDelay])`", "name": "`socket.setkeepalive([enable][,_initialdelay])`", "meta": { "added": [ "v0.1.92" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`socket.setKeepAlive([enable][, initialDelay])`" }, { "textRaw": "`socket.setNoDelay([noDelay])`", "name": "`socket.setnodelay([nodelay])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`socket.setNoDelay([noDelay])`" }, { "textRaw": "`socket.setTimeout(timeout[, callback])`", "name": "`socket.settimeout(timeout[,_callback])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`socket.setTimeout(timeout[, callback])`" }, { "textRaw": "`socket.unref()`", "name": "`socket.unref()`", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`socket.unref()`" }, { "textRaw": "`socket.write(data[, encoding][, callback])`", "name": "`socket.write(data[,_encoding][,_callback])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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, which may not be immediately.

\n

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

", "type": "module", "displayName": "`socket.write(data[, encoding][, callback])`" } ], "type": "module", "displayName": "Class: `net.Socket`" }, { "textRaw": "`net.connect()`", "name": "`net.connect()`", "desc": "

Aliases to\nnet.createConnection().

\n

Possible signatures:

\n", "modules": [ { "textRaw": "`net.connect(options[, connectListener])`", "name": "`net.connect(options[,_connectlistener])`", "meta": { "added": [ "v0.7.0" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "`net.connect(options[, connectListener])`" }, { "textRaw": "`net.connect(path[, connectListener])`", "name": "`net.connect(path[,_connectlistener])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "`net.connect(path[, connectListener])`" }, { "textRaw": "`net.connect(port[, host][, connectListener])`", "name": "`net.connect(port[,_host][,_connectlistener])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "`net.connect(port[, host][, connectListener])`" } ], "type": "module", "displayName": "`net.connect()`" }, { "textRaw": "`net.createConnection()`", "name": "`net.createconnection()`", "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.

", "modules": [ { "textRaw": "`net.createConnection(options[, connectListener])`", "name": "`net.createconnection(options[,_connectlistener])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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
", "type": "module", "displayName": "`net.createConnection(options[, connectListener])`" }, { "textRaw": "`net.createConnection(path[, connectListener])`", "name": "`net.createconnection(path[,_connectlistener])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`net.createConnection(path[, connectListener])`" }, { "textRaw": "`net.createConnection(port[, host][, connectListener])`", "name": "`net.createconnection(port[,_host][,_connectlistener])`", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`net.createConnection(port[, host][, connectListener])`" } ], "type": "module", "displayName": "`net.createConnection()`" }, { "textRaw": "`net.createServer([options][, connectionListener])`", "name": "`net.createserver([options][,_connectionlistener])`", "meta": { "added": [ "v0.5.0" ], "changes": [] }, "desc": "\n

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
", "type": "module", "displayName": "`net.createServer([options][, connectionListener])`" }, { "textRaw": "`net.isIP(input)`", "name": "`net.isip(input)`", "meta": { "added": [ "v0.3.0" ], "changes": [] }, "desc": "\n

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.

", "type": "module", "displayName": "`net.isIP(input)`" }, { "textRaw": "`net.isIPv4(input)`", "name": "`net.isipv4(input)`", "meta": { "added": [ "v0.3.0" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "`net.isIPv4(input)`" }, { "textRaw": "`net.isIPv6(input)`", "name": "`net.isipv6(input)`", "meta": { "added": [ "v0.3.0" ], "changes": [] }, "desc": "\n

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

", "type": "module", "displayName": "`net.isIPv6(input)`" } ], "type": "module", "displayName": "Net" } ] }