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-ws.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/k6-ws/. 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.
k6/ws
Note
A module with a better and standard API exists.
The new k6/websockets API partially implements the WebSockets API living standard.
When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance.
The
k6/ws module provides a WebSocket client implementing the WebSocket protocol.
| Function | Description |
|---|---|
| connect( url, params, callback ) | Create a WebSocket connection, and provides a Socket client to interact with the service. The method blocks the test finalization until the connection is closed. |
| Class/Method | Description |
|---|---|
| Params | Used for setting various WebSocket connection parameters such as headers, cookie jar, compression, etc. |
| Socket | WebSocket client used to interact with a WS connection. |
| Socket.close() | Close the WebSocket connection. |
| Socket.on(event, callback) | Set up an event listener on the connection for any of the following events: - open - binaryMessage - message - ping - pong - close - error. |
| Socket.ping() | Send a ping. |
| Socket.send(data) | Send string data. |
| Socket.sendBinary(data) | Send binary data. |
| Socket.setInterval(callback, interval) | Call a function repeatedly at certain intervals, while the connection is open. |
| Socket.setTimeout(callback, period) | Call a function with a delay, if the connection is open. |
Was this page helpful?
Related resources from Grafana Labs

