WebSocket is a full-duplex communication protocol that keeps a persistent TCP connection open between a client and a server, enabling either side to push data at any time. Real-time dashboards, collaborative editing tools, live auction platforms, and multiplayer games all rely on WebSockets for low-latency, bidirectional communication. Gatling’s built-in WebSocket support lets you connect, send messages, check inbound frames, and handle unsolicited server-pushed messages — all within the same virtual-user model you use for HTTP testing.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/gatling/gatling.io-doc/llms.txt
Use this file to discover all available pages before exploring further.
The code examples in this guide use the Java SDK. The same concepts and method names apply to the Kotlin and Scala SDKs.
Prerequisites
- Gatling version 3.13 or higher
- Clone gatling/devrel-projects and navigate to
articles/websocketguide - Node.js (to run the demo WebSocket server)
The Demo Server
The guide uses a simple Node.js WebSocket server that accepts connections and sends back a random number of messages at random intervals between 0 and 1 second:Writing the Simulation
Create the Scenario
The scenario below connects to the WebSocket server, waits for a specific first message, and then processes remaining buffered messages:| Component | Description |
|---|---|
ws("name").connect("/") | Opens a WebSocket connection to the path |
.await(seconds).on(check) | Blocks and waits up to N seconds for a matching inbound message |
ws.checkTextMessage("name").check(...) | Defines a check to apply against text frames |
processUnmatchedMessages(...) | Handles buffered messages that were not matched by any explicit check |
ws("name").close() | Sends a close frame and tears down the connection |
Inject a Single User for Verification
Always verify your scenario works correctly with a single virtual user before ramping up:Scaling Up
Once the single-user run succeeds, increase the injection profile to simulate realistic concurrent WebSocket connections:Running the Test
Place the simulation in your project
Copy the simulation class into
src/test/java/ (or the appropriate source directory for your build tool).Monitoring WebSocket Connections in Reports
WebSocket simulations benefit from Gatling Enterprise Edition’s Connections tab, which shows:- TCP connection open/close rates — confirms that virtual users are establishing and tearing down connections as expected
- Concurrent connections over time — helps correlate performance changes with connection counts
- TLS handshake times — relevant when testing
wss://endpoints
Best Practices
Use secure WebSockets
In production, your WebSocket endpoint should use
wss:// (TLS). Update baseUrl to https:// (Gatling uses wss:// automatically when the base URL is HTTPS) and add .disableCaching() if needed.Buffer asynchronous messages
Real WebSocket servers may push messages at any time. Use
processUnmatchedMessages to drain the buffer and avoid memory growth during long-running simulations.Add realistic pauses
Insert
.pause() calls between sends to model the natural pacing of your application’s clients. Without pauses, virtual users may flood the server with far more messages per second than a real client would send.Include error handling
Use
exitBlockOnFail() around the connection and initial check so that virtual users who cannot establish a connection do not continue executing the rest of the scenario.