Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/latest/javascript-api/k6-ws/socket/socket-setinterval.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/k6-ws/socket/socket-setinterval/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
Socket.setInterval(callback, interval)
Note
A module with a better and standard API exists.
The new k6/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.
| Parameter | Type | Description |
|---|---|---|
| callback | function | The function to call every interval milliseconds. |
| interval | number | The number of milliseconds between two calls to callback. |
Example
import ws from 'k6/ws';
import { check } from 'k6';
export default function () {
const url = 'wss://echo.websocket.org';
const params = { tags: { my_tag: 'hello' } };
const res = ws.connect(url, params, function (socket) {
socket.on('open', function open() {
console.log('connected');
socket.setInterval(function timeout() {
socket.ping();
console.log('Pinging every 1sec (setInterval test)');
}, 1000);
});
socket.on('pong', function () {
console.log('PONG!');
});
});
check(res, { 'status is 101': (r) => r && r.status === 101 });
}Was this page helpful?
Related resources from Grafana Labs

