k6/ws module provides a WebSocket client implementing the WebSocket protocol. This module uses a local event loop that blocks VU execution until the connection is closed.
For better performance with multiple concurrent connections, consider using
k6/websockets which uses a global event loop.Overview
Thek6/ws module creates WebSocket connections that block test execution until closed. This is suitable for testing scenarios where each VU maintains a single long-lived WebSocket connection.
Importing the Module
API Reference
Functions
Creates a WebSocket connection and provides a Socket client to interact with the service. The method blocks test finalization until the connection is closed.Parameters:Returns:
WebSocket URL (e.g.,
wss://echo.websocket.org)Connection parameters (headers, cookies, compression, tags)
Function called when the WebSocket connection is initiated. Receives a Socket object.
HTTP Response object from the WebSocket handshake
Socket Class
The Socket object is passed to the callback function and provides methods to interact with the WebSocket connection.Methods
Closes the WebSocket connection.
Sets up an event listener on the connection.Events:
open- Connection establishedmessage- Text message receivedbinaryMessage- Binary message receivedping- Ping receivedpong- Pong receivedclose- Connection closederror- Error occurred
Event name to listen for
Handler function for the event
Sends a ping frame to the server.
Connection Parameters
Params Object
Compression algorithm to use. Currently only
"deflate" is supported.Cookie jar for the WebSocket connection. Uses the default VU cookie jar if not specified.
Custom HTTP headers to include in the WebSocket handshake request.
Custom metric tags for filtering results and setting thresholds.
Connection Lifecycle
Callingconnect() will block the VU until the WebSocket connection is closed by one of:
- Remote host close event
Socket.close()call- k6 VU interruption (test end, CLI command)
Examples
Basic WebSocket Connection
Custom Headers and Parameters
WebSocket Metrics
k6 automatically collects WebSocket-specific metrics:| Metric | Type | Description |
|---|---|---|
ws_connecting | Trend | Time to establish connection |
ws_session_duration | Trend | Total session duration |
ws_sessions | Counter | Total number of sessions |
ws_msgs_sent | Counter | Messages sent |
ws_msgs_received | Counter | Messages received |
ws_ping | Trend | Ping round-trip time |
Comparison with k6/websockets
| Feature | k6/ws | k6/websockets |
|---|---|---|
| Event Loop | Local (blocks VU) | Global (non-blocking) |
| Concurrent Connections | One per VU | Multiple per VU |
| API Style | Callback-based | Browser WebSocket API |
| Performance | Good for single connections | Better for multiple connections |
| Use Case | Traditional WebSocket testing | Modern async WebSocket testing |
Related Resources
k6/websockets
Modern WebSocket API with global event loop
Params
WebSocket connection parameters
WebSocket Testing Guide
Learn WebSocket testing patterns
Metrics Reference
WebSocket metrics documentation