How to capture the server data in k6 scripts

Hi Team,

Scenario : There is one POST call , it writes the data in queue and leave.

After some time there is one socket event raised and server sends data back to UI.

Requesting you to help us out the k6 scripting for the same.

Are you referring to a WebSocket here? If so, and this isn’t a case of simply looking at the response.body, then you need to open a WebSocket connection and await the message you’re expecting to see. I recommend reading the documentation and experimenting with it.

Please give it a try and show us any code that you’ve tried and we may assist further. Any detail you can provide on the protocols involved would help, too.

Please find below the sample code and getting the below error.

let url = ws://localhost:3000/socket.io/?EIO=3&transport=websocket

let pathId;
ws.connect(url, null, function (socket) {​​​​​​​
socket.on(‘open’, function () {​​​​​​​
console.log(‘WebSocket connection established!’);
let response = http.post(urlpath, JSON.stringify(data), params);
check(response, {​​​​​​​
‘url status 200’: (response) => response.status === 200
}​​​​​​​);
if (response.status === 200) {​​​​​​​
pathId = response.body.replace(/"/gi, ‘’);
}​​​​​​​
else {​​​​​​​
console.log(request failed with Error Code ${​​​​​​​response.status}​​​​​​​);
}​​​​​​​
}​​​​​​​);

    socket.on('message', function (message) {​​​​​​​
    // Printing all the statuses , closing the socket once status is active
        console.log(`Received message: ${​​​​​​​message}​​​​​​​`);
    if (response.status === Active) {​​​​​​​
            socket.close();
        }​​​​​​​
    }​​​​​​​);


    socket.on('close', function () {​​​​​​​
        console.log('WebSocket connection disconnected!');
    }​​​​​​​);


    socket.on('error', function (e) {​​​​​​​
        console.log('An unexpected error occured: ', e.error());
    }​​​​​​​);


}​​​​​​​);
return pathId;

Connection gets disconnected and all the status messages are not received.

Requesting you to please help me with the same.

@Tom : Requesting you to please help me with the above query.

The code looks OK, but it’s hard to tell without seeing your whole script and what settings you’re using to run it.

A quick google suggests close 1005 corresponds to a CloseEvent, which appears to be a server-initiated request to close the connection. Anyone’s guess why it’s doing that, though… :slight_smile:

You may want to have a look at DevTools’ Network tab when you interact with the service. Alternatively, proxying the traffic through a web proxy like Fiddler or Charlesproxy if it’s not browser-based. These tools cater for inspection of the WebSocket frames (messages) that will help you understand how to imitate the client.

@Tom : It is simple script and using k6 run command to run the script.

In fiddler , we cant see the socket connection details.

Requesting you to share me the sample running socket code in k6.

As my requirement is to create the socket and get the messages from the server by using the below code.

socket.on(‘message’, function (message) {​​​​​​​
}​​​​​​​);

If you can share some sample runnable code.

The example here works if you simply copy & paste the code and run it. Here’s a slightly modified version that also uses messages:

import ws from 'k6/ws';
import { check } from 'k6';
export default function () {
  var url = 'ws://echo.websocket.org';
  var params = { tags: { my_tag: 'hello' } };
  var res = ws.connect(url, params, function (socket) {
    socket.on('open', function open() {
      console.log('connected');
      socket.setInterval(function timeout() {
        socket.ping();
        console.log('Pinging every 1sec (setInterval test)');
      }, 1000);
      // TM - send a message
      socket.send("a message");
    });
    // TM - and handle the response
    socket.on('message', function(message) {
      console.log("Received message: " + message);
    });
    socket.on('ping', function () {
      console.log('PING!');
    });
    socket.on('pong', function () {
      console.log('PONG!');
    });
    socket.on('close', function () {
      console.log('disconnected');
    });
    socket.setTimeout(function () {
      console.log('10 seconds passed, closing the socket');
      socket.close();
    }, 10000);
  });
  check(res, { 'status is 101': (r) => r && r.status === 101 });
}

When you run this, you should see the socket.on('message') event handler outputting the response.

Based on your console output, you are able to see messages arrive; the first message has a sid among other things, but the 2nd and 3rd don’t seem to have anything except an identifier (40, 41). As we don’t know the server-side WebSocket implementation, we can’t tell you what those mean; for all we know, the client is meant to respond to those messages, and not doing so within a certain amount of time could cause the server to close the connection.

Perhaps it would make sense for us to jump on a call so that you can show me what you’re working with. Please send us an email via support@k6.io and we’ll set that up.

Hi @Tom , I am waiting for the approval so that I can show the code .

In the meanwhile I was just trying to test the socket code in k6 for below website:
https://www.piesocket.com/websocket-tester#

  var url = 'wss://piesocket.helpcrunch.com/socket.io/?EIO=3&transport=websocket';
  var res = ws.connect(url, null, function (socket) {
    socket.on('open', function open() {
      console.log('connected');
      socket.setInterval(function timeout() {
        socket.ping();
        console.log('Pinging every 1sec (setInterval test)');
      }, 1000);
    });
    // TM - and handle the response
    socket.on('message', function (message) {
      console.log("Received message: " + message);
    });
    socket.on('ping', function () {
      console.log('PING!');
    });
    socket.on('pong', function () {
      console.log('PONG!');
    });
    socket.on('close', function () {
      console.log('disconnected');
    });
    socket.setTimeout(function () {
      console.log('10 seconds passed, closing the socket');
      socket.close();
    }, 10000);
  });
  check(res, { 'status is 101': (r) => r && r.status === 101 });

I am getting the below output :

INFO[0001] connected                                     source=console
**INFO[0001] Received message: 0{"sid":"NrVprltsq7r-pr0dmqyD","upgrades":[],"pingInterval":10000,"pingTimeout":30000}  source=console**
**INFO[0001] Received message: 40                          source=console**
INFO[0002] Pinging every 1sec (setInterval test)         source=console
INFO[0002] PONG!                                         source=console
INFO[0003] Pinging every 1sec (setInterval test)         source=console
INFO[0003] PONG!                                         source=console
INFO[0004] Pinging every 1sec (setInterval test)         source=console
INFO[0004] PONG!                                         source=console
INFO[0005] Pinging every 1sec (setInterval test)         source=console
INFO[0005] PONG!                                         source=console
INFO[0006] Pinging every 1sec (setInterval test)         source=console
INFO[0006] PONG!                                         source=console
INFO[0007] Pinging every 1sec (setInterval test)         source=console
INFO[0007] PONG!                                         source=console
INFO[0008] Pinging every 1sec (setInterval test)         source=console
INFO[0008] PONG!                                         source=console
INFO[0009] Pinging every 1sec (setInterval test)         source=console
INFO[0010] Pinging every 1sec (setInterval test)         source=console
INFO[0011] PONG!                                         source=console
INFO[0011] PONG!                                         source=console
INFO[0011] 10 seconds passed, closing the socket         source=console
INFO[0011] disconnected                                  source=console

But when I see the network socket in the website , I am getting few more messages like message 42 , but not getting in k6 script :

Requesting you to help me for the same.

hi @chkant,

From my very fast testing the 42 messages are outgoing from you to the server and at least I couldn’t reproduce them being sent with firefox or chromium without me typing them in the send bar.

Looking at your screenshot I don’t see another connection, so you don’t seem to be looking at the wrong one. On the other hand if you are connected to the websocket the pie socket site should have a field to send messages which you don’t :thinking: .

The 42 in front seems to be a socket.io thing and k6 does not support socket io and looking at this workaround the format is slightly different, but maybe it is a different socket io implementation :man_shrugging:

@mstoykov @Tom : Team , can you please confirm in k6 socket.io are not supported ?

wss://env.aaps.com/socket.io/?EIO=3&transport=websocket

Are there any official documentation available for the same ?