k6 WebSocket exposes ready-state constants
Introduced 2026-09-21 · score 0.91
HOW TO USE IT
When reviewing WebSocket message handling, check the socket state before sending. Send only when `socket.readyState === WebSocket.OPEN`; `OPEN` means the connection is ready to communicate.
if (socket.readyState === WebSocket.OPEN) {
socket.send('hello');
}WHY THIS HELPS
When reviewing WebSocket message handling, check a socket's state before sending a message.
REQUIREMENTS AND LIMITATIONS
- Version: v2.3.0
- socket.readyState (required): Current connection state; compare it with a ready-state constant.
- WebSocket.OPEN (required): Constant with value `1`; connection is open and ready to communicate. Constants are read-only and also available on each instance.
- socket.send(data) (required): Sends data through the WebSocket.
FIRST-PARTY EVIDENCE
- https://github.com/grafana/k6/releases/tag/v2.3.0
- https://github.com/grafana/k6/blob/v2.3.0/release%20notes/v2.3.0.md
- https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/
- https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/
Check the installed version and the source before using it. Entry JSON