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/destroy.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/javascript-api/k6-x-tcp/socket/destroy/. 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.destroy()
Closes the connection and destroys the socket. After you call destroy(), the socket transitions to the 'destroyed' state, and you can’t reuse it. A close event is emitted after the socket is destroyed.
Signature
socket.destroy()Parameters
None.
Returns
void
Example
import { Socket } from "k6/x/tcp"
export default async function () {
const socket = new Socket()
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)
socket.destroy()
})
await socket.connect(8080, "example.com")
await socket.write("Hello, server!")
await closed
}Was this page helpful?
Related resources from Grafana Labs

