Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/latest/javascript-api/k6-x-tcp/socket/connect.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/k6-x-tcp/socket/connect/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
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
// 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
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

