net
Stability: 2 - Stable
The node:net module provides an asynchronous network API for creating stream-based TCP or IPC servers (createServer) and clients (createConnection).
It can be accessed using:
import net from 'node:net';
Usage in Deno
import * as mod from "node:net";
class BlockList
Usage in Deno
import { BlockList } from "node:net";
The BlockList object can be used with some network APIs to specify rules for disabling inbound or outbound access to specific IP addresses, IP ranges, or IP subnets.
Properties
rules: readonly string[] The list of rules added to the blocklist.
Methods
addAddress(
address: string,
type?: IPVersion,
): void Adds a rule to block the given IP address.
addAddress(address: SocketAddress): void Adds a rule to block a range of IP addresses from start (inclusive) toend (inclusive).
addRange(
start: SocketAddress,
end: SocketAddress,
): void addSubnet(
net: SocketAddress,
prefix: number,
): void Adds a rule to block a range of IP addresses specified as a subnet mask.
check(address: SocketAddress): boolean Returns true if the given IP address matches any of the rules added to theBlockList.
const blockList = new net.BlockList();
blockList.addAddress('123.123.123.123');
blockList.addRange('10.0.0.1', '10.0.0.10');
blockList.addSubnet('8592:757c:efae:4e45::', 64, 'ipv6');
console.log(blockList.check('123.123.123.123')); // Prints: true
console.log(blockList.check('10.0.0.3')); // Prints: true
console.log(blockList.check('222.111.111.222')); // Prints: false
// IPv6 notation for IPv4 addresses works:
console.log(blockList.check('::ffff:7b7b:7b7b', 'ipv6')); // Prints: true
console.log(blockList.check('::ffff:123.123.123.123', 'ipv6')); // Prints: true
Static Methods
isBlockList(value: unknown): value is BlockList Returns true if the value is a net.BlockList.
class Server
Usage in Deno
import { Server } from "node:net";
This class is used to create a TCP or IPC server.
Constructors
Server(connectionListener?: (socket: Socket) => void) Server(
options?: ServerOpts,
connectionListener?: (socket: Socket) => void,
) Properties
connections: number listening: boolean Indicates whether or not the server is listening for connections.
maxConnections: number Set this property to reject connections when the server's connection count gets high.
It is not recommended to use this option once a socket has been sent to a child with child_process.fork().
Methods
[Symbol.asyncDispose](): Promise<void> Calls Server.close() and returns a promise that fulfills when the server has closed.
addListener(
event: string,
listener: (...args: any[]) => void,
): this events.EventEmitter
- close
- connection
- error
- listening
- drop
addListener(
event: "close",
listener: () => void,
): this addListener(
event: "connection",
listener: (socket: Socket) => void,
): this addListener(
event: "error",
listener: (err: Error) => void,
): this addListener(
event: "listening",
listener: () => void,
): this addListener(
event: "drop",
listener: (data?: DropArgument) => void,
): this address(): Returns the bound address, the address family name, and port of the server as reported by the operating system if listening on an IP socket (useful to find which port was assigned when getting an OS-assigned address):{ port: 12346, family: 'IPv4', address: '127.0.0.1' }.
For a server listening on a pipe or Unix domain socket, the name is returned as a string.
const server = net.createServer((socket) => {
socket.end('goodbye\n');
}).on('error', (err) => {
// Handle errors here.
throw err;
});
// Grab an arbitrary unused port.
server.listen(() => {
console.log('opened server on', server.address());
});
server.address() returns null before the 'listening' event has been emitted or after calling server.close().
close(callback?: (err?: Error) => void): this Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections are ended and the server emits a 'close' event. The optional callback will be called once the 'close' event occurs. Unlike that event, it will be called with an Error as its only argument if the server was not open when it was closed.
emit(
event: string | symbol,
...args: any[],
): boolean emit(event: "close"): boolean emit(
event: "error",
err: Error,
): boolean emit(event: "listening"): boolean emit(
event: "drop",
data?: DropArgument,
): boolean getConnections(cb: (
error: Error | null,
count: number,
) => void): void Asynchronously get the number of concurrent connections on the server. Works when sockets were sent to forks.
Callback should take two arguments err and count.
listen(
port?: number,
hostname?: string,
backlog?: number,
listeningListener?: () => void,
): this Start a server listening for connections. A net.Server can be a TCP or an IPC server depending on what it listens to.
Possible signatures:
server.listen(handle[, backlog][, callback])server.listen(options[, callback])-
server.listen(path[, backlog][, callback])forIPCservers -
server.listen([port[, host[, backlog]]][, callback])for TCP servers
This function is asynchronous. When the server starts listening, the 'listening' event will be emitted. The last parameter callbackwill be added as a listener for the 'listening' event.
All listen() methods can take a backlog parameter to specify the maximum length of the queue of pending connections. The actual length will be determined by the OS through sysctl settings such as tcp_max_syn_backlog and somaxconn on Linux. The default value of this parameter is 511 (not 512).
All Socket are set to SO_REUSEADDR (see socket(7) for details).
The server.listen() method can be called again if and only if there was an error during the first server.listen() call or server.close() has been called. Otherwise, an ERR_SERVER_ALREADY_LISTEN error will be thrown.
One of the most common errors raised when listening is EADDRINUSE. This happens when another server is already listening on the requestedport/path/handle. One way to handle this would be to retry after a certain amount of time:
server.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
console.error('Address in use, retrying...');
setTimeout(() => {
server.close();
server.listen(PORT, HOST);
}, 1000);
}
});
listen(
port?: number,
hostname?: string,
listeningListener?: () => void,
): this listen(
port?: number,
backlog?: number,
listeningListener?: () => void,
): this listen(
port?: number,
listeningListener?: () => void,
): this listen(
path: string,
backlog?: number,
listeningListener?: () => void,
): this listen(
path: string,
listeningListener?: () => void,
): this listen(
options: ListenOptions,
listeningListener?: () => void,
): this listen(
handle: any,
backlog?: number,
listeningListener?: () => void,
): this listen(
handle: any,
listeningListener?: () => void,
): this on(
event: string,
listener: (...args: any[]) => void,
): this on(
event: "close",
listener: () => void,
): this on(
event: "error",
listener: (err: Error) => void,
): this on(
event: "listening",
listener: () => void,
): this on(
event: "drop",
listener: (data?: DropArgument) => void,
): this once(
event: string,
listener: (...args: any[]) => void,
): this once(
event: "close",
listener: () => void,
): this once(
event: "error",
listener: (err: Error) => void,
): this once(
event: "listening",
listener: () => void,
): this once(
event: "drop",
listener: (data?: DropArgument) => void,
): this prependListener(
event: string,
listener: (...args: any[]) => void,
): this prependListener(
event: "close",
listener: () => void,
): this prependListener(
event: "connection",
listener: (socket: Socket) => void,
): this prependListener(
event: "error",
listener: (err: Error) => void,
): this prependListener(
event: "listening",
listener: () => void,
): this prependListener(
event: "drop",
listener: (data?: DropArgument) => void,
): this prependOnceListener(
event: string,
listener: (...args: any[]) => void,
): this prependOnceListener(
event: "close",
listener: () => void,
): this prependOnceListener(
event: "connection",
listener: (socket: Socket) => void,
): this prependOnceListener(
event: "error",
listener: (err: Error) => void,
): this prependOnceListener(
event: "listening",
listener: () => void,
): this prependOnceListener(
event: "drop",
listener: (data?: DropArgument) => void,
): this ref(): this Opposite of unref(), calling ref() on a previously unrefed server will not let the program exit if it's the only server left (the default behavior). If the server is refed calling ref() again will have no effect.
unref(): this Calling unref() on a server will allow the program to exit if this is the only active server in the event system. If the server is already unrefed callingunref() again will have no effect.
class Socket
Usage in Deno
import { Socket } from "node:net";
The fd option is not supported.
This class is an abstraction of a TCP socket or a streaming IPC endpoint (uses named pipes on Windows, and Unix domain sockets otherwise). It is also an EventEmitter.
A net.Socket can be created by the user and used directly to interact with a server. For example, it is returned by createConnection, so the user can use it to talk to the server.
It can also be created by Node.js and passed to the user when a connection is received. For example, it is passed to the listeners of a 'connection' event emitted on a Server, so the user can use it to interact with the client.
Constructors
Socket(options?: SocketConstructorOpts) Properties
autoSelectFamilyAttemptedAddresses: string[] This property is only present if the family autoselection algorithm is enabled in socket.connect(options) and it is an array of the addresses that have been attempted.
Each address is a string in the form of $IP:$PORT. If the connection was successful, then the last address is the one that the socket is currently connected to.
bufferSize: number This property shows the number of characters buffered for writing. The buffer may contain strings whose length after encoding is not yet known. So this number is only an approximation of the number of bytes in the buffer.
net.Socket has the property that socket.write() always works. This is to help users get up and running quickly. The computer cannot always keep up with the amount of data that is written to a socket. The network connection simply might be too slow. Node.js will internally queue up the data written to a socket and send it out over the wire when it is possible.
The consequence of this internal buffering is that memory may grow. Users who experience large or growing bufferSize should attempt to "throttle" the data flows in their program with socket.pause() and socket.resume().
bytesRead: number The amount of received bytes.
bytesWritten: number The amount of bytes sent.
connecting: boolean If true, socket.connect(options[, connectListener]) was called and has not yet finished. It will stay true until the socket becomes connected, then it is set to false and the 'connect' event is emitted. Note that the socket.connect(options[, connectListener]) callback is a listener for the 'connect' event.
destroyed: boolean See writable.destroyed for further details.
localAddress: string The string representation of the local IP address the remote client is connecting on. For example, in a server listening on '0.0.0.0', if a client connects on '192.168.1.1', the value of socket.localAddress would be'192.168.1.1'.
localFamily: string The string representation of the local IP family. 'IPv4' or 'IPv6'.
localPort: number The numeric representation of the local port. For example, 80 or 21.
pending: boolean This is true if the socket is not connected yet, either because .connect()has not yet been called or because it is still in the process of connecting (see socket.connecting).
readyState: SocketReadyState This property represents the state of the connection as a string.
- If the stream is connecting
socket.readyStateisopening. - If the stream is readable and writable, it is
open. - If the stream is readable and not writable, it is
readOnly. - If the stream is not readable and writable, it is
writeOnly.
remoteAddress: string | undefined The string representation of the remote IP address. For example,'74.125.127.100' or '2001:4860:a005::68'. Value may be undefined if the socket is destroyed (for example, if the client disconnected).
remoteFamily: string | undefined The string representation of the remote IP family. 'IPv4' or 'IPv6'. Value may be undefined if the socket is destroyed (for example, if the client disconnected).
remotePort: number | undefined The numeric representation of the remote port. For example, 80 or 21. Value may be undefined if the socket is destroyed (for example, if the client disconnected).
timeout: number | undefined The socket timeout in milliseconds as set by socket.setTimeout(). It is undefined if a timeout has not been set.
Methods
addListener(
event: string,
listener: (...args: any[]) => void,
): this events.EventEmitter
- close
- connect
- connectionAttempt
- connectionAttemptFailed
- connectionAttemptTimeout
- data
- drain
- end
- error
- lookup
- ready
- timeout
addListener(
event: "close",
listener: (hadError: boolean) => void,
): this addListener(
event: "connect",
listener: () => void,
): this addListener(
event: "connectionAttempt",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this addListener(
event: "connectionAttemptFailed",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this addListener(
event: "connectionAttemptTimeout",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this addListener(
event: "data",
listener: (data: Buffer) => void,
): this addListener(
event: "drain",
listener: () => void,
): this addListener(
event: "end",
listener: () => void,
): this addListener(
event: "error",
listener: (err: Error) => void,
): this addListener(
event: "lookup",
listener: (
err: Error,
address: string,
family: string | number,
host: string,
) => void,
): this addListener(
event: "ready",
listener: () => void,
): this addListener(
event: "timeout",
listener: () => void,
): this address(): AddressInfo | { } Returns the bound address, the address family name and port of the socket as reported by the operating system:{ port: 12346, family: 'IPv4', address: '127.0.0.1' }
connect(
options: SocketConnectOpts,
connectionListener?: () => void,
): this Initiate a connection on a given socket.
Possible signatures:
socket.connect(options[, connectListener])-
socket.connect(path[, connectListener])forIPCconnections. -
socket.connect(port[, host][, connectListener])for TCP connections. - Returns:
net.SocketThe socket itself.
This function is asynchronous. When the connection is established, the 'connect' event will be emitted. If there is a problem connecting, instead of a 'connect' event, an 'error' event will be emitted with the error passed to the 'error' listener. The last parameter connectListener, if supplied, will be added as a listener for the 'connect' event once.
This function should only be used for reconnecting a socket after'close' has been emitted or otherwise it may lead to undefined behavior.
connect(
port: number,
host: string,
connectionListener?: () => void,
): this connect(
port: number,
connectionListener?: () => void,
): this connect(
path: string,
connectionListener?: () => void,
): this destroySoon(): void Destroys the socket after all data is written. If the finish event was already emitted the socket is destroyed immediately. If the socket is still writable it implicitly calls socket.end().
emit(
event: string | symbol,
...args: any[],
): boolean emit(
event: "close",
hadError: boolean,
): boolean emit(event: "connect"): boolean emit(
event: "connectionAttempt",
ip: string,
port: number,
family: number,
): boolean emit(
event: "connectionAttemptFailed",
ip: string,
port: number,
family: number,
): boolean emit(
event: "connectionAttemptTimeout",
ip: string,
port: number,
family: number,
): boolean emit(
event: "data",
data: Buffer,
): boolean emit(event: "drain"): boolean emit(event: "end"): boolean emit(
event: "error",
err: Error,
): boolean emit(
event: "lookup",
err: Error,
address: string,
family: string | number,
host: string,
): boolean emit(event: "ready"): boolean emit(event: "timeout"): boolean end(callback?: () => void): this Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.
See writable.end() for further details.
end(
buffer: Uint8Array | string,
callback?: () => void,
): this end(
str: Uint8Array | string,
encoding?: BufferEncoding,
callback?: () => void,
): this on(
event: string,
listener: (...args: any[]) => void,
): this on(
event: "close",
listener: (hadError: boolean) => void,
): this on(
event: "connect",
listener: () => void,
): this on(
event: "connectionAttempt",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this on(
event: "connectionAttemptFailed",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this on(
event: "connectionAttemptTimeout",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this on(
event: "data",
listener: (data: Buffer) => void,
): this on(
event: "drain",
listener: () => void,
): this on(
event: "end",
listener: () => void,
): this on(
event: "error",
listener: (err: Error) => void,
): this on(
event: "lookup",
listener: (
err: Error,
address: string,
family: string | number,
host: string,
) => void,
): this on(
event: "ready",
listener: () => void,
): this on(
event: "timeout",
listener: () => void,
): this once(
event: string,
listener: (...args: any[]) => void,
): this once(
event: "close",
listener: (hadError: boolean) => void,
): this once(
event: "connectionAttempt",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this once(
event: "connectionAttemptFailed",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this once(
event: "connectionAttemptTimeout",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this once(
event: "connect",
listener: () => void,
): this once(
event: "data",
listener: (data: Buffer) => void,
): this once(
event: "drain",
listener: () => void,
): this once(
event: "end",
listener: () => void,
): this once(
event: "error",
listener: (err: Error) => void,
): this once(
event: "lookup",
listener: (
err: Error,
address: string,
family: string | number,
host: string,
) => void,
): this once(
event: "ready",
listener: () => void,
): this once(
event: "timeout",
listener: () => void,
): this pause(): this Pauses the reading of data. That is, 'data' events will not be emitted. Useful to throttle back an upload.
prependListener(
event: string,
listener: (...args: any[]) => void,
): this prependListener(
event: "close",
listener: (hadError: boolean) => void,
): this prependListener(
event: "connect",
listener: () => void,
): this prependListener(
event: "connectionAttempt",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this prependListener(
event: "connectionAttemptFailed",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this prependListener(
event: "connectionAttemptTimeout",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this prependListener(
event: "data",
listener: (data: Buffer) => void,
): this prependListener(
event: "drain",
listener: () => void,
): this prependListener(
event: "end",
listener: () => void,
): this prependListener(
event: "error",
listener: (err: Error) => void,
): this prependListener(
event: "lookup",
listener: (
err: Error,
address: string,
family: string | number,
host: string,
) => void,
): this prependListener(
event: "ready",
listener: () => void,
): this prependListener(
event: "timeout",
listener: () => void,
): this prependOnceListener(
event: string,
listener: (...args: any[]) => void,
): this prependOnceListener(
event: "close",
listener: (hadError: boolean) => void,
): this prependOnceListener(
event: "connect",
listener: () => void,
): this prependOnceListener(
event: "connectionAttempt",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this prependOnceListener(
event: "connectionAttemptFailed",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this prependOnceListener(
event: "connectionAttemptTimeout",
listener: (
ip: string,
port: number,
family: number,
) => void,
): this prependOnceListener(
event: "data",
listener: (data: Buffer) => void,
): this prependOnceListener(
event: "drain",
listener: () => void,
): this prependOnceListener(
event: "end",
listener: () => void,
): this prependOnceListener(
event: "error",
listener: (err: Error) => void,
): this prependOnceListener(
event: "lookup",
listener: (
err: Error,
address: string,
family: string | number,
host: string,
) => void,
): this prependOnceListener(
event: "ready",
listener: () => void,
): this prependOnceListener(
event: "timeout",
listener: () => void,
): this ref(): this Opposite of unref(), calling ref() on a previously unrefed socket will not let the program exit if it's the only socket left (the default behavior). If the socket is refed calling ref again will have no effect.
resetAndDestroy(): this Close the TCP connection by sending an RST packet and destroy the stream. If this TCP socket is in connecting status, it will send an RST packet and destroy this TCP socket once it is connected. Otherwise, it will call socket.destroy with an ERR_SOCKET_CLOSED Error. If this is not a TCP socket (for example, a pipe), calling this method will immediately throw an ERR_INVALID_HANDLE_TYPE Error.
resume(): this Resumes reading after a call to socket.pause().
setEncoding(encoding?: BufferEncoding): this Set the encoding for the socket as a Readable Stream. See readable.setEncoding() for more information.
setKeepAlive(
enable?: boolean,
initialDelay?: number,
): this Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket.
Set initialDelay (in milliseconds) to set the delay between the last data packet received and the first keepalive probe. Setting 0 forinitialDelay will leave the value unchanged from the default (or previous) setting.
Enabling the keep-alive functionality will set the following socket options:
SO_KEEPALIVE=1TCP_KEEPIDLE=initialDelayTCP_KEEPCNT=10TCP_KEEPINTVL=1
setNoDelay(noDelay?: boolean): this Enable/disable the use of Nagle's algorithm.
When a TCP connection is created, it will have Nagle's algorithm enabled.
Nagle's algorithm delays data before it is sent via the network. It attempts to optimize throughput at the expense of latency.
Passing true for noDelay or not passing an argument will disable Nagle's algorithm for the socket. Passing false for noDelay will enable Nagle's algorithm.
setTimeout(
timeout: number,
callback?: () => void,
): this Sets the socket to timeout after timeout milliseconds of inactivity on the socket. By default net.Socket do not have a timeout.
When an idle timeout is triggered the socket will receive a 'timeout' event but the connection will not be severed. The user must manually call socket.end() or socket.destroy() to end the connection.
socket.setTimeout(3000);
socket.on('timeout', () => {
console.log('socket timeout');
socket.end();
});
If timeout is 0, then the existing idle timeout is disabled.
The optional callback parameter will be added as a one-time listener for the 'timeout' event.
unref(): this Calling unref() on a socket will allow the program to exit if this is the only active socket in the event system. If the socket is already unrefed callingunref() again will have no effect.
write(
buffer: Uint8Array | string,
cb?: (err?: Error) => void,
): boolean Sends data on the socket. The second parameter specifies the encoding in the case of a string. It defaults to UTF8 encoding.
Returns true if the entire data was flushed successfully to the kernel buffer. Returns false if all or part of the data was queued in user memory.'drain' will be emitted when the buffer is again free.
The optional callback parameter will be executed when the data is finally written out, which may not be immediately.
See Writable stream write() method for more information.
write(
str: Uint8Array | string,
encoding?: BufferEncoding,
cb?: (err?: Error) => void,
): boolean class SocketAddress
Usage in Deno
import { SocketAddress } from "node:net";
Constructors
SocketAddress(options: SocketAddressInitOptions) Properties
Static Methods
parse(input: string): SocketAddress | undefined function connect
Usage in Deno
import { connect } from "node:net";
Overload 1
connect(
options: NetConnectOpts,
connectionListener?: () => void,
): SocketAliases to createConnection.
Possible signatures:
Parameters
options: NetConnectOpts connectionListener: () => void Return Type
Overload 2
connect(
port: number,
host?: string,
connectionListener?: () => void,
): SocketParameters
port: number host: string connectionListener: () => void Return Type
Overload 3
function createConnection
Usage in Deno
import { createConnection } from "node:net";
Overload 1
createConnection(
options: NetConnectOpts,
connectionListener?: () => void,
): SocketA factory function, which creates a new 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:
- createConnection
-
createConnection for
IPCconnections. - createConnection for TCP connections.
The connect function is an alias to this function.
Parameters
options: NetConnectOpts connectionListener: () => void Return Type
Overload 2
createConnection(
port: number,
host?: string,
connectionListener?: () => void,
): SocketParameters
port: number host: string connectionListener: () => void Return Type
Overload 3
function createServer
Usage in Deno
import { createServer } from "node:net";
Overload 1
createServer(connectionListener?: (socket: Socket) => void): ServerCreates a new TCP or IPC server.
If allowHalfOpen is set to true, when the other end of the socket signals the end of transmission, the server will only send back the end of transmission when socket.end() is explicitly called. For example, in the context of TCP, when a FIN packed is received, a FIN packed is sent back only when socket.end() is explicitly called. Until then the connection is half-closed (non-readable but still writable). See 'end' event and RFC 1122 (section 4.2.2.13) for more information.
If pauseOnConnect is set to true, then the socket associated with each incoming connection will be paused, and no data will be read from its handle. This allows connections to be passed between processes without any data being read by the original process. To begin reading data from a paused socket, call socket.resume().
The server can be a TCP server or an IPC server, depending on what it listen() to.
Here is an example of a TCP echo server which listens for connections on port 8124:
import net from 'node:net';
const server = net.createServer((c) => {
// 'connection' listener.
console.log('client connected');
c.on('end', () => {
console.log('client disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.on('error', (err) => {
throw err;
});
server.listen(8124, () => {
console.log('server bound');
});
Test this by using telnet:
telnet localhost 8124
To listen on the socket /tmp/echo.sock:
server.listen('/tmp/echo.sock', () => {
console.log('server bound');
});
Use nc to connect to a Unix domain socket server:
nc -U /tmp/echo.sock
Parameters
connectionListener: (socket: Socket) => void Automatically set as a listener for the 'connection' event.
Return Type
Overload 2
createServer(
options?: ServerOpts,
connectionListener?: (socket: Socket) => void,
): ServerParameters
options: ServerOpts connectionListener: (socket: Socket) => void Return Type
function getDefaultAutoSelectFamily
Usage in Deno
import { getDefaultAutoSelectFamily } from "node:net";
getDefaultAutoSelectFamily(): booleanGets the current default value of the autoSelectFamily option of socket.connect(options). The initial default value is true, unless the command line option--no-network-family-autoselection is provided.
Return Type
boolean function getDefaultAutoSelectFamilyAttemptTimeout
Usage in Deno
import { getDefaultAutoSelectFamilyAttemptTimeout } from "node:net";
getDefaultAutoSelectFamilyAttemptTimeout(): numberGets the current default value of the autoSelectFamilyAttemptTimeout option of socket.connect(options). The initial default value is 250 or the value specified via the command line option --network-family-autoselection-attempt-timeout.
Return Type
number The current default value of the autoSelectFamilyAttemptTimeout option.
function isIP
Usage in Deno
import { isIP } from "node:net";
isIP(input: string): numberReturns 6 if input is an IPv6 address. Returns 4 if input is an IPv4 address in dot-decimal notation with no leading zeroes. Otherwise, returns0.
net.isIP('::1'); // returns 6
net.isIP('127.0.0.1'); // returns 4
net.isIP('127.000.000.001'); // returns 0
net.isIP('127.0.0.1/24'); // returns 0
net.isIP('fhqwhgads'); // returns 0
Parameters
input: string Return Type
number function isIPv4
Usage in Deno
import { isIPv4 } from "node:net";
isIPv4(input: string): booleanReturns true if input is an IPv4 address in dot-decimal notation with no leading zeroes. Otherwise, returns false.
net.isIPv4('127.0.0.1'); // returns true
net.isIPv4('127.000.000.001'); // returns false
net.isIPv4('127.0.0.1/24'); // returns false
net.isIPv4('fhqwhgads'); // returns false
Parameters
input: string Return Type
boolean function isIPv6
Usage in Deno
import { isIPv6 } from "node:net";
isIPv6(input: string): booleanReturns true if input is an IPv6 address. Otherwise, returns false.
net.isIPv6('::1'); // returns true
net.isIPv6('fhqwhgads'); // returns false
Parameters
input: string Return Type
boolean function setDefaultAutoSelectFamily
Usage in Deno
import { setDefaultAutoSelectFamily } from "node:net";
setDefaultAutoSelectFamily(value: boolean): voidSets the default value of the autoSelectFamily option of socket.connect(options).
Parameters
value: boolean The new default value. The initial default value is true, unless the command line option --no-network-family-autoselection is provided.
Return Type
void function setDefaultAutoSelectFamilyAttemptTimeout
Usage in Deno
import { setDefaultAutoSelectFamilyAttemptTimeout } from "node:net";
setDefaultAutoSelectFamilyAttemptTimeout(value: number): voidSets the default value of the autoSelectFamilyAttemptTimeout option of socket.connect(options).
Parameters
value: number The new default value, which must be a positive number. If the number is less than 10, the value 10 is used instead. The initial default value is 250 or the value specified via the command line option --network-family-autoselection-attempt-timeout.
Return Type
void interface AddressInfo
Usage in Deno
import { type AddressInfo } from "node:net";
interface DropArgument
Usage in Deno
import { type DropArgument } from "node:net";
Properties
localAddress: string localPort: number localFamily: string remoteAddress: string remotePort: number remoteFamily: string interface IpcNetConnectOpts
Usage in Deno
import { type IpcNetConnectOpts } from "node:net";
Properties
timeout: number | undefined interface IpcSocketConnectOpts
Usage in Deno
import { type IpcSocketConnectOpts } from "node:net";
Properties
path: string interface ListenOptions
Usage in Deno
import { type ListenOptions } from "node:net";
Properties
backlog: number | undefined exclusive: boolean | undefined host: string | undefined ipv6Only: boolean | undefined reusePort: boolean | undefined path: string | undefined port: number | undefined readableAll: boolean | undefined writableAll: boolean | undefined interface OnReadOpts
Usage in Deno
import { type OnReadOpts } from "node:net";
Properties
buffer: Uint8Array | (() => Uint8Array) Methods
callback(
bytesWritten: number,
buffer: Uint8Array,
): boolean This function is called for every chunk of incoming data. Two arguments are passed to it: the number of bytes written to buffer and a reference to buffer. Return false from this function to implicitly pause() the socket.
interface ServerOpts
Usage in Deno
import { type ServerOpts } from "node:net";
Properties
allowHalfOpen: boolean | undefined Indicates whether half-opened TCP connections are allowed.
pauseOnConnect: boolean | undefined Indicates whether the socket should be paused on incoming connections.
noDelay: boolean | undefined If set to true, it disables the use of Nagle's algorithm immediately after a new incoming connection is received.
keepAlive: boolean | undefined If set to true, it enables keep-alive functionality on the socket immediately after a new incoming connection is received, similarly on what is done in socket.setKeepAlive([enable][, initialDelay]).
keepAliveInitialDelay: number | undefined If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket.
highWaterMark: number | undefined Optionally overrides all net.Sockets' readableHighWaterMark and writableHighWaterMark.
blockList can be used for disabling inbound access to specific IP addresses, IP ranges, or IP subnets. This does not work if the server is behind a reverse proxy, NAT, etc. because the address checked against the block list is the address of the proxy, or the one specified by the NAT.
interface SocketAddressInitOptions
Usage in Deno
import { type SocketAddressInitOptions } from "node:net";
interface SocketConstructorOpts
Usage in Deno
import { type SocketConstructorOpts } from "node:net";
Properties
fd: number | undefined allowHalfOpen: boolean | undefined onread: OnReadOpts | undefined readable: boolean | undefined writable: boolean | undefined signal: AbortSignal interface TcpNetConnectOpts
Usage in Deno
import { type TcpNetConnectOpts } from "node:net";
Properties
timeout: number | undefined interface TcpSocketConnectOpts
Usage in Deno
import { type TcpSocketConnectOpts } from "node:net";
Properties
port: number host: string | undefined localAddress: string | undefined localPort: number | undefined hints: number | undefined family: number | undefined lookup: LookupFunction | undefined noDelay: boolean | undefined keepAlive: boolean | undefined keepAliveInitialDelay: number | undefined autoSelectFamily: boolean | undefined autoSelectFamilyAttemptTimeout: number | undefined interface ConnectOpts
Usage in Deno
import { type ConnectOpts } from "node:net";
type alias IPVersion
Usage in Deno
import { type IPVersion } from "node:net";
Definition
"ipv4" | "ipv6" type alias LookupFunction
Usage in Deno
import { type LookupFunction } from "node:net";
Definition
(
hostname: string,
options: dns.LookupOptions,
callback: (
err: ErrnoException | null,
address: string | dns.LookupAddress[],
family?: number,
) => void,
) => void type alias SocketConnectOpts
Usage in Deno
import { type SocketConnectOpts } from "node:net";
Definition
type alias SocketReadyState
Usage in Deno
import { type SocketReadyState } from "node:net";
Definition
"opening"
| "open"
| "readOnly"
| "writeOnly"
| "closed"
© 2018–2025 the Deno authors
Licensed under the MIT License.
https://docs.deno.com/api/node/net