Is there any way to using K6 to send socket pong back to WebSocket Server

We’ve got a WebSocket server to regularly send “PING” to the client to expect a “PONG” response, but from K6 side, we’ve just got socket.ping() function but no socket.pong() function to handle this. Is there any way to send “PONG” frame back to server in k6?
Thank you!

Hi @JackL , Welcome to the community forum.

k6 does automatically send pong back, but you can get notified that a ping happened using the socket.on pong event.

Hope this helps

Hi @mstoykov , thank you for the quick feedback. But during my test, it seems k6 does not automatically send pong back, I’m using Java websocket server (GitHub - TooTallNate/Java-WebSocket: A barebones WebSocket client and server implementation writte) and enabled lost connection detection (Lost connection detection · TooTallNate/Java-WebSocket Wiki · GitHub). And while using K6 as the websocket client, it will always get error like “The connection was closed because the other endpoint did not respond with a pong in time.”
From my script, I added code as below and could see the “PING” message logged in the console.
socket.on(‘ping’, function () {
console.log(‘PING!’);
});
The k6 version I used is k6 v0.31.1, any idea if anything wrong?
Thank you!

Can you also handle the error event and see if some error is being returned? as the code does send pong and then triggers the ping event so :man_shrugging:

Thank you, @mstoykov . I captured tcpdump and verified that k6 does send the pong frame, it seems some error happened at server side. :joy:

Glad to hear it’s not a bug in k6, but didn’t setting a handler on error provide you with an error message?

as in

    const res = ws.connect(url, function(socket) {
      ...
      socket.on('error', function(e) {
          console.log('An unexpected error occured: ', e.error());
      });
      ...
    });