Menu
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
Open source
Socket.connect()
Asynchronously establishes a TCP connection to a remote server. Returns a Promise that resolves when the connection is established, or rejects on failure.
Signature
JavaScript
// Overload 1: port and optional host
socket.connect(port, host)
// Overload 2: options object
socket.connect(options)Parameters
Overload 1
| Parameter | Type | Description |
|---|---|---|
| port | number | string | Destination port (1-65535) |
| host | string | Destination hostname or IP address. Defaults to 'localhost' |
Overload 2
| Parameter | Type | Description |
|---|---|---|
| options.port | number | string | Destination port (required) |
| options.host | string | Destination hostname or IP address. Defaults to 'localhost' |
| options.tls | boolean | Enable TLS/SSL encryption. Defaults to false |
| options.tags | object | Custom tags for connection metrics (key-value pairs) |
Returns
| Type | Description |
|---|---|
| Promise<void> | Resolves when the connection is established. Rejects on connection failure |
TLS/SSL configuration
When tls: true is set, the socket performs a TLS handshake after establishing the TCP connection. TLS settings such as certificates, verification, and cipher suites are controlled by k6’s standard
TLS configuration.
Example
JavaScript
import { Socket } from "k6/x/tcp"
export default async function () {
// Overload 1: connect with separate port and host
const socket = new Socket()
const closed = new Promise((resolve) => { socket.on("close", resolve) })
socket.on("error", (err) => { console.error("Error:", err) })
await socket.connect(8080, "example.com")
console.log("Connected")
socket.destroy()
await closed
// Overload 2: connect with options object and TLS
const tlsSocket = new Socket({ tags: { protocol: "tls" } })
const tlsClosed = new Promise((resolve) => { tlsSocket.on("close", resolve) })
tlsSocket.on("error", (err) => { console.error("TLS error:", err) })
await tlsSocket.connect({
port: 443,
host: "secure.example.com",
tls: true,
})
console.log("TLS connection established")
tlsSocket.destroy()
await tlsClosed
}Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.
Choose a product
Viewing: next
Find another version
Scroll for more