node:net
Node-compatible TCP/IPC servers, sockets, and IP address utilities (node:net).
Available in vjs only (not available in vjs-lite). Enabled by default
unless --no-node-compat is passed.
import ... from "node:net"
node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)Example
Section titled “Example”import { createServer, connect } from "node:net";
const server = createServer((socket) => { socket.end("hello\n");});server.listen(0, "127.0.0.1");createServervjs onlyv0.0.0-alpha.2Server.createConnectionvjs onlyv0.0.0-alpha.2options.connectvjs onlyv0.0.0-alpha.2createConnection(options, connectionListener).isIPvjs onlyv0.0.0-alpha.2input is an IPv4 (4), IPv6 (6), or invalid (0) IP address.isIPv4vjs onlyv0.0.0-alpha.2true if input is a valid IPv4 address.isIPv6vjs onlyv0.0.0-alpha.2true if input is a valid IPv6 address.getDefaultAutoSelectFamilyvjs onlyv0.0.0-alpha.2autoSelectFamily socket option.setDefaultAutoSelectFamilyvjs onlyv0.0.0-alpha.2autoSelectFamily socket option.getDefaultAutoSelectFamilyAttemptTimeoutvjs onlyv0.0.0-alpha.2autoSelectFamilyAttemptTimeout in milliseconds.setDefaultAutoSelectFamilyAttemptTimeoutvjs onlyv0.0.0-alpha.2autoSelectFamilyAttemptTimeout in milliseconds.SocketAddressvjs onlyv0.0.0-alpha.2BlockListvjs onlyv0.0.0-alpha.2Socketvjs onlyv0.0.0-alpha.2Servervjs onlyv0.0.0-alpha.2Socket connections.AbortSignalLikevjs onlyv0.0.0-alpha.2net listen and socket options.AddressInfovjs onlyv0.0.0-alpha.2.address().SocketAddressInitOptionsvjs onlyv0.0.0-alpha.2new SocketAddress(options).SocketConstructorOptsvjs onlyv0.0.0-alpha.2new Socket(options).TcpSocketConnectOptsvjs onlyv0.0.0-alpha.2connect(), createConnection(), and socket.connect().IpcSocketConnectOptsvjs onlyv0.0.0-alpha.2ListenOptionsvjs onlyv0.0.0-alpha.2server.listen(options).ServerOptsvjs onlyv0.0.0-alpha.2createServer() and new Server().IPVersionvjs onlyv0.0.0-alpha.2LookupFunctionvjs onlyv0.0.0-alpha.2connect().SocketConnectOptsvjs onlyv0.0.0-alpha.2connect() and createConnection().NetConnectOptsvjs onlyv0.0.0-alpha.2SocketConnectOpts.Functions
Section titled “Functions”createServer
Section titled “createServer”export function createServer(connectionListener?: (socket: Socket) => void): Server;
export function createServer(
options?: ServerOpts,
connectionListener?: (socket: Socket) => void,
): Server;
Create a new TCP or IPC Server.
createServer(connectionListener?)| Name | Type | Description |
|---|---|---|
connectionListener optional | (socket: Socket) => void | Optional "connection" event listener. |
Server — New Server instance.createServer(options?, connectionListener?)Create a new TCP or IPC Server with explicit options.
| Name | Type | Description |
|---|---|---|
options optional | ServerOpts | Server socket options. |
connectionListener optional | (socket: Socket) => void | Optional "connection" event listener. |
Server — New Server instance.createConnection
Section titled “createConnection”export function createConnection(
options: SocketConnectOpts,
connectionListener?: () => void,
): Socket;
export function createConnection(
port: number,
host?: string,
connectionListener?: () => void,
): Socket;
export function createConnection(path: string, connectionListener?: () => void): Socket;
Open a TCP or IPC socket connection using options.
createConnection(options, connectionListener?)| Name | Type | Description |
|---|---|---|
options | SocketConnectOpts | TCP or IPC connection options. |
connectionListener optional | () => void | Optional "connect" listener. |
Socket — Connected Socket.createConnection(port, host?, connectionListener?)Open a TCP socket connection to port and optional host.
| Name | Type | Description |
|---|---|---|
port | number | Target TCP port. |
host optional | string | Target hostname or IP (default "localhost"). |
connectionListener optional | () => void | Optional "connect" listener. |
Socket — Connected Socket.createConnection(path, connectionListener?)Open an IPC socket connection to path.
| Name | Type | Description |
|---|---|---|
path | string | Unix socket or Windows named pipe path. |
connectionListener optional | () => void | Optional "connect" listener. |
Socket — Connected Socket.connect
Section titled “connect”export function connect(options: SocketConnectOpts, connectionListener?: () => void): Socket;
export function connect(port: number, host?: string, connectionListener?: () => void): Socket;
export function connect(path: string, connectionListener?: () => void): Socket;
Alias for createConnection(options, connectionListener).
connect(options, connectionListener?)| Name | Type | Description |
|---|---|---|
options | SocketConnectOpts | — |
connectionListener optional | () => void | — |
connect(port, host?, connectionListener?)Alias for createConnection(port, host, connectionListener).
| Name | Type | Description |
|---|---|---|
port | number | — |
host optional | string | — |
connectionListener optional | () => void | — |
connect(path, connectionListener?)Alias for createConnection(path, connectionListener).
| Name | Type | Description |
|---|---|---|
path | string | — |
connectionListener optional | () => void | — |
export function isIP(input: string): 0 | 4 | 6;
Test whether input is an IPv4 (4), IPv6 (6), or invalid (0) IP address.
| Name | Type | Description |
|---|---|---|
input | string | Address string to inspect. |
0 | 4 | 6 — 4, 6, or 0.isIPv4
Section titled “isIPv4”export function isIPv4(input: string): boolean;
Return true if input is a valid IPv4 address.
| Name | Type | Description |
|---|---|---|
input | string | Address string to inspect. |
boolean — true if input is IPv4, otherwise false.isIPv6
Section titled “isIPv6”export function isIPv6(input: string): boolean;
Return true if input is a valid IPv6 address.
| Name | Type | Description |
|---|---|---|
input | string | Address string to inspect. |
boolean — true if input is IPv6, otherwise false.getDefaultAutoSelectFamily
Section titled “getDefaultAutoSelectFamily”export function getDefaultAutoSelectFamily(): boolean;
Return the process-wide default for the autoSelectFamily socket option.
boolean — true if Happy Eyeballs auto-selection is enabled by default.setDefaultAutoSelectFamily
Section titled “setDefaultAutoSelectFamily”export function setDefaultAutoSelectFamily(value: boolean): void;
Set the process-wide default for the autoSelectFamily socket option.
| Name | Type | Description |
|---|---|---|
value | boolean | New default boolean value. |
voidgetDefaultAutoSelectFamilyAttemptTimeout
Section titled “getDefaultAutoSelectFamilyAttemptTimeout”export function getDefaultAutoSelectFamilyAttemptTimeout(): number;
Return the process-wide default autoSelectFamilyAttemptTimeout in milliseconds.
number — Timeout in milliseconds.setDefaultAutoSelectFamilyAttemptTimeout
Section titled “setDefaultAutoSelectFamilyAttemptTimeout”export function setDefaultAutoSelectFamilyAttemptTimeout(value: number): void;
Set the process-wide default autoSelectFamilyAttemptTimeout in milliseconds.
| Name | Type | Description |
|---|---|---|
value | number | Positive integer timeout in milliseconds. |
voidClasses
Section titled “Classes”SocketAddress
Section titled “SocketAddress”export class SocketAddress {
readonly address: string;
readonly family: IPVersion;
readonly flowlabel: number;
readonly port: number;
constructor(options?: SocketAddressInitOptions);
static parse(input: string): SocketAddress | undefined;
toJSON(): { address: string; family: IPVersion; flowlabel: number; port: number };
}
Immutable representation of an IPv4 or IPv6 socket endpoint.
constructor(options?: SocketAddressInitOptions);
Construct a SocketAddress.
| Name | Type | Description |
|---|---|---|
options optional | SocketAddressInitOptions | Optional address, family, flowlabel, and port settings. |
| Property | Type | Description | Since |
|---|---|---|---|
address readonly | string | IP address string. | v0.0.0-alpha.2 |
family readonly | IPVersion | IP address family ("ipv4" or "ipv6"). | v0.0.0-alpha.2 |
flowlabel readonly | number | IPv6 flow label. | v0.0.0-alpha.2 |
port readonly | number | TCP/UDP port number. | v0.0.0-alpha.2 |
SocketAddress.parse
Section titled “SocketAddress.parse”static parse(input: string): SocketAddress | undefined;
Parse an IP-port string into a SocketAddress, or return undefined if invalid.
| Name | Type | Description |
|---|---|---|
input | string | Address string (for example "127.0.0.1:8080" or "[::1]:8080"). |
SocketAddress.toJSON
Section titled “SocketAddress.toJSON”toJSON(): { address: string; family: IPVersion; flowlabel: number; port: number };
Return a plain JSON-serializable { address, family, flowlabel, port } object.
{ address: string; family: IPVersion; flowlabel: number; port: number } — Socket address summary object.BlockList
Section titled “BlockList”export class BlockList {
readonly rules: readonly string[];
constructor();
addAddress(address: string | SocketAddress, type?: IPVersion): void;
addRange(start: string | SocketAddress, end: string | SocketAddress, type?: IPVersion): void;
addSubnet(net: string | SocketAddress, prefix: number, type?: IPVersion): void;
check(address: string | SocketAddress, type?: IPVersion): boolean;
static isBlockList(value: unknown): value is BlockList;
}
Rule set for matching IPv4 and IPv6 addresses, ranges, and CIDR subnets.
constructor();
Construct an empty BlockList.
| Property | Type | Description | Since |
|---|---|---|---|
rules readonly | readonly string[] | Ordered list of human-readable blocklist rules. | v0.0.0-alpha.2 |
BlockList.addAddress
Section titled “BlockList.addAddress”addAddress(address: string | SocketAddress, type?: IPVersion): void;
Add an exact IP address rule to the blocklist.
| Name | Type | Description |
|---|---|---|
address | string | SocketAddress | IP address string or SocketAddress. |
type optional | IPVersion | Optional IP family when address is a string. |
voidBlockList.addRange
Section titled “BlockList.addRange”addRange(start: string | SocketAddress, end: string | SocketAddress, type?: IPVersion): void;
Add an inclusive IP address range rule to the blocklist.
| Name | Type | Description |
|---|---|---|
start | string | SocketAddress | Start IP address string or SocketAddress. |
end | string | SocketAddress | End IP address string or SocketAddress. |
type optional | IPVersion | Optional IP family when string addresses are supplied. |
voidBlockList.addSubnet
Section titled “BlockList.addSubnet”addSubnet(net: string | SocketAddress, prefix: number, type?: IPVersion): void;
Add a CIDR subnet rule to the blocklist.
| Name | Type | Description |
|---|---|---|
net | string | SocketAddress | Network base IP string or SocketAddress. |
prefix | number | CIDR prefix length (0..=32 for IPv4, 0..=128 for IPv6). |
type optional | IPVersion | Optional IP family when net is a string. |
voidBlockList.check
Section titled “BlockList.check”check(address: string | SocketAddress, type?: IPVersion): boolean;
Return true if address matches any rule in this BlockList.
| Name | Type | Description |
|---|---|---|
address | string | SocketAddress | IP address string or SocketAddress to test. |
type optional | IPVersion | Optional IP family when address is a string. |
boolean — true if blocked, otherwise false.BlockList.isBlockList
Section titled “BlockList.isBlockList”static isBlockList(value: unknown): value is BlockList;
Type guard testing whether value is a BlockList instance.
| Name | Type | Description |
|---|---|---|
value | unknown | Value to inspect. |
Socket
Section titled “Socket”export class Socket {
readonly connecting: boolean;
readonly pending: boolean;
readonly destroyed: boolean;
readonly readyState: "opening" | "open" | "readOnly" | "writeOnly" | "closed";
readonly localAddress?: string;
readonly localPort?: number;
readonly localFamily?: string;
readonly remoteAddress?: string;
readonly remotePort?: number;
readonly remoteFamily?: string;
readonly bytesRead: number;
readonly bytesWritten: number;
readonly bufferSize: number;
readonly autoSelectFamilyAttemptedAddresses?: string[];
readonly timeout?: number;
allowHalfOpen: boolean;
readonly readable: boolean;
readonly writable: boolean;
readonly readableEnded: boolean;
readonly writableEnded: boolean;
readonly writableFinished: boolean;
readonly closed: boolean;
readonly errored: Error | null;
constructor(options?: SocketConstructorOpts);
connect(options: SocketConnectOpts, connectionListener?: () => void): this;
connect(port: number, host?: string, connectionListener?: () => void): this;
connect(port: number, connectionListener?: () => void): this;
connect(path: string, connectionListener?: () => void): this;
address(): AddressInfo | Record<string, never>;
write(chunk: string | Uint8Array, callback?: (err?: Error | null) => void): boolean;
write(chunk: string, encoding: string, callback?: (err?: Error | null) => void): boolean;
end(callback?: () => void): this;
end(chunk: string | Uint8Array, callback?: () => void): this;
end(chunk: string, encoding: string, callback?: () => void): this;
destroy(error?: Error): this;
destroySoon(): void;
pause(): this;
resume(): this;
read(size?: number): any;
setEncoding(encoding?: string): this;
setTimeout(timeout: number, callback?: () => void): this;
setNoDelay(noDelay?: boolean): this;
setKeepAlive(enable?: boolean, initialDelay?: number): this;
ref(): this;
unref(): this;
resetAndDestroy(): this;
pipe<T extends { write(chunk: any, ...args: any[]): any; end(...args: any[]): any }>(
destination: T,
options?: { end?: boolean },
): T;
on(eventName: string | symbol, listener: (...args: any[]) => void): this;
once(eventName: string | symbol, listener: (...args: any[]) => void): this;
off(eventName: string | symbol, listener: (...args: any[]) => void): this;
addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
removeAllListeners(eventName?: string | symbol): this;
emit(eventName: string | symbol, ...args: any[]): boolean;
listeners(eventName: string | symbol): Array<(...args: any[]) => void>;
listenerCount(eventName: string | symbol, listener?: (...args: any[]) => void): number;
eventNames(): Array<string | symbol>;
[Symbol.asyncIterator](): AsyncIterableIterator<any>;
}
Bidirectional TCP or IPC stream socket.
constructor(options?: SocketConstructorOpts);
Construct a new Socket.
| Name | Type | Description |
|---|---|---|
options optional | SocketConstructorOpts | Optional socket configuration. |
| Property | Type | Description | Since |
|---|---|---|---|
connecting readonly | boolean | true while a connect() handshake is still in progress. | v0.0.0-alpha.2 |
pending readonly | boolean | true if the socket is not yet connected. | v0.0.0-alpha.2 |
destroyed readonly | boolean | true after destroy() has been invoked. | v0.0.0-alpha.2 |
readyState readonly | "opening" | "open" | "readOnly" | "writeOnly" | "closed" | Current socket lifecycle state. | v0.0.0-alpha.2 |
localAddress readonly optional | string | Bound local IP address, if connected. | v0.0.0-alpha.2 |
localPort readonly optional | number | Bound local port, if connected. | v0.0.0-alpha.2 |
localFamily readonly optional | string | Local address family ("IPv4" or "IPv6"). | v0.0.0-alpha.2 |
remoteAddress readonly optional | string | Remote peer IP address, if connected. | v0.0.0-alpha.2 |
remotePort readonly optional | number | Remote peer port, if connected. | v0.0.0-alpha.2 |
remoteFamily readonly optional | string | Remote address family ("IPv4" or "IPv6"). | v0.0.0-alpha.2 |
bytesRead readonly | number | Total bytes received on this socket. | v0.0.0-alpha.2 |
bytesWritten readonly | number | Total bytes sent on this socket. | v0.0.0-alpha.2 |
bufferSize readonly | number | Bytes currently buffered for writing. | v0.0.0-alpha.2 |
autoSelectFamilyAttemptedAddresses readonly optional | string[] | Addresses attempted during Happy Eyeballs auto-selection. | v0.0.0-alpha.2 |
timeout readonly optional | number | Configured inactivity timeout in milliseconds, if set. | v0.0.0-alpha.2 |
allowHalfOpen | boolean | Whether the socket remains half-open after remote FIN. | v0.0.0-alpha.2 |
readable readonly | boolean | Whether the readable side is currently readable. | v0.0.0-alpha.2 |
writable readonly | boolean | Whether the writable side is currently writable. | v0.0.0-alpha.2 |
readableEnded readonly | boolean | Whether "end" has been emitted. | v0.0.0-alpha.2 |
writableEnded readonly | boolean | Whether end() has been called. | v0.0.0-alpha.2 |
writableFinished readonly | boolean | Whether "finish" has been emitted. | v0.0.0-alpha.2 |
closed readonly | boolean | Whether "close" has been emitted. | v0.0.0-alpha.2 |
errored readonly | Error | null | Error passed to destroy(err), or null. | v0.0.0-alpha.2 |
Socket.connect
Section titled “Socket.connect”connect(options: SocketConnectOpts, connectionListener?: () => void): this;
connect(port: number, host?: string, connectionListener?: () => void): this;
connect(port: number, connectionListener?: () => void): this;
connect(path: string, connectionListener?: () => void): this;
Initiate a connection using options and optional "connect" listener.
connect(options, connectionListener?)| Name | Type | Description |
|---|---|---|
options | SocketConnectOpts | — |
connectionListener optional | () => void | — |
thisconnect(port, host?, connectionListener?)Initiate a TCP connection to port and optional host.
| Name | Type | Description |
|---|---|---|
port | number | — |
host optional | string | — |
connectionListener optional | () => void | — |
thisconnect(port, connectionListener?)Initiate a TCP connection to port on localhost with connectionListener.
| Name | Type | Description |
|---|---|---|
port | number | — |
connectionListener optional | () => void | — |
thisconnect(path, connectionListener?)Initiate an IPC connection to path.
| Name | Type | Description |
|---|---|---|
path | string | — |
connectionListener optional | () => void | — |
thisSocket.address
Section titled “Socket.address”address(): AddressInfo | Record<string, never>;
Return bound local address metadata { address, family, port }.
AddressInfo | Record<string, never>Socket.write
Section titled “Socket.write”write(chunk: string | Uint8Array, callback?: (err?: Error | null) => void): boolean;
write(chunk: string, encoding: string, callback?: (err?: Error | null) => void): boolean;
Write data to the socket.
write(chunk, callback?)| Name | Type | Description |
|---|---|---|
chunk | string | Uint8Array | — |
callback optional | (err?: Error | null) => void | — |
booleanwrite(chunk, encoding, callback?)Write string data with explicit encoding to the socket.
| Name | Type | Description |
|---|---|---|
chunk | string | — |
encoding | string | — |
callback optional | (err?: Error | null) => void | — |
booleanSocket.end
Section titled “Socket.end”end(callback?: () => void): this;
end(chunk: string | Uint8Array, callback?: () => void): this;
end(chunk: string, encoding: string, callback?: () => void): this;
Half-close the writable side of the socket.
end(callback?)| Name | Type | Description |
|---|---|---|
callback optional | () => void | — |
thisend(chunk, callback?)Write a final chunk and half-close the writable side.
| Name | Type | Description |
|---|---|---|
chunk | string | Uint8Array | — |
callback optional | () => void | — |
thisend(chunk, encoding, callback?)Write a final string chunk with encoding and half-close the writable side.
| Name | Type | Description |
|---|---|---|
chunk | string | — |
encoding | string | — |
callback optional | () => void | — |
thisSocket.destroy
Section titled “Socket.destroy”destroy(error?: Error): this;
Close the socket immediately and emit "close".
| Name | Type | Description |
|---|---|---|
error optional | Error | — |
thisSocket.destroySoon
Section titled “Socket.destroySoon”destroySoon(): void;
Close the socket after pending writes drain.
voidSocket.pause
Section titled “Socket.pause”pause(): this;
Pause "data" event emission.
thisSocket.resume
Section titled “Socket.resume”resume(): this;
Resume "data" event emission.
thisSocket.read
Section titled “Socket.read”read(size?: number): any;
Read buffered bytes from the socket.
| Name | Type | Description |
|---|---|---|
size optional | number | — |
anySocket.setEncoding
Section titled “Socket.setEncoding”setEncoding(encoding?: string): this;
Set character encoding for emitted "data" chunks.
| Name | Type | Description |
|---|---|---|
encoding optional | string | — |
thisSocket.setTimeout
Section titled “Socket.setTimeout”setTimeout(timeout: number, callback?: () => void): this;
Set socket inactivity timeout in milliseconds (0 disables timeout).
| Name | Type | Description |
|---|---|---|
timeout | number | — |
callback optional | () => void | — |
thisSocket.setNoDelay
Section titled “Socket.setNoDelay”setNoDelay(noDelay?: boolean): this;
Enable or disable TCP_NODELAY.
| Name | Type | Description |
|---|---|---|
noDelay optional | boolean | — |
thisSocket.setKeepAlive
Section titled “Socket.setKeepAlive”setKeepAlive(enable?: boolean, initialDelay?: number): this;
Enable or disable TCP keep-alive probes.
| Name | Type | Description |
|---|---|---|
enable optional | boolean | — |
initialDelay optional | number | — |
thisSocket.ref
Section titled “Socket.ref”ref(): this;
Keep the event loop active while the socket is open.
thisSocket.unref
Section titled “Socket.unref”unref(): this;
Allow the event loop to exit if this socket is the only active handle.
thisSocket.resetAndDestroy
Section titled “Socket.resetAndDestroy”resetAndDestroy(): this;
Send TCP RST and destroy the socket immediately.
thisSocket.pipe
Section titled “Socket.pipe”pipe<T extends { write(chunk: any, ...args: any[]): any; end(...args: any[]): any }>(
destination: T,
options?: { end?: boolean },
): T;
Pipe received data into destination.
| Name | Type | Description |
|---|---|---|
destination | T | — |
options optional | { end?: boolean } | — |
TSocket.on
Section titled “Socket.on”on(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisSocket.once
Section titled “Socket.once”once(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register a one-time event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisSocket.off
Section titled “Socket.off”off(eventName: string | symbol, listener: (...args: any[]) => void): this;
Remove an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisSocket.addListener
Section titled “Socket.addListener”addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisSocket.removeListener
Section titled “Socket.removeListener”removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
Remove an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisSocket.removeAllListeners
Section titled “Socket.removeAllListeners”removeAllListeners(eventName?: string | symbol): this;
Remove all listeners for eventName or all events.
| Name | Type | Description |
|---|---|---|
eventName optional | string | symbol | — |
thisSocket.emit
Section titled “Socket.emit”emit(eventName: string | symbol, ...args: any[]): boolean;
Synchronously emit an event.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
...args | any[] | — |
booleanSocket.listeners
Section titled “Socket.listeners”listeners(eventName: string | symbol): Array<(...args: any[]) => void>;
Return registered listeners for eventName.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
Array<(...args: any[]) => void>Socket.listenerCount
Section titled “Socket.listenerCount”listenerCount(eventName: string | symbol, listener?: (...args: any[]) => void): number;
Return listener count for eventName.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener optional | (...args: any[]) => void | — |
numberSocket.eventNames
Section titled “Socket.eventNames”eventNames(): Array<string | symbol>;
Return all registered event names.
Array<string | symbol>Socket.[Symbol.asyncIterator]
Section titled “Socket.[Symbol.asyncIterator]”[Symbol.asyncIterator](): AsyncIterableIterator<any>;
Iterate asynchronously over received socket chunks.
Server
Section titled “Server”export class Server {
readonly listening: boolean;
maxConnections: number;
readonly connections: number;
constructor(connectionListener?: (socket: Socket) => void);
constructor(options?: ServerOpts, connectionListener?: (socket: Socket) => void);
listen(
port?: number,
hostname?: string,
backlog?: number,
listeningListener?: () => void,
): this;
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;
close(callback?: (err?: Error) => void): this;
address(): AddressInfo | string | null;
getConnections(cb: (error: Error | null, count: number) => void): this;
ref(): this;
unref(): this;
on(eventName: string | symbol, listener: (...args: any[]) => void): this;
once(eventName: string | symbol, listener: (...args: any[]) => void): this;
off(eventName: string | symbol, listener: (...args: any[]) => void): this;
addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
removeAllListeners(eventName?: string | symbol): this;
emit(eventName: string | symbol, ...args: any[]): boolean;
listeners(eventName: string | symbol): Array<(...args: any[]) => void>;
listenerCount(eventName: string | symbol, listener?: (...args: any[]) => void): number;
eventNames(): Array<string | symbol>;
[Symbol.asyncDispose](): Promise<void>;
}
TCP or IPC listening server accepting inbound Socket connections.
constructor(connectionListener?: (socket: Socket) => void);
constructor(options?: ServerOpts, connectionListener?: (socket: Socket) => void);
Construct a Server with an optional "connection" listener.
new Server(connectionListener?)| Name | Type | Description |
|---|---|---|
connectionListener optional | (socket: Socket) => void | Callback invoked for each accepted Socket. |
new Server(options?, connectionListener?)Construct a Server with explicit options and optional "connection" listener.
| Name | Type | Description |
|---|---|---|
options optional | ServerOpts | Server socket configuration. |
connectionListener optional | (socket: Socket) => void | Callback invoked for each accepted Socket. |
| Property | Type | Description | Since |
|---|---|---|---|
listening readonly | boolean | true while the server is actively listening for connections. | v0.0.0-alpha.2 |
maxConnections | number | Maximum concurrent connections accepted by this server. | v0.0.0-alpha.2 |
connections readonly | number | Number of currently open connections. | v0.0.0-alpha.2 |
Server.listen
Section titled “Server.listen”listen(
port?: number,
hostname?: string,
backlog?: number,
listeningListener?: () => void,
): this;
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;
Start listening for TCP connections on port.
listen(port?, hostname?, backlog?, listeningListener?)| Name | Type | Description |
|---|---|---|
port optional | number | — |
hostname optional | string | — |
backlog optional | number | — |
listeningListener optional | () => void | — |
thislisten(port?, hostname?, listeningListener?)Start listening on port with optional hostname and listener.
| Name | Type | Description |
|---|---|---|
port optional | number | — |
hostname optional | string | — |
listeningListener optional | () => void | — |
thislisten(port?, backlog?, listeningListener?)Start listening on port with optional backlog and listener.
| Name | Type | Description |
|---|---|---|
port optional | number | — |
backlog optional | number | — |
listeningListener optional | () => void | — |
thislisten(port?, listeningListener?)Start listening on port with optional listener.
| Name | Type | Description |
|---|---|---|
port optional | number | — |
listeningListener optional | () => void | — |
thislisten(path, backlog?, listeningListener?)Start listening on an IPC path.
| Name | Type | Description |
|---|---|---|
path | string | — |
backlog optional | number | — |
listeningListener optional | () => void | — |
thislisten(path, listeningListener?)Start listening on an IPC path with optional listener.
| Name | Type | Description |
|---|---|---|
path | string | — |
listeningListener optional | () => void | — |
thislisten(options, listeningListener?)Start listening using a ListenOptions object.
| Name | Type | Description |
|---|---|---|
options | ListenOptions | — |
listeningListener optional | () => void | — |
thisServer.close
Section titled “Server.close”close(callback?: (err?: Error) => void): this;
Stop accepting new connections and close once existing connections end.
| Name | Type | Description |
|---|---|---|
callback optional | (err?: Error) => void | — |
thisServer.address
Section titled “Server.address”address(): AddressInfo | string | null;
Return the bound socket address (AddressInfo, IPC string, or null).
AddressInfo | string | nullServer.getConnections
Section titled “Server.getConnections”getConnections(cb: (error: Error | null, count: number) => void): this;
Asynchronously retrieve the current number of active connections.
| Name | Type | Description |
|---|---|---|
cb | (error: Error | null, count: number) => void | — |
thisServer.ref
Section titled “Server.ref”ref(): this;
Keep the event loop active while the server is listening.
thisServer.unref
Section titled “Server.unref”unref(): this;
Allow the event loop to exit even if the server is listening.
thisServer.on
Section titled “Server.on”on(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisServer.once
Section titled “Server.once”once(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register a one-time event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisServer.off
Section titled “Server.off”off(eventName: string | symbol, listener: (...args: any[]) => void): this;
Remove an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisServer.addListener
Section titled “Server.addListener”addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisServer.removeListener
Section titled “Server.removeListener”removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
Remove an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisServer.removeAllListeners
Section titled “Server.removeAllListeners”removeAllListeners(eventName?: string | symbol): this;
Remove all listeners for eventName or all events.
| Name | Type | Description |
|---|---|---|
eventName optional | string | symbol | — |
thisServer.emit
Section titled “Server.emit”emit(eventName: string | symbol, ...args: any[]): boolean;
Synchronously emit an event.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
...args | any[] | — |
booleanServer.listeners
Section titled “Server.listeners”listeners(eventName: string | symbol): Array<(...args: any[]) => void>;
Return registered listeners for eventName.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
Array<(...args: any[]) => void>Server.listenerCount
Section titled “Server.listenerCount”listenerCount(eventName: string | symbol, listener?: (...args: any[]) => void): number;
Return listener count for eventName.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener optional | (...args: any[]) => void | — |
numberServer.eventNames
Section titled “Server.eventNames”eventNames(): Array<string | symbol>;
Return all registered event names.
Array<string | symbol>Server.[Symbol.asyncDispose]
Section titled “Server.[Symbol.asyncDispose]”[Symbol.asyncDispose](): Promise<void>;
Close the server asynchronously when disposed via await using.
Promise<void>Interfaces
Section titled “Interfaces”AbortSignalLike
Section titled “AbortSignalLike”export interface AbortSignalLike {
readonly aborted: boolean;
readonly reason?: unknown;
addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
removeEventListener?(type: string, listener: (...args: any[]) => void): void;
}
Minimal abort-signal contract accepted by net listen and socket options.
| Property | Type | Description | Since |
|---|---|---|---|
aborted readonly | boolean | Whether the signal has already been aborted. | v0.0.0-alpha.2 |
reason readonly optional | unknown | Optional abort reason value. | v0.0.0-alpha.2 |
AbortSignalLike.addEventListener
Section titled “AbortSignalLike.addEventListener”addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
Register an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | — |
listener | (...args: any[]) => void | — |
options optional | { once?: boolean } | — |
voidAbortSignalLike.removeEventListener
Section titled “AbortSignalLike.removeEventListener”removeEventListener?(type: string, listener: (...args: any[]) => void): void;
Unregister an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | — |
listener | (...args: any[]) => void | — |
voidAddressInfo
Section titled “AddressInfo”export interface AddressInfo {
address: string;
family: string;
port: number;
}
Bound socket or server address metadata returned by .address().
| Property | Type | Description | Since |
|---|---|---|---|
address | string | Bound IP address string. | v0.0.0-alpha.2 |
family | string | Address family ("IPv4" or "IPv6"). | v0.0.0-alpha.2 |
port | number | Bound TCP port number. | v0.0.0-alpha.2 |
SocketAddressInitOptions
Section titled “SocketAddressInitOptions”export interface SocketAddressInitOptions {
address?: string;
family?: IPVersion;
flowlabel?: number;
port?: number;
}
Options accepted by new SocketAddress(options).
| Property | Type | Description | Since |
|---|---|---|---|
address optional | string | IPv4 or IPv6 address string (default "127.0.0.1" or "::"). | v0.0.0-alpha.2 |
family optional | IPVersion | IP address family ("ipv4" or "ipv6"). | v0.0.0-alpha.2 |
flowlabel optional | number | IPv6 flow label (0..=1048575). | v0.0.0-alpha.2 |
port optional | number | Port number (0..=65535). | v0.0.0-alpha.2 |
SocketConstructorOpts
Section titled “SocketConstructorOpts”export interface SocketConstructorOpts {
fd?: number;
allowHalfOpen?: boolean;
readable?: boolean;
writable?: boolean;
signal?: AbortSignalLike;
}
Options accepted by new Socket(options).
| Property | Type | Description | Since |
|---|---|---|---|
fd optional | number | Existing file descriptor to wrap. | v0.0.0-alpha.2 |
allowHalfOpen optional | boolean | Keep writable side open after remote FIN (default false). | v0.0.0-alpha.2 |
readable optional | boolean | Allow reads on wrapped fd. | v0.0.0-alpha.2 |
writable optional | boolean | Allow writes on wrapped fd. | v0.0.0-alpha.2 |
signal optional | AbortSignalLike | Optional abort signal destroying the socket on abort. | v0.0.0-alpha.2 |
TcpSocketConnectOpts
Section titled “TcpSocketConnectOpts”export interface TcpSocketConnectOpts {
port: number;
host?: string;
localAddress?: string;
localPort?: number;
hints?: number;
family?: number;
lookup?: LookupFunction;
noDelay?: boolean;
keepAlive?: boolean;
keepAliveInitialDelay?: number;
autoSelectFamily?: boolean;
autoSelectFamilyAttemptTimeout?: number;
signal?: AbortSignalLike;
}
TCP connection options accepted by connect(), createConnection(), and socket.connect().
| Property | Type | Description | Since |
|---|---|---|---|
port | number | Remote TCP port (0..=65535). | v0.0.0-alpha.2 |
host optional | string | Remote hostname or IP address (default "localhost"). | v0.0.0-alpha.2 |
localAddress optional | string | Local interface IP address to bind before connecting. | v0.0.0-alpha.2 |
localPort optional | number | Local port to bind before connecting. | v0.0.0-alpha.2 |
hints optional | number | DNS lookup hints bitmask. | v0.0.0-alpha.2 |
family optional | number | IP family constraint (0, 4, or 6). | v0.0.0-alpha.2 |
lookup optional | LookupFunction | Custom DNS lookup callback. | v0.0.0-alpha.2 |
noDelay optional | boolean | Disable Nagle's algorithm (TCP_NODELAY). | v0.0.0-alpha.2 |
keepAlive optional | boolean | Enable TCP keep-alive probes. | v0.0.0-alpha.2 |
keepAliveInitialDelay optional | number | Initial delay in milliseconds before TCP keep-alive probes start. | v0.0.0-alpha.2 |
autoSelectFamily optional | boolean | Enable Happy Eyeballs IPv4/IPv6 auto-selection. | v0.0.0-alpha.2 |
autoSelectFamilyAttemptTimeout optional | number | Attempt timeout in milliseconds for Happy Eyeballs family selection. | v0.0.0-alpha.2 |
signal optional | AbortSignalLike | Optional abort signal cancelling connection establishment. | v0.0.0-alpha.2 |
IpcSocketConnectOpts
Section titled “IpcSocketConnectOpts”export interface IpcSocketConnectOpts {
path: string;
signal?: AbortSignalLike;
}
Unix domain socket or Windows named-pipe connection options.
| Property | Type | Description | Since |
|---|---|---|---|
path | string | Filesystem socket path or Windows named pipe path. | v0.0.0-alpha.2 |
signal optional | AbortSignalLike | Optional abort signal cancelling connection establishment. | v0.0.0-alpha.2 |
ListenOptions
Section titled “ListenOptions”export interface ListenOptions {
port?: number;
host?: string;
backlog?: number;
path?: string;
exclusive?: boolean;
readableAll?: boolean;
writableAll?: boolean;
ipv6Only?: boolean;
signal?: AbortSignalLike;
}
Options accepted by server.listen(options).
| Property | Type | Description | Since |
|---|---|---|---|
port optional | number | TCP port to bind (0 selects an ephemeral OS port). | v0.0.0-alpha.2 |
host optional | string | Local hostname or IP address to bind. | v0.0.0-alpha.2 |
backlog optional | number | Maximum pending connection queue length. | v0.0.0-alpha.2 |
path optional | string | Unix socket or named pipe path (alternative to port). | v0.0.0-alpha.2 |
exclusive optional | boolean | Disable cluster port sharing when true. | v0.0.0-alpha.2 |
readableAll optional | boolean | Make IPC path readable by all users. | v0.0.0-alpha.2 |
writableAll optional | boolean | Make IPC path writable by all users. | v0.0.0-alpha.2 |
ipv6Only optional | boolean | Restrict dual-stack IPv6 sockets to IPv6 only. | v0.0.0-alpha.2 |
signal optional | AbortSignalLike | Optional abort signal closing the server when aborted. | v0.0.0-alpha.2 |
ServerOpts
Section titled “ServerOpts”export interface ServerOpts {
allowHalfOpen?: boolean;
pauseOnConnect?: boolean;
noDelay?: boolean;
keepAlive?: boolean;
keepAliveInitialDelay?: number;
highWaterMark?: number;
}
Options accepted by createServer() and new Server().
| Property | Type | Description | Since |
|---|---|---|---|
allowHalfOpen optional | boolean | Keep accepted sockets half-open after remote FIN (default false). | v0.0.0-alpha.2 |
pauseOnConnect optional | boolean | Pause accepted sockets on connection until resume() is called. | v0.0.0-alpha.2 |
noDelay optional | boolean | Disable Nagle's algorithm on accepted sockets. | v0.0.0-alpha.2 |
keepAlive optional | boolean | Enable TCP keep-alive probes on accepted sockets. | v0.0.0-alpha.2 |
keepAliveInitialDelay optional | number | Initial TCP keep-alive probe delay in milliseconds. | v0.0.0-alpha.2 |
highWaterMark optional | number | Default stream high-water mark for accepted sockets. | v0.0.0-alpha.2 |
Type Aliases
Section titled “Type Aliases”IPVersion
Section titled “IPVersion”export type IPVersion = "ipv4" | "ipv6";
IP address family string.
LookupFunction
Section titled “LookupFunction”export type LookupFunction = (
hostname: string,
options: { family?: number; hints?: number; all?: boolean },
callback: (err: Error | null, address: string | Array<{ address: string; family: number }>, family?: number) => void,
) => void;
Custom DNS lookup callback signature accepted by connect().
SocketConnectOpts
Section titled “SocketConnectOpts”export type SocketConnectOpts = TcpSocketConnectOpts | IpcSocketConnectOpts;
Union of TCP and IPC connection options accepted by connect() and createConnection().
NetConnectOpts
Section titled “NetConnectOpts”export type NetConnectOpts = SocketConnectOpts;
Alias for SocketConnectOpts.
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** `BlockList` constructor. @since 0.0.0-alpha.2 */
BlockList: typeof BlockList;
/** `Server` constructor. @since 0.0.0-alpha.2 */
Server: typeof Server;
/** `Socket` constructor. @since 0.0.0-alpha.2 */
Socket: typeof Socket;
/** `SocketAddress` constructor. @since 0.0.0-alpha.2 */
SocketAddress: typeof SocketAddress;
/** Open a socket connection. @since 0.0.0-alpha.2 */
connect: typeof connect;
/** Open a socket connection. @since 0.0.0-alpha.2 */
createConnection: typeof createConnection;
/** Create a TCP/IPC server. @since 0.0.0-alpha.2 */
createServer: typeof createServer;
/** Get default `autoSelectFamily` setting. @since 0.0.0-alpha.2 */
getDefaultAutoSelectFamily: typeof getDefaultAutoSelectFamily;
/** Get default `autoSelectFamilyAttemptTimeout` setting. @since 0.0.0-alpha.2 */
getDefaultAutoSelectFamilyAttemptTimeout: typeof getDefaultAutoSelectFamilyAttemptTimeout;
/** Classify an IP string (`0`, `4`, or `6`). @since 0.0.0-alpha.2 */
isIP: typeof isIP;
/** Return `true` if `input` is IPv4. @since 0.0.0-alpha.2 */
isIPv4: typeof isIPv4;
/** Return `true` if `input` is IPv6. @since 0.0.0-alpha.2 */
isIPv6: typeof isIPv6;
/** Set default `autoSelectFamily` setting. @since 0.0.0-alpha.2 */
setDefaultAutoSelectFamily: typeof setDefaultAutoSelectFamily;
/** Set default `autoSelectFamilyAttemptTimeout` setting. @since 0.0.0-alpha.2 */
setDefaultAutoSelectFamilyAttemptTimeout: typeof setDefaultAutoSelectFamilyAttemptTimeout;
};
export default _default;
Default export object providing the node:net API.
