Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/next/javascript-api/k6-x-tcp/socket/connect.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/javascript-api/k6-x-tcp/socket/connect/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
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

