No results for

Powered byAlgolia
⚠️ This documentation is outdated. Please visit for the latest k6 documentation.📚

Socket.setInterval(callback, interval)

A module with a better and standard API exists

The new k6/experimental/websockets API partially implements the WebSockets API living standard.

When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance.

Call a function repeatedly, while the WebSocket connection is open.

callbackfunctionThe function to call every interval milliseconds.
intervalnumberThe number of milliseconds between two calls to callback.


import ws from 'k6/ws';
import { check } from 'k6';
export default function () {
const url = 'ws://';
const params = { tags: { my_tag: 'hello' } };
const res = ws.connect(url, params, function (socket) {
socket.on('open', function open() {
socket.setInterval(function timeout() {;
console.log('Pinging every 1sec (setInterval test)');
}, 1000);
socket.on('pong', function () {
check(res, { 'status is 101': (r) => r && r.status === 101 });