k6
k6 copied to clipboard
k6-websocket request with multiple messages
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