---
title: "Socket.connect( port, [host] ) | Grafana k6 documentation"
description: "Establish a TCP connection"
---

# 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 ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
// Overload 1: port and optional host
socket.connect(port, host)

// Overload 2: options object
socket.connect(options)
```

## Parameters

**Overload 1**

Expand table

| Parameter | Type             | Description                                                   |
|-----------|------------------|---------------------------------------------------------------|
| port      | number \| string | Destination port (1-65535)                                    |
| host      | string           | Destination hostname or IP address. Defaults to `'localhost'` |

**Overload 2**

Expand table

| 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

Expand table

| Type                | Description                                                                |
|---------------------|----------------------------------------------------------------------------|
| Promise&lt;void&gt; | 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](/docs/k6/latest/using-k6/protocols/ssl-tls/).

## Example

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```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
}
```
