Skip to content

TcpSocket

Defined in: packages/node/src/net/tcp-socket.ts:36

TCP socket for establishing connections to TCP servers.

Example usage:

const socket = TcpSocket.from();
await socket.connect(80, { host: "example.com" });
await socket.write(
Buffer.from("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"),
);
await socket.end({ waitForClose: true });

TSocket extends net.Socket = net.Socket

TEvents extends TcpSocketEvents = TcpSocketEvents

new TcpSocket<TSocket, TEvents>(socket): TcpSocket<TSocket, TEvents>

Defined in: packages/node/src/net/stream-socket.ts:121

Creates a new StreamSocket instance.

TSocket

The underlying Node.js socket.

TcpSocket<TSocket, TEvents>

InetSocket.constructor

get bytesRead(): number

Defined in: packages/node/src/net/stream-socket.ts:147

Total number of bytes read from the socket so far.

number

InetSocket.bytesRead


get bytesWritten(): number

Defined in: packages/node/src/net/stream-socket.ts:152

Total number of bytes written to the socket so far.

number

InetSocket.bytesWritten


get closed(): boolean

Defined in: packages/node/src/net/stream-socket.ts:137

Indicates whether the socket has been fully closed.

boolean

InetSocket.closed


get connecting(): boolean

Defined in: packages/node/src/net/stream-socket.ts:157

Whether the socket is currently in the process of connecting.

boolean

InetSocket.connecting


get destroyed(): boolean

Defined in: packages/node/src/net/stream-socket.ts:142

Indicates whether the underlying socket has been destroyed.

boolean

InetSocket.destroyed


get localEndpoint(): InetEndpoint | null

Defined in: packages/node/src/net/inet-socket.ts:71

Local endpoint of the socket, or null while it is not connected.

If the socket family is not IPv4 or IPv6.

InetEndpoint | null

InetSocket.localEndpoint


get remoteEndpoint(): InetEndpoint | null

Defined in: packages/node/src/net/inet-socket.ts:51

Remote endpoint of the socket, or null while it is not connected.

If the socket family is not IPv4 or IPv6.

InetEndpoint | null

InetSocket.remoteEndpoint


get stream(): Duplex

Defined in: packages/node/src/net/stream-socket.ts:132

Underlying duplex stream for reading and writing data.

This is the wrapped net.Socket instance and can be passed directly to APIs that expect a Node.js stream.

Duplex

InetSocket.stream


get timeout(): number | null

Defined in: packages/node/src/net/stream-socket.ts:162

Current inactivity timeout in milliseconds, or null if disabled.

number | null

set timeout(timeout): void

Defined in: packages/node/src/net/stream-socket.ts:173

Updates the inactivity timeout for the socket.

When set to a positive number, the socket emits a “timeout” event if no I/O activity occurs within the given number of milliseconds. A value of 0 or null disables the timeout entirely.

number | null

void

InetSocket.timeout

connect(port, options?): Promise<void>

Defined in: packages/node/src/net/tcp-socket.ts:60

Establishes a TCP connection to the specified port.

number

The port to connect to.

TcpSocketConnectOptions

Connection options, including the host (defaults to localhost) and an optional abort signal.

Promise<void>

A promise that resolves when the connection is established.

When the connection fails, when the socket closes before connecting, or when the signal is aborted.


destroy(): void

Defined in: packages/node/src/net/stream-socket.ts:262

Immediately destroys the underlying socket.

Pending I/O is discarded, the connection is closed and the socket transitions to a terminal state. Any registered “close” listeners will still be invoked.

void

InetSocket.destroy


end(options?): Promise<void>

Defined in: packages/node/src/net/stream-socket.ts:204

Half-closes the socket, optionally waiting for a full close.

If waitForClose is omitted or false, the promise resolves once the local side has finished sending data and the FIN has been queued. When waitForClose is true, the promise resolves only after the remote side has also closed and the “close” event has fired.

StreamSocketEndOptions

Optional settings for ending the socket.

Promise<void>

A promise that resolves once the socket has ended.

InetSocket.end


isSubscribed<K>(event, listener): boolean

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:38

Check if a callback is subscribed to a specific event.

K extends string | number | symbol

K

The event name.

Callable<TEvents[K]>

The callback to check.

boolean

InetSocket.isSubscribed


ref(): void

Defined in: packages/node/src/net/stream-socket.ts:181

Marks the socket as referenced, preventing the Node.js process from exiting while the socket is active.

void

InetSocket.ref


setKeepAlive(enable, initialDelay?): void

Defined in: packages/node/src/net/inet-socket.ts:93

Sets the keep-alive option for the socket.

boolean

Whether to enable keep-alive.

number

The initial delay in milliseconds before the first keep-alive probe.

void

InetSocket.setKeepAlive


setNoDelay(noDelay): void

Defined in: packages/node/src/net/inet-socket.ts:102

Enables or disables the Nagle algorithm for the socket.

boolean

Whether to disable the Nagle algorithm.

void

InetSocket.setNoDelay


subscribe<K>(event, listener, options?): Callable | null

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:23

K extends string | number | symbol

K

Callable<TEvents[K]>

EventDispatcherSubscribeOptions

Callable | null

InetSocket.subscribe


unref(): void

Defined in: packages/node/src/net/stream-socket.ts:189

Marks the socket as unreferenced, allowing the Node.js process to exit even if the socket is still active.

void

InetSocket.unref


unsubscribe<K>(event, listener): void

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:31

Unsubscribe a callback from a specific event.

K extends string | number | symbol

K

The event name.

Callable<TEvents[K]>

The callback to remove.

void

InetSocket.unsubscribe


wait<K>(event, options?): Promise<TEvents[K]>

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:45

Wait for the next occurrence of a specific event, optionally matching a predicate.

This is a convenience method that subscribes to the dispatcher, waits for the next event that matches the predicate (if provided), and then unsubscribes automatically.

K extends string | number | symbol

K

The event name to wait for.

EventDispatcherWaitOptions<TEvents[K]>

Optional settings including predicate and abort signal.

Promise<TEvents[K]>

A promise that resolves with the event arguments as an array.

InetSocket.wait


write(data): Promise<void>

Defined in: packages/node/src/net/stream-socket.ts:241

Writes data to the socket and resolves once that chunk has been flushed.

Awaiting the result is what applies backpressure: the promise settles on the chunk’s own flush rather than on the whole buffer draining, so a producer that awaits each write never outruns the socket. A caller that does not await is free to queue without bound.

Uint8Array

The data to write.

Promise<void>

When the socket errors or is destroyed before the chunk flushes.

InetSocket.write


static from(options?): TcpSocket

Defined in: packages/node/src/net/tcp-socket.ts:46

Creates a new TcpSocket instance.

Options for creating the underlying Node.js socket.

boolean

BlockList

BoundSocket

boolean

number

boolean

OnReadOpts

boolean

AbortSignal

number

boolean

TcpSocket

A new TcpSocket instance.

InetSocket.from