k6/ws module provides WebSocket client functionality for testing real-time applications.
Functions
connect(url, [params], callback)
Establishes a WebSocket connection.string
WebSocket URL (ws:// or wss://)
object
Connection parameters
function
Function to execute after connection is established. Receives Socket object.
HTTPResponse
HTTP response object from the connection handshake
Connection Parameters
object
Custom headers for the WebSocket handshake
object
Custom tags for metrics
CookieJar
Cookie jar to use for the connection
string
Compression algorithm (
deflate is supported, experimental)Socket Object
The Socket object is passed to the connect callback and provides methods to interact with the WebSocket.Event Handlers
on(event, handler)
Registers an event handler.string
Event name:
open, message, binaryMessage, ping, pong, close, or errorfunction
Function to call when event occurs
Sending Messages
send(data)
Sends a text message.string
Message to send
sendBinary(data)
Sends a binary message.ArrayBuffer
Binary data to send
ping()
Sends a ping frame.Timing Functions
setTimeout(callback, delay)
Executes a function after a delay.function
Function to execute
number
Delay in milliseconds
setInterval(callback, interval)
Executes a function repeatedly at intervals.function
Function to execute
number
Interval in milliseconds
Connection Control
close([code])
Closes the WebSocket connection.number
WebSocket close code (default: 1001)