---
title: "Test WebSockets with proxymock | Speedscale Docs"
description: "Record an inbound WebSocket session, replay it against a service, and mock the same exchange as an outbound dependency."
canonical: "https://docs.speedscale.com/guides/websocket/proxymock/"
---

[Documentation index](https://docs.speedscale.com/guides/llms.txt) | [Markdown](https://docs.speedscale.com/guides/websocket/proxymock/index.md) | [Canonical page](https://docs.speedscale.com/guides/websocket/proxymock/)

# Test WebSockets with proxymock

Use one short session to test both sides of a WebSocket connection. The demo server sends `{"type":"welcome"}`, the client sends `check`, the server replies `{"type":"echo","text":"check"}`, and the client closes with code `1000`.

<a id="start-the-demo"></a>

## Start the demo

Use Node.js 22 or newer and [an installed and initialized proxymock CLI](https://docs.speedscale.com/proxymock/getting-started/installation/). In a new directory, run:

```bash
npm init -y
npm install ws
curl -fsSLO https://docs.speedscale.com/examples/browser-websocket/demo.mjs
curl -fsSLO https://docs.speedscale.com/examples/browser-websocket/socket-client.mjs
node demo.mjs
```

Leave the server running on port `8080`. Run the following commands in separate terminals in this directory.

<a id="record-the-services-inbound-session"></a>

## Record the service's inbound session

Start a recorder in front of the application:

```bash
proxymock record --app-port 8080 --out ./socket-inbound --out-format json
```

Run the sample client through the inbound proxy:

```bash
node socket-client.mjs ws://localhost:4143/ws
```

Wait for the client to print `Received welcome and echo, closed with 1000`, then stop the recorder. Open `proxymock web --in ./socket-inbound` and find the upgrade and its client and server messages. Keep the whole connection together when preparing the replay.

<a id="replay-against-the-service"></a>

## Replay against the service

Replay the inbound recording against the server on its application port:

```bash
proxymock replay --in ./socket-inbound --test-against http://localhost:8080 \
  --test-config regression --out ./socket-baseline \
  --fail-if 'requests.response-match-pct < 100'
```

The regression test config checks status codes, headers, and schemas. `--fail-if` also fails the run when a message body differs from the recording. The baseline passes.

Restart the server with `REGRESSION=1 node demo.mjs` and repeat the replay with `--out ./socket-regression`. The upgrade still succeeds and the server still sends the welcome, but the echo's `text` changes from `check` to `changed`. The run exits with code 1:

```text
requests.response-match-pct: 83.33
 ✘ failed eval "requests.response-match-pct < 100.00" - observed requests.response-match-pct was 83.33
```

`socket-regression/replay-verdict.json` names the message and the field:

```bash
jq '.pairs[] | select(.bodyMatch == "fail") | .bodyChanges' socket-regression/replay-verdict.json
```

```json
[{ "severity": "regression", "kind": "value_changed", "endpoint": "SERVER /ws",
   "location": "http.res.bodyBase64.text", "baseline": "check", "candidate": "changed" }]
```

Plain-text messages are checked the same way. If the server sent `welcome` and echoed `check` as plain text, the changed echo would fail with the location `http.res.bodyBase64`, the whole message body.

For a short load run, restart the unchanged server and run:

```bash
proxymock replay --in ./socket-inbound --test-against http://localhost:8080 \
  --vus 5 --for 30s --out ./socket-load
```

Each virtual user opens its own connection and replays the whole session, handshake first, as many times as the run allows. Each message counts as a request in the latency and throughput table. Use `https://` as the replay target when the service uses `wss://`.

> **Coding agent prompt**
> 
> Record the demo's inbound WebSocket session and verify that the handshake, welcome, client message, reply, and close are present. Run a baseline and then replay against the changed server. Show the first server message that differs.

<a id="mock-the-downstream-endpoint"></a>

## Mock the downstream endpoint

Restart the unchanged server. Record the demo as an outbound dependency through a mapped port:

```bash
proxymock record --map 8081=http://localhost:8080 --out ./socket-outbound --out-format json
```

Run the client against the mapped port and wait for it to close:

```bash
node socket-client.mjs ws://localhost:8081/ws
```

Stop the recorder and the demo server. Start the mock with the same mapping so the client can reuse its address:

```bash
proxymock mock --in ./socket-outbound --map 8081=http://localhost:8080 --no-passthrough
```

Run the same client command on port `8081`. It should print `Received welcome and echo, closed with 1000`. The live server is stopped, so the successful exchange verifies that the mock served it. If the handshake fails, confirm that the recording is outbound and contains the upgrade and messages for `/ws`.

> **Coding agent prompt**
> 
> Record the demo's outbound WebSocket session using the mapped port. Stop the server, start proxymock in mock mode, and run the same client through the mock. Verify the messages and close code.

See [WebSocket Testing](https://docs.speedscale.com/guides/websocket/) for message matching, limitations, and troubleshooting.
