A module with a better and standard API exists
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.
|Socket.close()||Close the WebSocket connection.|
|Socket.on(event, callback)||Set up an event listener on the connection for any of the following events:|
|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.|
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.