← All Modules

ws

WebSocket client. No require() needed.

Connect options

opts is a table; every field is optional:

-- Backward compatible: no opts.
local conn = ws.connect("ws://example.com/socket")
ws.send(conn, "hello")
print(ws.recv(conn))
ws.close(conn)

-- Subprotocols + bearer auth + skip TLS verification.
local conn = ws.connect("wss://example.com/api/exec", {
  subprotocols = { "v4.channel.k8s.io" },
  headers = { Authorization = "Bearer " .. token },
  insecure = true,
})
print(ws.protocol(conn))  -- "v4.channel.k8s.io"

-- Binary frames round-trip as raw bytes (channel-prefixed protocols, etc.).
ws.send_binary(conn, "\0raw\255bytes")
local frame = ws.recv(conn)

Gating

ws.connect is a gated (mutating) builtin: read-only mode replaces it with a blocking stub and approval mode suspends it for confirmation. Anything built on top of it — including k8s.pods:exec — inherits that gating. ws.send, ws.send_binary, ws.recv, ws.protocol and ws.close operate on an already-established connection and are not separately gated.