Socket.IO client for k6, implemented as an xk6 extension. Use it to drive Socket.IO WebSocket traffic from k6 scripts, including custom events, connect/disconnect hooks, and basic auth/query configuration.
- Socket.IO over Engine.IO v4 WebSocket transport
- Simple
io()API that mirrors common Socket.IO usage - Event handlers for
connect,disconnect, and custom events emit()andsend()helpers- Pass-through options for
k6/ws.connect
- Go 1.24+
- k6 and xk6
Build a custom k6 binary with this extension:
xk6 build --with github.com/xemax32/xk6-socket.io@latestIf you are working locally in this repo:
xk6 build --with github.com/xemax32/xk6-socket.io=.Start a Socket.IO test server (a simple one is provided):
node test/sio-test/server.jsRun a k6 script using the custom binary:
./k6 run script.jsExample script:
import { io } from "k6/x/socketio";
import { sleep } from "k6";
export default function () {
const options = {
path: "/socket.io/",
namespace: "/",
auth: { token: "demo-token" },
query: { env: "local", user: "vu-1" },
params: {
headers: { "x-client": "k6" },
tags: { scenario: "socketio" },
},
};
io("http://localhost:4000", options, (socket) => {
socket.on("connect", () => {
console.log("connected");
socket.emit("hello", { payload: "hi from k6" });
socket.send({ type: "data", ts: Date.now() });
});
socket.on("message", (msg) => {
console.log("message", msg);
});
socket.on("disconnect", () => {
console.log("disconnected");
});
});
}Connects to a Socket.IO server and returns the underlying k6/ws.connect result.
host(string): Base URL such ashttp://localhost:4000orwss://example.com.options(object, optional): Configuration described below.handler(function, optional): Called with a Socket.IO-likesocketwrapper.
Inside the handler you can use:
socket.on(event, handler)socket.emit(event, data)socket.send(data)(alias foremit("message", data))- All other
k6/wssocket methods are available as-is via the wrapper (it preserves the original WebSocket prototype).
Supported built-in events:
connectdisconnect
Custom events are dispatched from socket.on("your_event", handler) when messages arrive.
All options are optional.
| Option | Type | Default | Description |
|---|---|---|---|
path |
string | /socket.io/ |
Socket.IO path. |
namespace |
string | / |
Namespace to connect to (e.g. /chat). |
auth |
object | null |
Auth payload sent in the connect packet. |
query |
object | {} |
Query parameters appended to the URL. |
params |
object | {} |
Passed directly to k6/ws.connect (e.g. headers, tags). |
- WebSocket transport only (no HTTP long-polling).
- Engine.IO protocol v4.
- No reconnection logic.
- ACKs and binary payloads are not implemented.
- Tests
- Namespaces support (including dynamic namespace handling).
- Connection state recovery / session ID handling.
- Authentication middleware / auth payload support.
- Client/server-side connect_error and error packet handling.
- ACKs support.
- Room support
- Binary attachments framing.
See LICENSE.