While we intend to keep experimental modules as stable as possible, we may need to introduce breaking changes. This could happen at future k6 releases until the module becomes fully stable and graduates as a k6 core module. For more information, refer to the extension graduation process.
Experimental modules maintain a high level of stability and follow regular maintenance and security measures. Feel free to open an issue if you have any feedback or 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.
|Params||Used 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.|
|WebSocket.ping()||Send 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.readyState||The current state of the connection. Could be one of the four states.|
|WebSocket.url||The URL of the connection as resolved by the constructor.|
|WebSocket.bufferedAmount||The number of bytes of data that have been queued using calls to send() but not yet transmitted to the network.|
|WebSocket.binaryType||The binaryType is by default "ArrayBuffer". Setting it throws an exception, as k6 does not support the Blob type.|
|WebSocket.onmessage||A handler for message events.|
|WebSocket.onerror||A handler for error events.|
|WebSocket.onopen||A handler for open events.|
|WebSocket.onclose||A handler for close events.|
|WebSocket.onping||A handler for ping events.|
|WebSocket.onpong||A handler for pong events.|
k6 takes specific measurements for Websockets. For the complete list, refer to the Metrics reference.
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.