A module with a better and standard API exists
The new k6/experimental/websockets API partially implements the WebSockets API living standard.
When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance.
Socket is a WebSocket client to interact with a WebSocket connection. You can use it to listen various events happening on the WebSocket connection and send messages to the server. Additionally, you can use socket.setTimeout() and socket.setInterval() to execute code in the background, or repeatedly, while the WebSocket connection is open.
Method | Description |
---|---|
Socket.close() | Close the WebSocket connection. |
Socket.on(event, callback) | Set up an event listener on the connection for any of the following events: - open - binaryMessage - message - ping - pong - close - error. |
Socket.ping() | Send a ping. |
Socket.send(data) | Send string data. |
Socket.sendBinary(data) | Send binary data. |
Socket.setInterval(callback, interval) | Call a function repeatedly at certain intervals, while the connection is open. |
Socket.setTimeout(callback, period) | Call a function with a delay, if the connection is open. |
WebSocket built-in metrics
k6 automatically collects metrics when interacting with a WebSocket service through the k6/ws API. Review the list in the metrics reference.
Check out the Results output article for more information about how to process the metric information.