WebSocket error handling exception

Hi Team,
I am using K6 to carry out performance test for our products. However, I met an issue recently and have no idea how to resolve it. Really appreciate if you can help me.
The problem is that I implemented the websocket connection with the example code described in document like below:

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());
      }
    });

    socket.setTimeout(function () {
      console.log('2 seconds passed, closing the socket');
      socket.close();
    }, 2000);
  });

I can confirmed websocket was working well originally during the performance test. But sometimes, the error was captured during the life of websocket connection. I tried to print out the error with the following code

 if (e.error() != "websocket: close sent") {
        console.log('An unexpected error occured: ', e.error());
      }

Then I always got Cannot read property ‘error’ of undefined exception. Without error information, we cannot figure out what reason caused the Websocket issue. It would be grateful if you are able to help me.

Regards,
Johnny

Hi … this looks like a really old bug that noone has ever reported. Basically if there is an error when writing the close message k6 instead of the error that was returned will send a nil/null error to tha handler. This appears to be the only place where this could happen so I am pretty sure this is the issue.

I have already made a fix and PR but even if we merge it we are unlikely to make a release … soon™. If you can build from that branch and test it I would be grateful.
And thanks for reporting this :slight_smile:

Thanks Mstoykov for your quick fix. Unfortunately I didn’t have environment to build it. Would you please provide an installer built from that branch to me so that I have chance to verify it.

What OS are you on ?

Windows @mstoykov. It would be better if I can download the installer from somewhere. Thanks a lot.

Here, @Johnny_H, should work only once :slight_smile: because this is how firefox’s send work’s without a registration :frowning:

@mstoykov, unfortunately I am not able to access that link. Is it possible to upload this installer to GitHub? Thanks for your great support. :slight_smile:

well a private release it is apparently