No results for

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

connect( url, params, callback )

Initiate a WebSocket connection to a remote host.

Calling connect will block the VU finalization until the WebSocket connection is closed. Instead of continuously looping the main function (export default function() { ... }) over an over, each VU will be halted listening to async events and executing their event handlers until the connection is closed.

The following events can close the connection:

  • remote host close event.
  • Socket.close().
  • k6 VU interruption based on test configuration or CLI commands.
ParameterTypeDescription
urlstringRequest URL (e.g. "ws://echo.websocket.org").
paramsobjectParams object containing additional request parameters.
callbackfunctionThe callback function that will be called when the WebSocket connection is initiated. A Socket object will be passed to the function, and this object can be used to set up callbacks etc when things happen on the WebSocket connection

Returns

TypeDescription
ResponseHTTP Response object.

Example

import ws from 'k6/ws';
export default function () {
const url = 'ws://echo.websocket.org';
const resp = ws.connect(url, null, function (socket) {
socket.on('open', function () {
console.log('WebSocket connection established!');
socket.close();
});
});
}