Load test WebSocket APIs with Gatling: connect, send text or binary frames, define check sequences, match messages, and process unmatched inbound traffic.
Use this file to discover all available pages before exploring further.
Gatling’s WebSocket support is an extension to the HTTP SDK, making ws a first-class citizen in your scenario alongside regular HTTP actions. Because WebSocket is a full-duplex protocol—messages can arrive from the server at any time, not only in response to a client request—Gatling models the WebSocket flow as a parallel branch within a scenario. The HTTP branch and the WebSocket branch run concurrently, each maintaining its own state; you can reconcile them by saving values captured in WebSocket checks back into the shared Session. WebSocket support was originally contributed by Andrew Duffy.
Opening a WebSocket connection is the first step. The entry point is the HTTP URL of the WebSocket endpoint (the ws:// or wss:// scheme is determined by your HTTP protocol configuration).
Define a chain of actions to execute right after the WebSocket is (re-)connected. This is useful for sending an authentication frame or a subscription message immediately upon connection:
Use ws.checkTextMessage("checkName") or ws.checkBinaryMessage("checkName") to build a check definition. Almost all standard Gatling check criteria are available.
Java
// Single criterionws.checkTextMessage("price update") .check(jsonPath("$.price").saveAs("latestPrice"))// Multiple criteria on the same framews.checkTextMessage("order fill") .check( jsonPath("$.type").is("fill"), jsonPath("$.orderId").saveAs("filledOrderId"), jsonPath("$.quantity").ofInt().is(100) )
Use matching to filter which inbound frames a check should apply to. Non-matching frames are ignored. The matching criterion is a standard check expression that does not accept saveAs.
Java
ws.checkTextMessage("order update for my order") .matching(jsonPath("$.orderId").is("#{orderId}")) .check(jsonPath("$.status").saveAs("orderStatus"))
Messages that arrive but are not matched by any active check can be buffered and processed later. Enable buffering by setting wsUnmatchedInboundMessageBufferSize on the HTTP protocol:
The buffer is reset when a message is sent or processUnmatchedMessages is called.Call ws.processUnmatchedMessages on the ws DSL object (not on a named action):