<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>websockets on Grafana Labs</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-websockets/</link><description>Recent content in websockets on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/k6/v2.3.x/javascript-api/k6-websockets/index.xml" rel="self" type="application/rss+xml"/><item><title>Blob</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-websockets/blob/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-websockets/blob/</guid><content><![CDATA[&lt;h1 id=&#34;blob&#34;&gt;Blob&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;Blob&lt;/code&gt; is an interface that represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-experimental/streams/readablestream/&#34;&gt;ReadableStream&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s the type of the data received on 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-websockets/websocket/websocket-onmessage/&#34;&gt;WebSocket.onmessage&lt;/a&gt; when &lt;code&gt;WebSocket.binaryType&lt;/code&gt; is set to &lt;code&gt;&amp;quot;blob&amp;quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A &lt;code&gt;Blob&lt;/code&gt; instance has the following methods/properties:&lt;/p&gt;
&lt;!-- vale off --&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Class/Property&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;Blob.size&lt;/td&gt;
              &lt;td&gt;The number of bytes of data contained within the &lt;code&gt;Blob&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Blob.type&lt;/td&gt;
              &lt;td&gt;A string containing the MIME type, or an empty string if the type could not be determined.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Blob.arrayBuffer()&lt;/td&gt;
              &lt;td&gt;Returns a &lt;code&gt;Promise&lt;/code&gt; that resolves with the contents of the blob as binary data contained in an &lt;code&gt;ArrayBuffer&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Blob.bytes()&lt;/td&gt;
              &lt;td&gt;Returns a &lt;code&gt;Promise&lt;/code&gt; that resolves with a &lt;code&gt;Uint8Array&lt;/code&gt; containing the contents of the blob as an array of bytes.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Blob.slice()&lt;/td&gt;
              &lt;td&gt;Returns a new &lt;code&gt;Blob&lt;/code&gt; object which contains data from a subset of the blob on which it&amp;rsquo;s called.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Blob.stream()&lt;/td&gt;
              &lt;td&gt;Returns a 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-experimental/streams/readablestream/&#34;&gt;ReadableStream&lt;/a&gt; which upon reading returns the data contained within the &lt;code&gt;Blob&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Blob.text()&lt;/td&gt;
              &lt;td&gt;Returns a &lt;code&gt;Promise&lt;/code&gt; that resolves with a string containing the contents of the blob, interpreted as &lt;code&gt;UTF-8&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;!-- vale on --&gt;
]]></content><description>&lt;h1 id="blob">Blob&lt;/h1>
&lt;p>&lt;code>Blob&lt;/code> is an interface that represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-experimental/streams/readablestream/">ReadableStream&lt;/a>.&lt;/p></description></item><item><title>WebSocket</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-websockets/websocket/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-websockets/websocket/</guid><content><![CDATA[&lt;h1 id=&#34;websocket&#34;&gt;WebSocket&lt;/h1&gt;
&lt;p&gt;Creates a WebSocket instance for connection to a remote host.&lt;/p&gt;
&lt;p&gt;The following events can close the connection:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;remote host close event.&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-websockets/websocket/websocket-close/&#34;&gt;WebSocket.close()&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;k6 VU interruption based on test configuration or CLI commands.&lt;/li&gt;
&lt;/ul&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Parameter&lt;/th&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;url&lt;/td&gt;
              &lt;td&gt;string&lt;/td&gt;
              &lt;td&gt;The URL to which to connect (e.g. &amp;ldquo;ws://localhost:10000&amp;rdquo;).&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;protocols&lt;/td&gt;
              &lt;td&gt;array&lt;/td&gt;
              &lt;td&gt;Not yet implemented, reserved for the future use.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;params&lt;/td&gt;
              &lt;td&gt;object&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-websockets/params/&#34;&gt;Params&lt;/a&gt; object containing additional request parameters.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;returns&#34;&gt;Returns&lt;/h3&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;WebSocket&lt;/td&gt;
              &lt;td&gt;An instance of WebSocket object.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;ready-state-constants&#34;&gt;Ready-state constants&lt;/h3&gt;
&lt;p&gt;Compare an instance&amp;rsquo;s &lt;code&gt;readyState&lt;/code&gt; with these constants to check the connection state:&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Constant&lt;/th&gt;
              &lt;th&gt;Value&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;WebSocket.CONNECTING&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;The connection is not yet open.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;WebSocket.OPEN&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;The connection is open and ready to communicate.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;WebSocket.CLOSING&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;&lt;code&gt;2&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;The connection is closing.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;WebSocket.CLOSED&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;&lt;code&gt;3&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;The connection is closed or could not be opened.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;The constants are read-only and also available on each instance, for example, &lt;code&gt;ws.OPEN&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;example&#34;&gt;Example&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;A k6 script that initiates a WebSocket connection.&lt;/em&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { WebSocket } from &amp;#39;k6/websockets&amp;#39;;

export default function () {
  const ws = new WebSocket(&amp;#39;wss://quickpizza.grafana.com/ws&amp;#39;);

  ws.onopen = () =&amp;gt; {
    console.log(ws.readyState === WebSocket.OPEN); // true
    console.log(ws.OPEN === WebSocket.OPEN); // true
    ws.close();
  };

  ws.onclose = () =&amp;gt; {
    console.log(ws.readyState === WebSocket.CLOSED); // true
  };
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="websocket">WebSocket&lt;/h1>
&lt;p>Creates a WebSocket instance for connection to a remote host.&lt;/p>
&lt;p>The following events can close the connection:&lt;/p>
&lt;ul>
&lt;li>remote host close event.&lt;/li>
&lt;li>
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-websockets/websocket/websocket-close/">WebSocket.close()&lt;/a>.&lt;/li>
&lt;li>k6 VU interruption based on test configuration or CLI commands.&lt;/li>
&lt;/ul>
&lt;section class="expand-table-wrapper">&lt;div class="button-div">
&lt;button class="expand-table-btn">Expand table&lt;/button>
&lt;/div>&lt;div class="responsive-table-wrapper">
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Parameter&lt;/th>
&lt;th>Type&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>url&lt;/td>
&lt;td>string&lt;/td>
&lt;td>The URL to which to connect (e.g. &amp;ldquo;ws://localhost:10000&amp;rdquo;).&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>protocols&lt;/td>
&lt;td>array&lt;/td>
&lt;td>Not yet implemented, reserved for the future use.&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>params&lt;/td>
&lt;td>object&lt;/td>
&lt;td>
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-websockets/params/">Params&lt;/a> object containing additional request parameters.&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;/section>&lt;h3 id="returns">Returns&lt;/h3>
&lt;section class="expand-table-wrapper">&lt;div class="button-div">
&lt;button class="expand-table-btn">Expand table&lt;/button>
&lt;/div>&lt;div class="responsive-table-wrapper">
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Type&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>WebSocket&lt;/td>
&lt;td>An instance of WebSocket object.&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;/section>&lt;h3 id="ready-state-constants">Ready-state constants&lt;/h3>
&lt;p>Compare an instance&amp;rsquo;s &lt;code>readyState&lt;/code> with these constants to check the connection state:&lt;/p></description></item><item><title>Params</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-websockets/params/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-websockets/params/</guid><content><![CDATA[&lt;h1 id=&#34;params&#34;&gt;Params&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;Params&lt;/code&gt; is an object used by the WebSocket constructor. The &lt;code&gt;Params&lt;/code&gt; object contains request-specific options, such as headers that should be inserted into the connection initialization request.&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;Params.compression&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;string&lt;/td&gt;
              &lt;td&gt;Compression algorithm to be used by the WebSocket connection. The only supported algorithm currently is &lt;code&gt;deflate&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;Params.jar&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-http/cookiejar/&#34;&gt;http.CookieJar&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;The cookie jar that will be used when making the initial HTTP request to establish the WebSocket connection. If empty, the 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-http/cookiejar/&#34;&gt;default VU cookie jar&lt;/a&gt; will be used.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;Params.headers&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;object&lt;/td&gt;
              &lt;td&gt;Custom HTTP headers in key-value pairs that will be added to the initial HTTP request to establish the WebSocket connection. Keys are header names and values are header values.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;Params.tags&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;object&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/using-k6/tags-and-groups/#user-defined-tags&#34;&gt;Custom metric tags&lt;/a&gt; in key-value pairs where the keys are names of tags and the values are tag values. The WebSocket connection will 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-ws/socket/#websocket-built-in-metrics&#34;&gt;generate metrics samples&lt;/a&gt; with these tags attached, allowing users to filter the results data or set 
    &lt;a href=&#34;/docs/k6/v2.3.x/using-k6/thresholds/#thresholds-on-tags&#34;&gt;thresholds on sub-metrics&lt;/a&gt;.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;example-of-custom-metadata-headers-and-tags&#34;&gt;Example of custom metadata headers and tags&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;A k6 script that makes a WebSocket request with a custom header and tags results data with a specific tag&lt;/em&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { WebSocket } from &amp;#39;k6/websockets&amp;#39;;

export default function () {
  const url = &amp;#39;ws://localhost:10000&amp;#39;;
  const params = {
    headers: { &amp;#39;X-MyHeader&amp;#39;: &amp;#39;k6test&amp;#39; },
    tags: { k6test: &amp;#39;yes&amp;#39; },
  };

  const ws = new WebSocket(url, null, params);

  ws.onopen = () =&amp;gt; {
    console.log(&amp;#39;WebSocket connection established!&amp;#39;);
    ws.close();
  };
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The preceding example uses a WebSocket echo server, which you can run with the following command:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="params">Params&lt;/h1>
&lt;p>&lt;code>Params&lt;/code> is an object used by the WebSocket constructor. The &lt;code>Params&lt;/code> object contains request-specific options, such as headers that should be inserted into the connection initialization request.&lt;/p></description></item></channel></rss>