No results for

Powered byAlgolia
⚠️ This is archived documentation for v0.43. Go to the latest version


Experimental module, use at your own risk

While we intend to keep this module as simple and stable as possible, we may need to add features or introduce breaking changes. This could happen at any time until we release this module as stable.

Feel free to provide user feedback, and open an issue or pull request if you have any suggestions.

This experimental API implements the browser WebSocket API with additional k6-specific functionalities (cookies, tags, headers and so on).

The main difference between this module and k6/ws is that this module uses a global event loop instead of a local one. A global event loop lets a single VU have multiple concurrent connections, which improves performance.

The WebSocket API is not fully implemented, and we're working on it, but we believe it's usable for most users. So whether you're writing a new WebSocket test, or currently using the k6/ws module, we invite you to give it a try, and report any issues in the project's issue tracker. Our midterm goal is to make this module part of k6 core, and long-term to replace the k6/ws module.

ParamsUsed for setting various WebSocket connection parameters such as headers, cookie jar, compression, etc.
WebSocket(url, protocols, params)Constructs a new WebSocket connection.
WebSocket.close()Close the WebSocket connection. a ping.
WebSocket.send(data)Send string data.
WebSocket.addEventListener(event, handler)Add an event listener on the connection for specific event.

A WebSocket instance also has the following properties:

WebSocket.readyStateThe current state of the connection. Could be one of the four states.
WebSocket.urlThe URL of the connection as resolved by the constructor.
WebSocket.bufferedAmountThe number of bytes of data that have been queued using calls to send() but not yet transmitted to the network.
WebSocket.binaryTypeThe binaryType is by default "ArrayBuffer". Setting it throws an exception, as k6 does not support the Blob type.
WebSocket.onmessageA handler for message events.
WebSocket.onerrorA handler for error events.
WebSocket.onopenA handler for open events.
WebSocket.oncloseA handler for close events.
WebSocket.onpingA handler for ping events.
WebSocket.onpongA handler for pong events.


This example shows:

  • How a single VU can run multiple WebSockets connections asynchronously.
  • How to use the timeout and interval functions to stop the connections after some period.
import { randomString, randomIntBetween } from '';
import { WebSocket } from 'k6/experimental/websockets';
import { setTimeout, clearTimeout, setInterval, clearInterval } from 'k6/experimental/timers';
const chatRoomName = 'publicRoom'; // choose any chat room name
const sessionDuration = randomIntBetween(5000, 60000); // user session between 5s and 1m
export default function () {
for (let i = 0; i < 4; i++) {
function startWSWorker(id) {
// create a new websocket connection
const ws = new WebSocket(`wss://${chatRoomName}/`);
ws.addEventListener('open', () => {
// change the user name
ws.send(JSON.stringify({ event: 'SET_NAME', new_name: `Croc ${__VU}:${id}` }));
// listen for messages/errors and log them into console
ws.addEventListener('message', (e) => {
const msg = JSON.parse(;
if (msg.event === 'CHAT_MSG') {
console.log(`VU ${__VU}:${id} received: ${msg.user} says: ${msg.message}`);
} else if (msg.event === 'ERROR') {
console.error(`VU ${__VU}:${id} received:: ${msg.message}`);
} else {
console.log(`VU ${__VU}:${id} received unhandled message: ${msg.message}`);
// send a message every 2-8 seconds
const intervalId = setInterval(() => {
ws.send(JSON.stringify({ event: 'SAY', message: `I'm saying ${randomString(5)}` }));
}, randomIntBetween(2000, 8000)); // say something every 2-8 seconds
// after a sessionDuration stop sending messages and leave the room
const timeout1id = setTimeout(function () {
console.log(`VU ${__VU}:${id}: ${sessionDuration}ms passed, leaving the chat`);
ws.send(JSON.stringify({ event: 'LEAVE' }));
}, sessionDuration);
// after a sessionDuration + 3s close the connection
const timeout2id = setTimeout(function () {
console.log(`Closing the socket forcefully 3s after graceful LEAVE`);
}, sessionDuration + 3000);
// when connection is closing, clean up the previously created timers
ws.addEventListener('close', () => {
console.log(`VU ${__VU}:${id}: disconnected`);