Menu
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
Open source
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
JavaScript
socket.destroy()Parameters
None.
Returns
void
Example
JavaScript
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
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.
Choose a product
Viewing: next
Find another version
Scroll for more