Skip to main content
The 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

The k6/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

function
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:
string
required
WebSocket URL (e.g., wss://echo.websocket.org)
Params
Connection parameters (headers, cookies, compression, tags)
function
required
Function called when the WebSocket connection is initiated. Receives a Socket object.
Returns:
Response
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

function
Closes the WebSocket connection.
function
Sets up an event listener on the connection.Events:
  • open - Connection established
  • message - Text message received
  • binaryMessage - Binary message received
  • ping - Ping received
  • pong - Pong received
  • close - Connection closed
  • error - Error occurred
Parameters:
string
required
Event name to listen for
function
required
Handler function for the event
function
Sends a ping frame to the server.
function
Sends string data through the WebSocket.Parameters:
string
required
String data to send
function
Sends binary data through the WebSocket.Parameters:
ArrayBuffer
required
Binary data to send
function
Calls a function repeatedly at specified intervals while the connection is open.Parameters:
function
required
Function to call repeatedly
number
required
Interval in milliseconds
function
Calls a function after a delay if the connection is still open.Parameters:
function
required
Function to call after delay
number
required
Delay in milliseconds

Connection Parameters

Params Object

string
Compression algorithm to use. Currently only "deflate" is supported.
http.CookieJar
Cookie jar for the WebSocket connection. Uses the default VU cookie jar if not specified.
object
Custom HTTP headers to include in the WebSocket handshake request.
object
Custom metric tags for filtering results and setting thresholds.

Connection Lifecycle

Calling connect() will block the VU until the WebSocket connection is closed by one of:
  1. Remote host close event
  2. Socket.close() call
  3. k6 VU interruption (test end, CLI command)

Examples

Basic WebSocket Connection

Custom Headers and Parameters

WebSocket Metrics

k6 automatically collects WebSocket-specific metrics:

Comparison with k6/websockets

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