---
title: "connect( url, params, callback ) | Grafana k6 documentation"
description: "Create a WebSocket connection, and provides a Socket client to interact with the service."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

# connect( url, params, callback )

> Note
> 
> A module with a better and standard API exists.
> 
> The new [k6/websockets API](/docs/k6/next/javascript-api/k6-websockets/) partially implements the [WebSockets API living standard](https://websockets.spec.whatwg.org/).
> 
> When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance.

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()](/docs/k6/next/javascript-api/k6-ws/socket/socket-close/).
- k6 VU interruption based on test configuration or CLI commands.

Expand table

| Parameter | Type     | Description                                                                                                                                                                                                                                                                            |
|-----------|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| url       | string   | Request URL (e.g. “wss://echo.websocket.org”).                                                                                                                                                                                                                                         |
| params    | object   | [Params](/docs/k6/next/javascript-api/k6-ws/params/) object containing additional request parameters.                                                                                                                                                                                  |
| callback  | function | The callback function that will be called when the WebSocket connection is initiated. A [Socket](/docs/k6/next/javascript-api/k6-ws/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

Expand table

| Type                                                       | Description           |
|------------------------------------------------------------|-----------------------|
| [Response](/docs/k6/next/javascript-api/k6-http/response/) | HTTP Response object. |

### Example

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
import ws from 'k6/ws';

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