Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k6-websocket request with multiple messages #3794

Open
Amrin-Taj opened this issue Jun 12, 2024 · 0 comments
Open

k6-websocket request with multiple messages #3794

Amrin-Taj opened this issue Jun 12, 2024 · 0 comments
Assignees

Comments

@Amrin-Taj
Copy link

Brief summary

I am new to k6, trying to add the websocket api with multiple messages and validating the responses but no matter how i try, k6 is repeating the first msg response for everything.

k6 version

latest

OS

windows

Docker version and image (if applicable)

No response

Steps to reproduce the problem

import { check } from 'k6';
import ws from 'k6/ws';
import { sleep } from 'k6';

export default function () {
const url = 'ws://localhost:8080';
const messages = ['Hello, WebSocket!', 'Another message', 'And another one'];
let currentMessageIndex = 0;

let res = ws.connect(url, {}, function (socket) {
socket.on('open', function () {
console.log('connected');
// Send the first message
socket.send(messages[currentMessageIndex]);
});

socket.on('message', function (message) {
  console.log(`Received message: ${message}`);
  
  // Validate the response
  check(message, { [`is message ${currentMessageIndex + 1} correct`]: (msg) => msg === messages[currentMessageIndex] });

  currentMessageIndex++;

  // Send the next message if there are more to send
  if (currentMessageIndex < messages.length) {
    socket.send(messages[currentMessageIndex]);
  } else {
    socket.close();
  }
});

});

check(res, { 'status is 101': (r) => r && r.status === 101 });
}

in above all the messages response is Hello, WebSocket. can someone please tell me if i am doing it right

Expected behaviour

what multiple messages

Actual behaviour

only first msg is compared

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants