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 });Extends
Section titled “Extends”InetSocket<TSocket,TEvents>
Type Parameters
Section titled “Type Parameters”TSocket
Section titled “TSocket”TSocket extends net.Socket = net.Socket
TEvents
Section titled “TEvents”TEvents extends TcpSocketEvents = TcpSocketEvents
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new TcpSocket<
TSocket,TEvents>(socket):TcpSocket<TSocket,TEvents>
Defined in: packages/node/src/net/stream-socket.ts:121
Creates a new StreamSocket instance.
Parameters
Section titled “Parameters”socket
Section titled “socket”TSocket
The underlying Node.js socket.
Returns
Section titled “Returns”TcpSocket<TSocket, TEvents>
Inherited from
Section titled “Inherited from”Accessors
Section titled “Accessors”bytesRead
Section titled “bytesRead”Get Signature
Section titled “Get Signature”get bytesRead():
number
Defined in: packages/node/src/net/stream-socket.ts:147
Total number of bytes read from the socket so far.
Returns
Section titled “Returns”number
Inherited from
Section titled “Inherited from”bytesWritten
Section titled “bytesWritten”Get Signature
Section titled “Get Signature”get bytesWritten():
number
Defined in: packages/node/src/net/stream-socket.ts:152
Total number of bytes written to the socket so far.
Returns
Section titled “Returns”number
Inherited from
Section titled “Inherited from”closed
Section titled “closed”Get Signature
Section titled “Get Signature”get closed():
boolean
Defined in: packages/node/src/net/stream-socket.ts:137
Indicates whether the socket has been fully closed.
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”connecting
Section titled “connecting”Get Signature
Section titled “Get Signature”get connecting():
boolean
Defined in: packages/node/src/net/stream-socket.ts:157
Whether the socket is currently in the process of connecting.
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”destroyed
Section titled “destroyed”Get Signature
Section titled “Get Signature”get destroyed():
boolean
Defined in: packages/node/src/net/stream-socket.ts:142
Indicates whether the underlying socket has been destroyed.
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”localEndpoint
Section titled “localEndpoint”Get Signature
Section titled “Get Signature”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.
Throws
Section titled “Throws”If the socket family is not IPv4 or IPv6.
Returns
Section titled “Returns”InetEndpoint | null
Inherited from
Section titled “Inherited from”remoteEndpoint
Section titled “remoteEndpoint”Get Signature
Section titled “Get Signature”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.
Throws
Section titled “Throws”If the socket family is not IPv4 or IPv6.
Returns
Section titled “Returns”InetEndpoint | null
Inherited from
Section titled “Inherited from”stream
Section titled “stream”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”Duplex
Inherited from
Section titled “Inherited from”timeout
Section titled “timeout”Get Signature
Section titled “Get Signature”get timeout():
number|null
Defined in: packages/node/src/net/stream-socket.ts:162
Current inactivity timeout in milliseconds, or null if disabled.
Returns
Section titled “Returns”number | null
Set Signature
Section titled “Set Signature”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.
Parameters
Section titled “Parameters”timeout
Section titled “timeout”number | null
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”Methods
Section titled “Methods”connect()
Section titled “connect()”connect(
port,options?):Promise<void>
Defined in: packages/node/src/net/tcp-socket.ts:60
Establishes a TCP connection to the specified port.
Parameters
Section titled “Parameters”number
The port to connect to.
options?
Section titled “options?”Connection options, including the host (defaults to
localhost) and an optional abort signal.
Returns
Section titled “Returns”Promise<void>
A promise that resolves when the connection is established.
Throws
Section titled “Throws”When the connection fails, when the socket closes before connecting, or when the signal is aborted.
destroy()
Section titled “destroy()”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.
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”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.
Parameters
Section titled “Parameters”options?
Section titled “options?”Optional settings for ending the socket.
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the socket has ended.
Inherited from
Section titled “Inherited from”isSubscribed()
Section titled “isSubscribed()”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.
Type Parameters
Section titled “Type Parameters”K extends string | number | symbol
Parameters
Section titled “Parameters”K
The event name.
listener
Section titled “listener”Callable<TEvents[K]>
The callback to check.
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”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.
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”setKeepAlive()
Section titled “setKeepAlive()”setKeepAlive(
enable,initialDelay?):void
Defined in: packages/node/src/net/inet-socket.ts:93
Sets the keep-alive option for the socket.
Parameters
Section titled “Parameters”enable
Section titled “enable”boolean
Whether to enable keep-alive.
initialDelay?
Section titled “initialDelay?”number
The initial delay in milliseconds before the first keep-alive probe.
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”setNoDelay()
Section titled “setNoDelay()”setNoDelay(
noDelay):void
Defined in: packages/node/src/net/inet-socket.ts:102
Enables or disables the Nagle algorithm for the socket.
Parameters
Section titled “Parameters”noDelay
Section titled “noDelay”boolean
Whether to disable the Nagle algorithm.
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”subscribe()
Section titled “subscribe()”subscribe<
K>(event,listener,options?):Callable|null
Defined in: packages/async/src/events/event-dispatcher-map-base.ts:23
Type Parameters
Section titled “Type Parameters”K extends string | number | symbol
Parameters
Section titled “Parameters”K
listener
Section titled “listener”Callable<TEvents[K]>
options?
Section titled “options?”EventDispatcherSubscribeOptions
Returns
Section titled “Returns”Callable | null
Inherited from
Section titled “Inherited from”unref()
Section titled “unref()”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.
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”unsubscribe()
Section titled “unsubscribe()”unsubscribe<
K>(event,listener):void
Defined in: packages/async/src/events/event-dispatcher-map-base.ts:31
Unsubscribe a callback from a specific event.
Type Parameters
Section titled “Type Parameters”K extends string | number | symbol
Parameters
Section titled “Parameters”K
The event name.
listener
Section titled “listener”Callable<TEvents[K]>
The callback to remove.
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”wait()
Section titled “wait()”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.
Type Parameters
Section titled “Type Parameters”K extends string | number | symbol
Parameters
Section titled “Parameters”K
The event name to wait for.
options?
Section titled “options?”EventDispatcherWaitOptions<TEvents[K]>
Optional settings including predicate and abort signal.
Returns
Section titled “Returns”Promise<TEvents[K]>
A promise that resolves with the event arguments as an array.
Inherited from
Section titled “Inherited from”write()
Section titled “write()”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.
Parameters
Section titled “Parameters”Uint8Array
The data to write.
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”When the socket errors or is destroyed before the chunk flushes.
Inherited from
Section titled “Inherited from”from()
Section titled “from()”
staticfrom(options?):TcpSocket
Defined in: packages/node/src/net/tcp-socket.ts:46
Creates a new TcpSocket instance.
Parameters
Section titled “Parameters”options?
Section titled “options?”Options for creating the underlying Node.js socket.
allowHalfOpen?
Section titled “allowHalfOpen?”boolean
blockList?
Section titled “blockList?”BlockList
handle?
Section titled “handle?”BoundSocket
keepAlive?
Section titled “keepAlive?”boolean
keepAliveInitialDelay?
Section titled “keepAliveInitialDelay?”number
noDelay?
Section titled “noDelay?”boolean
onread?
Section titled “onread?”OnReadOpts
readable?
Section titled “readable?”boolean
signal?
Section titled “signal?”AbortSignal
typeOfService?
Section titled “typeOfService?”number
writable?
Section titled “writable?”boolean
Returns
Section titled “Returns”TcpSocket
A new TcpSocket instance.