Creates a new TCP socket instance.
The socket is created in a disconnected state. Use connect() to establish a connection.
Optionaloptions: SocketOptionsOptional configuration for the socket
Total number of bytes received through this socket.
Increments with each 'data' event. Useful for tracking received data volumes and bandwidth usage.
Total number of bytes successfully sent through this socket.
Increments with each write() operation. Useful for tracking data transfer volumes in metrics and debugging.
ReadonlyconnectedIndicates whether the socket is currently connected to a remote endpoint.
This is a convenience property that returns true when ready_state is 'open',
and false otherwise. It provides a simple boolean check for connection status.
Optionallocal_The local IP address used for this connection.
Available after the socket is connected. Useful for debugging network routing or when testing from hosts with multiple interfaces.
Optionallocal_The local port number used for this connection.
Available after the socket is connected. The OS typically assigns an ephemeral port from the available port range.
The current state of the socket connection.
Provides detailed information about the socket's lifecycle state, similar to Node.js net.Socket readyState.
Read this property to check the connection status before performing operations or for debugging connection issues.
Optionalremote_The remote IP address of the connected server.
Available after DNS resolution completes during connection. This shows the actual IP address even if you connected using a hostname.
Optionalremote_The remote port number of the connected server.
This is the port you specified in the connect() call.
Initiates a TCP connection to the specified host and port.
This method is non-blocking and returns immediately. Listen for the 'connect' event to know when the connection is established, or 'error' for failures.
The destination port number (1-65535)
Optionalhost: stringThe destination hostname or IP address (defaults to 'localhost')
The socket instance for method chaining
Initiates a TCP connection using detailed connection options.
This method is non-blocking and returns immediately. Listen for the 'connect' event to know when the connection is established, or 'error' for failures.
Comprehensive connection configuration
The socket instance for method chaining
Asynchronously establishes a TCP connection to the specified host and port.
Returns a Promise that resolves when the connection is established, or rejects on connection failure. This is useful for sequential operations where you need to ensure the connection is ready before proceeding.
The destination port number (1-65535)
Optionalhost: stringThe destination hostname or IP address. Defaults to 'localhost'
A Promise that resolves when connected successfully
Asynchronously establishes a TCP connection using detailed options.
Returns a Promise that resolves when the connection is established, or rejects on connection failure. Useful for async/await workflows.
Connection options including port, host, TLS, and tags
A Promise that resolves when connected successfully
Destroys the socket and closes the connection.
Once destroyed, the socket cannot be reused. All pending operations will be terminated and a 'close' event will be emitted.
If an error is provided, an 'error' event will be emitted before closing.
Optionalerror: ErrorOptional error to emit before closing the socket
The socket itself for method chaining
Registers an event listener for the 'connect' event.
The 'connect' event is emitted when the socket successfully establishes a connection to the remote server. This indicates the socket is ready for reading and writing data.
Important: Only one listener is supported per event type. Calling this method multiple times will replace the previously set listener.
The event name 'connect'
Callback function invoked when the connection is established
The socket instance for method chaining
Registers an event listener for the 'data' event.
The 'data' event is emitted when data is received from the remote endpoint. The data is provided as an ArrayBuffer for efficient binary data handling.
Important: Only one listener is supported per event type. Calling this method multiple times will replace the previously set listener.
The event name 'data'
Callback function invoked when data is received
The socket instance for method chaining
Registers an event listener for the 'close' event.
The 'close' event is emitted when the socket connection is fully closed by either the local or remote endpoint. This is the final event in the socket lifecycle.
This event is guaranteed to be emitted exactly once per socket, making it ideal for cleanup operations and resolving promises in async patterns.
Important: Only one listener is supported per event type. Calling this method multiple times will replace the previously set listener.
The event name 'close'
Callback function invoked when the socket closes
The socket instance for method chaining
Registers an event listener for the 'error' event.
The 'error' event is emitted when a socket error occurs, such as connection failures, network issues, or protocol errors. It's crucial to handle this event to prevent unhandled errors.
Important: Only one listener is supported per event type. Calling this method multiple times will replace the previously set listener.
The event name 'error'
Callback function invoked when an error occurs, receives the Error object
The socket instance for method chaining
Registers an event listener for the 'timeout' event.
The 'timeout' event is emitted when the socket times out due to inactivity
as configured by setTimeout(). Note that the socket is not automatically
destroyed after a timeout - you must manually call destroy() if needed.
Important: Only one listener is supported per event type. Calling this method multiple times will replace the previously set listener.
The event name 'timeout'
Callback function invoked when the socket times out
The socket instance for method chaining
Sets the socket timeout for inactivity.
Sets the socket to timeout after timeout milliseconds of inactivity on the socket.
By default, TCP sockets 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 destroy() to end the connection.
If timeout is 0, then the existing idle timeout is disabled.
Timeout duration in milliseconds. Use 0 to disable timeout
The socket instance for method chaining
Sends data on the socket with additional options.
The data to write - can be a string or ArrayBuffer for binary data
Optionaloptions: WriteOptionsOptional configuration options for the write operation
true if the data was flushed successfully
// Write with encoding
socket.write('SGVsbG8gV29ybGQ=', { encoding: 'base64' });
// Write with tags for metrics
socket.write('Hello, server!', {
tags: { operation: 'handshake', protocol: 'custom' }
});
// Write with both encoding and tags
socket.write('encoded-payload', {
encoding: 'hex',
tags: { data_type: 'binary', size: 'small' }
});
Asynchronously sends data on the socket with additional options. Returns a Promise that resolves when the data has been written to the socket.
The data to write - can be a string or ArrayBuffer for binary data
Optionaloptions: WriteOptionsOptional configuration options for the write operation
A Promise that resolves when the data has been successfully written
// Write with encoding
await socket.writeAsync('SGVsbG8gV29ybGQ=', { encoding: 'base64' });
// Write with tags for metrics
await socket.writeAsync('Hello, server!', {
tags: { operation: 'handshake', protocol: 'custom' }
});
// Write with both encoding and tags
await socket.writeAsync('encoded-payload', {
encoding: 'hex',
tags: { data_type: 'binary', size: 'small' }
});
TCP Socket implementation for k6 performance testing.
Provides a Node.js-compatible API for TCP socket operations, adapted for the k6 environment. Supports both synchronous and asynchronous operations with comprehensive event handling.
Example