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.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/k6-x-tcp/socket/. 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
The Socket class provides an event-driven interface for establishing TCP connections, sending and receiving data, and managing the connection lifecycle.
Constructor
new Socket(options)Creates a new TCP socket in the disconnected state. Use connect() to establish a connection.
Parameters
| Parameter | Type | Description |
|---|---|---|
| options | object | Optional socket configuration |
| options.tags | object | Custom tags for metrics (key-value pairs). Applied to all metrics generated by this socket |
Properties
| Property | Type | Description |
|---|---|---|
ready_state | SocketState | Current connection state: 'disconnected', 'opening', 'open', or 'destroyed' |
connected | boolean | Read-only. true when ready_state is 'open' |
local_ip | string | undefined |
local_port | number | undefined |
remote_ip | string | undefined |
remote_port | number | undefined |
bytes_written | number | Total bytes sent through this socket |
bytes_read | number | Total bytes received through this socket |
Events
Register handlers for these events using socket.on(). The socket supports only one listener per event. Registering a second listener replaces the first.
| Event | Listener signature | Description |
|---|---|---|
connect | () => void | Emitted when the connection is established |
data | (data: Uint8Array) => void | Emitted when data is received from the server |
close | () => void | Emitted when the connection is fully closed |
error | (error: Error) => void | Emitted on socket error |
timeout | () => void | Emitted when the inactivity timeout fires (see setTimeout()) |
Methods
| Method | Description |
|---|---|
| connect() | Establish a TCP connection |
| write() | Send data over the socket |
| destroy() | Close and destroy the socket |
| setTimeout() | Set inactivity timeout |
| on() | Register an event handler |
Example
import { Socket } from "k6/x/tcp"
export default async function () {
const socket = new Socket({ tags: { service: "echo" } })
const closed = new Promise((resolve) => {
socket.on("close", () => {
console.log("Connection closed")
resolve()
})
})
socket.on("data", (data) => {
const str = String.fromCharCode.apply(null, new Uint8Array(data))
console.log("Received:", str)
socket.destroy()
})
socket.on("error", (err) => {
console.error("Error:", err)
})
const host = __ENV.TCP_HOST || "localhost"
const port = __ENV.TCP_PORT || "8080"
await socket.connect(port, host)
console.log("Connected. State:", socket.ready_state)
await socket.write("Hello, server!")
await closed
}Was this page helpful?
Related resources from Grafana Labs

