<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>k6/ws on Grafana Labs</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-ws/</link><description>Recent content in k6/ws on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/k6/v2.3.x/javascript-api/k6-ws/index.xml" rel="self" type="application/rss+xml"/><item><title>connect( url, params, callback )</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-ws/connect/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-ws/connect/</guid><content><![CDATA[&lt;h1 id=&#34;connect-url-params-callback-&#34;&gt;connect( url, params, callback )&lt;/h1&gt;


&lt;div data-shared=&#34;ws-module.md&#34;&gt;
            

&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;A module with a better and standard API exists.
&lt;br&gt;
&lt;br&gt;
The new 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-websockets/&#34;&gt;k6/websockets API&lt;/a&gt; partially implements the &lt;a href=&#34;https://websockets.spec.whatwg.org/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;WebSockets API living standard&lt;/a&gt;.
&lt;br&gt;
&lt;br&gt;
When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;/div&gt;

        
&lt;p&gt;Initiate a WebSocket connection to a remote host.&lt;/p&gt;
&lt;p&gt;Calling connect will block the VU finalization until the WebSocket connection is closed. Instead of continuously looping the main function (&lt;code&gt;export default function () { ... }&lt;/code&gt;) over an over, each VU will be halted listening to async events and executing their event handlers until the connection is closed.&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-ws/socket/socket-close/&#34;&gt;Socket.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;Request URL (e.g. &amp;ldquo;wss://echo.websocket.org&amp;rdquo;).&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-ws/params/&#34;&gt;Params&lt;/a&gt; object containing additional request parameters.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;callback&lt;/td&gt;
              &lt;td&gt;function&lt;/td&gt;
              &lt;td&gt;The callback function that will be called when the WebSocket connection is initiated. A 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-ws/socket/&#34;&gt;Socket&lt;/a&gt; object will be passed to the function, and this object can be used to set up callbacks etc when things happen on the WebSocket connection&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;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-http/response/&#34;&gt;Response&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;HTTP Response object.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;example&#34;&gt;Example&lt;/h3&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 ws from &amp;#39;k6/ws&amp;#39;;

export default function () {
  const url = &amp;#39;wss://echo.websocket.org&amp;#39;;
  const resp = ws.connect(url, null, function (socket) {
    socket.on(&amp;#39;open&amp;#39;, function () {
      console.log(&amp;#39;WebSocket connection established!&amp;#39;);
      socket.close();
    });
  });
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="connect-url-params-callback-">connect( url, params, callback )&lt;/h1>
&lt;div data-shared="ws-module.md">
&lt;div class="admonition admonition-note">&lt;blockquote>&lt;p class="title text-uppercase">Note&lt;/p>&lt;p>A module with a better and standard API exists.
&lt;br>
&lt;br>
The new
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-websockets/">k6/websockets API&lt;/a> partially implements the &lt;a href="https://websockets.spec.whatwg.org/" target="_blank" rel="noopener noreferrer">WebSockets API living standard&lt;/a>.
&lt;br>
&lt;br>
When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance.&lt;/p></description></item><item><title>Params</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-ws/params/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-ws/params/</guid><content><![CDATA[&lt;h1 id=&#34;params&#34;&gt;Params&lt;/h1&gt;


&lt;div data-shared=&#34;ws-module.md&#34;&gt;
            

&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;A module with a better and standard API exists.
&lt;br&gt;
&lt;br&gt;
The new 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-websockets/&#34;&gt;k6/websockets API&lt;/a&gt; partially implements the &lt;a href=&#34;https://websockets.spec.whatwg.org/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;WebSockets API living standard&lt;/a&gt;.
&lt;br&gt;
&lt;br&gt;
When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;/div&gt;

        
&lt;p&gt;&lt;em&gt;Params&lt;/em&gt; is an object used by the WebSocket methods that generate WebSocket requests. &lt;em&gt;Params&lt;/em&gt; contains request-specific options like headers that should be inserted into the 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;. If the option is left unset or empty, it defaults to no compression.&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 will make a WebSocket request with a custom header and tag 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 ws from &amp;#39;k6/ws&amp;#39;;

export default function () {
  const url = &amp;#39;wss://echo.websocket.org&amp;#39;;
  const params = {
    headers: { &amp;#39;X-MyHeader&amp;#39;: &amp;#39;k6test&amp;#39; },
    tags: { k6test: &amp;#39;yes&amp;#39; },
  };
  const res = ws.connect(url, params, function (socket) {
    socket.on(&amp;#39;open&amp;#39;, function () {
      console.log(&amp;#39;WebSocket connection established!&amp;#39;);
      socket.close();
    });
  });
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="params">Params&lt;/h1>
&lt;div data-shared="ws-module.md">
&lt;div class="admonition admonition-note">&lt;blockquote>&lt;p class="title text-uppercase">Note&lt;/p>&lt;p>A module with a better and standard API exists.
&lt;br>
&lt;br>
The new
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-websockets/">k6/websockets API&lt;/a> partially implements the &lt;a href="https://websockets.spec.whatwg.org/" target="_blank" rel="noopener noreferrer">WebSockets API living standard&lt;/a>.
&lt;br>
&lt;br>
When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance.&lt;/p></description></item><item><title>Socket</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-ws/socket/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-ws/socket/</guid><content><![CDATA[&lt;h1 id=&#34;socket&#34;&gt;Socket&lt;/h1&gt;


&lt;div data-shared=&#34;ws-module.md&#34;&gt;
            

&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;A module with a better and standard API exists.
&lt;br&gt;
&lt;br&gt;
The new 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-websockets/&#34;&gt;k6/websockets API&lt;/a&gt; partially implements the &lt;a href=&#34;https://websockets.spec.whatwg.org/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;WebSockets API living standard&lt;/a&gt;.
&lt;br&gt;
&lt;br&gt;
When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;/div&gt;

        
&lt;p&gt;&lt;code&gt;Socket&lt;/code&gt; is a WebSocket client to interact with a WebSocket connection. You can use it to listen various events happening on the WebSocket connection and send messages to the server. Additionally, you can use socket.setTimeout() and socket.setInterval() to execute code in the background, or repeatedly, while the WebSocket connection is open.&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;Method&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;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-ws/socket/socket-close/&#34;&gt;Socket.close()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Close the WebSocket connection.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-ws/socket/socket-on/&#34;&gt;Socket.on(event, callback)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Set up an event listener on the connection for any of the following events:&lt;br /&gt;- open&lt;br /&gt;- binaryMessage&lt;br /&gt;- message&lt;br /&gt;- ping&lt;br /&gt;- pong&lt;br /&gt;- close&lt;br /&gt;- error.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-ws/socket/socket-ping/&#34;&gt;Socket.ping()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Send a ping.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-ws/socket/socket-send/&#34;&gt;Socket.send(data)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Send string data.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-ws/socket/socket-sendbinary/&#34;&gt;Socket.sendBinary(data)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Send binary data.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-ws/socket/socket-setinterval/&#34;&gt;Socket.setInterval(callback, interval)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Call a function repeatedly at certain intervals, while the connection is open.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-ws/socket/socket-settimeout/&#34;&gt;Socket.setTimeout(callback, period)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Call a function with a delay, if the connection is open.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;websocket-built-in-metrics&#34;&gt;WebSocket built-in metrics&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;k6&lt;/code&gt; automatically collects metrics when interacting with a WebSocket service through the &lt;code&gt;k6/ws&lt;/code&gt; API.
Review the list in the 
    &lt;a href=&#34;/docs/k6/v2.3.x/using-k6/metrics/reference/#websockets&#34;&gt;metrics reference&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Check out the 
    &lt;a href=&#34;/docs/k6/v2.3.x/get-started/results-output/&#34;&gt;Results output article&lt;/a&gt; for more information about how to process the metric information.&lt;/p&gt;
&lt;h3 id=&#34;example&#34;&gt;Example&lt;/h3&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 ws from &amp;#39;k6/ws&amp;#39;;
import { check } from &amp;#39;k6&amp;#39;;

export default function () {
  const url = &amp;#39;wss://echo.websocket.org&amp;#39;;
  const params = { tags: { my_tag: &amp;#39;hello&amp;#39; } };

  const response = ws.connect(url, params, function (socket) {
    socket.on(&amp;#39;open&amp;#39;, function open() {
      console.log(&amp;#39;connected&amp;#39;);
      socket.send(Date.now());

      socket.setInterval(function timeout() {
        socket.ping();
        console.log(&amp;#39;Pinging every 1sec (setInterval test)&amp;#39;);
      }, 1000);
    });

    socket.on(&amp;#39;ping&amp;#39;, function () {
      console.log(&amp;#39;PING!&amp;#39;);
    });

    socket.on(&amp;#39;pong&amp;#39;, function () {
      console.log(&amp;#39;PONG!&amp;#39;);
    });

    socket.on(&amp;#39;pong&amp;#39;, function () {
      // Multiple event handlers on the same event
      console.log(&amp;#39;OTHER PONG!&amp;#39;);
    });

    socket.on(&amp;#39;message&amp;#39;, function (message) {
      console.log(`Received message: ${message}`);
    });

    socket.on(&amp;#39;close&amp;#39;, function () {
      console.log(&amp;#39;disconnected&amp;#39;);
    });

    socket.on(&amp;#39;error&amp;#39;, function (e) {
      if (e.error() != &amp;#39;websocket: close sent&amp;#39;) {
        console.log(&amp;#39;An unexpected error occured: &amp;#39;, e.error());
      }
    });

    socket.setTimeout(function () {
      console.log(&amp;#39;2 seconds passed, closing the socket&amp;#39;);
      socket.close();
    }, 2000);
  });

  check(response, { &amp;#39;status is 101&amp;#39;: (r) =&amp;gt; r &amp;amp;&amp;amp; r.status === 101 });
}
//VU execution won&amp;#39;t be completely finished until the connection is closed.&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="socket">Socket&lt;/h1>
&lt;div data-shared="ws-module.md">
&lt;div class="admonition admonition-note">&lt;blockquote>&lt;p class="title text-uppercase">Note&lt;/p>&lt;p>A module with a better and standard API exists.
&lt;br>
&lt;br>
The new
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-websockets/">k6/websockets API&lt;/a> partially implements the &lt;a href="https://websockets.spec.whatwg.org/" target="_blank" rel="noopener noreferrer">WebSockets API living standard&lt;/a>.
&lt;br>
&lt;br>
When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance.&lt;/p></description></item></channel></rss>