K6 & Websockets understanding

Hello!

I’m currently experimenting with K6 to load test a web app that relies on both requests and WebSockets to simulate a user experience.

This is probably due to my lack of understanding in the technology but if the web app opens a WebSocket from a post request will K6 handle this in the way a user would navigating the site?

So -

  1. User makes a post request
  2. Websocket connection is opened and remains open whilst users make further post requests
  3. Websocket is closed at the end of the “exercise”

If I just do a post request with K6 will a websocket be opened in the background and handled by the VU or do I have to manually open one using the WS functionality of K6?

Issue being I’ve seen other posts regarding not being able to do requests whilst working with Websockets so just looking for some clarification on the above!

Thank you :smiley:

Hi, welcome to the forum :slight_smile:

In short: no, k6 will not open a WebSocket connection for you unless you use the k6/ws module directly.

In general, k6 doesn’t behave like a browser as it doesn’t interpret the server-side JavaScript, so you’ll need to implement this behavior in your test script.

For most applications the easiest way of getting a head start with advanced workflows is to use the browser recorder or to record to a HAR file, which you can then convert to a k6 script using the har-to-k6 tool.

Unfortunately the converter doesn’t yet handle WebSocket connections, so you’ll need to implement that manually. You could use the recorder/converter for the HTTP requests and then edit the script to add the WS handling as needed.

Though keep in mind that the HTTP request to upgrade the connection to WS must be a GET request according to the spec:

  1. The method of the request MUST be GET, and the HTTP version MUST be at least 1.1

Hope this helps,

Ivan

Thanks for your reply Ivan.

Am I correct in thinking if I open up a WebSocket connection I then won’t be able to follow up with HTTP requests with it still open?

The application I’m testing works via opening a WS per user and staying open prompting the user to do specific things (User fires a post request until the WS prompts to do something else) so unsure whether or not it’s going to be viable to test this way.

Am I correct in thinking if I open up a WebSocket connection I then won’t be able to follow up with HTTP requests with it still open?

No, that’s not the case. You can make any HTTP requests while the WS connection is open. Just keep in mind that the ws.connect() call is synchronous, meaning that it will block for the duration of the WS connection, while the event handlers (created with socket.on()) are asynchronous and are triggered on incoming events. So you can make HTTP requests before you call ws.connect(), in the event handlers themselves, e.g. when you receive a new message, or when the WS connection is closed.

Take a look at the documentation and examples.