Websocket request using K6

How to capture error code/ status code of WebSocket Request?
Below is my code

//SessionID is generated from Jmeter and used here. 

import ws from 'k6/ws';

import { check } from 'k6';

import { Rate } from 'k6/metrics';

// A custom metric to track failure rates

var failureRate = new Rate("check_failure_rate");

// Options

export let options = {

    //stages for spike test

    stages: [

  

      { duration: '30s', target: 1000 }, // below normal load

      { duration: '1m', target: 1000 },

      { duration: '30s', target: 5000 }, // spike to 5000 users

      { duration: '3m', target: 5000 }, // stay at 5000 for 3 minutes

      { duration: '30s', target: 1000 }, // scale down. Recovery stage.

      { duration: '3m', target: 1000 },

      { duration: '30s', target: 0 },

  

    ],

    

      noVUConnectionReuse: false, //specifying whether k6 should reuse TCP connections between iterations of a VU

      rps: 50, //The maximum number of requests to make per second, in total across all VUs

      summaryTimeUnit: 'ms', //Define which time unit will be used for all time values in the end-of-test summary. Possible values are s (seconds), ms (milliseconds) and us (microseconds). If no value is specified, k6 will use mixed time units, choosing the most appropriate unit for each value.

      discardResponseBodies: true,

  };

  export default function () {

    var url = 'wss://sauat.bajajfinserv.in/wsc/recommendedbanneroffersuat'; //API endpoint

  

    //code for generation of sessionID

    // var wsSessionID = Math.random().toString(36).substr(2, 11) + Math.random().toString(36).substr(2, 11);

    var wsSessionID = "16257408141770SkkHg0FfT2g7J2rmOLXb4vsLOok00"; //sessionID generated from Jmeter

    var operation = 'recommendedbanneroffersuat'; //operation end point

    var requestBody = "/hl7dVflT0Sch7VRPn8joVSbJmPGVfHCsQ4ah7j7X+MGZ6F/QUvAOvnCug1HApdrUz/bwqsoGV5X67gbDCWkATJavaRqiPZRjZhljNmt3a/hgL+xsJBEtrkR12dDZATPfPqOGWwbNaKfa1NzTC/seA=="; //encrypted request body

  

    let params = {

      "headers":{

          "Authorization":"Bearer clpVU3kwSFNKZlA5TUtrOl9jUGtLb3VwLXMtZGlSQlZHN1ZKZ040Vmh1QQ==",

          "token":"dd8f4eee15c338ebf2484e772a15a05a",

          // "wsSessionID":"${wsSessionID}",

          "wsSessionID": wsSessionID,

          "operation": operation,

          "source":"local",

          "requestCounter":"1"

        },

      "requestBody":requestBody

    }

    // console.log(JSON.stringify(params));

  

    var response = ws.connect(url, params, function (socket) {

      socket.on('open', function open() {

        console.log('connected');

        socket.send(Date.now());

  

        // socket.setInterval(function timeout() {

        //   socket.ping();

        //   console.log('Pinging every 1sec (setInterval test)');

        // }, 1000);

      });

  

      socket.on('ping', function () {

        console.log('PING!');

      });

  

      socket.on('pong', function () {

        console.log('PONG!');

      });

  

      socket.on('pong', function () {

        // Multiple event handlers on the same event

        console.log('OTHER PONG!');

      });

  

      socket.on('message', function (message) {

        console.log(`Received message: ${message}`);

      });

  

      socket.on('close', function () {

        console.log('disconnected');

      });

  

      socket.on('error', function (e) {

        if (e.error() != 'websocket: close sent') {

          console.log('An unexpected error occured: ', e.error());

        }

      });

  

      //The timeout will close down the WebSocket connection after 10 seconds.

      socket.setTimeout(function () {

        console.log('10 seconds passed, closing the socket');

        socket.close();

      }, 10000);

    });

  

    // To display the response code

     let checkRes = check(response, { 

       ['status is 101']: (r) => r && r.status === 101 });

  

      check(response, {

      ["Error code is " + response.error_code]: (r) => r && r.error_code != 0 });

  

  // We reverse the check() result since we want to count the failures

  failureRate.add(!checkRes);

  }

This is the metric produced after test.

Hi @neha13rb ,

Currently websockets do not have error codes. There is an open issue about it, but nobody has started working on it and there are some internal changes that will be needed in order for it to be doable. If you are interested in making a PR, please first say so in the issue so we can discuss it :).

Apart from that, I do think this is pretty low on the priority list of the OSS developers as there are other at least seemingly more important things on the list.

Sorry about that and if you really need them to diagnose something please say so there so we can better gauge the importance of the issue.

Thanks @mstoykov.
I will do the necessary things required.