# Web Socket step

The WebSocket step enables real-time, bidirectional communication testing by allowing you to connect to WebSocket endpoints, send messages, and validate responses. This step is particularly useful for testing applications that require persistent connections and real-time data exchange.

## WebSocket Step Operations

A WebSocket step can perform three main operations:

1. **Connect** - Establishes a WebSocket connection to the specified endpoint
2. **Send** - Transmits messages through an established WebSocket connection
3. **Receive** - Listens for and validates incoming WebSocket messages

## Step Configuration

### Connection

![](https://684333474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LHDbUNdi3wPd9vSolzU%2Fuploads%2Fgit-blob-bca1d517f7dda341e093169c42ac13d5dba47c53%2Fws-step-connect.png?alt=media)

When establishing a connection:

* The URL format follows: `wss://<your-websocket-endpoint>`
* Headers can be added to the WebSocket handshake request for authentication or custom protocols
* A timeout value (in seconds) can be set for the connection attempt

### Sending Messages

![](https://684333474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LHDbUNdi3wPd9vSolzU%2Fuploads%2Fgit-blob-ad755e76b03faa54d02fcea28353a5a306b0a111%2Fws-step-send.png?alt=media)

For sending messages:

* Supports two data types:
  * Text (JSON, plain text)
  * Binary data
* Messages can include dynamic parameters
* Headers can be included with the message
* Used for both sending data and subscribing to channels
* A timeout can be specified for the send operation

Note: A Send step without a preceding Connect step will automatically create a new WebSocket connection.

### Receiving Messages

![](https://684333474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LHDbUNdi3wPd9vSolzU%2Fuploads%2Fgit-blob-7ec9ca1fa4b8014742ce6f53bff9de4d884eae13%2Fws-step-receive.png?alt=media)

When receiving messages:

* Messages are fetched based on the **WebSocket URL**
  * Use URL query parameters (e.g., `wss://example.com/ws?userId=1`) to send and filter messages by specific properties
* **Message retrieval options**:
  * Last - Returns the most recent single message
  * Last 10 - Returns an array of the last 10 messages
  * Last 50 - Returns an array of the last 50 messages
* **Extractions** support all standard Loadmill extraction methods
  * Example JSONPath for array filtering: `$[?(@.id == "${my_id}")]`
  * [Learn more about extractions](https://docs.loadmill.com/api-testing/test-suite-editor/set-parameters-extractions)
* **Wait for extractions to be resolved** - Since WebSocket connections are asynchronous, this setting defines how long to wait for messages that match your extraction criteria. Default is 5 seconds, but you may need to adjust this based on your application's behavior.
* **Assertions** - Use the same powerful assertion capabilities as HTTP requests to validate your WebSocket messages, including JSON/XML validation, value comparisons, and regex matching. [Learn more about assertions](https://docs.loadmill.com/api-testing/test-suite-editor/assertions)
* The **Flow Control** section allows to repeat the request until the parameter's value meets the requirement or number of iteration reached.

## Example Run

Let's look at a simple WebSocket test flow in action:

![](https://684333474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LHDbUNdi3wPd9vSolzU%2Fuploads%2Fgit-blob-c95e94aaeb4522ab540f1f416cd28517103600a1%2Fws-steps-example-run.png?alt=media)

This example demonstrates a complete WebSocket interaction:

#### 1. Connect to WebSocket:

* Establishes connection to a WebSocket server

#### 2. Send Some Data:

* Sends a message on the connection

#### 3. Assert message received:

* Waits for all extractions to be resolved
* Once resolved, validates that the extracted `${message}` exists
* Received messages are displayed for easy debugging


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.loadmill.com/test-editor/steps/web-socket-step.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
