A WebSocket connection.
Obtained from connect (client) or upgrade (server). Provides methods to send and receive messages, send pings, and perform the close handshake.
@eryx/websocket ModuleWebSocket client and server library (RFC 6455).
Client - connect to a WebSocket server:
local websocket = require("@eryx/websocket")
local ws = websocket.connect("ws://localhost:8080/chat")
ws:send("hello")
local msg = ws:receive()
if msg then
print(msg.data)
end
ws:close()
Client - secure WebSocket (wss://):
local ws = websocket.connect("wss://echo.example.com/ws")
Client - with compression and heartbeat:
local ws = websocket.connect("wss://echo.example.com/ws", {
compress = true, -- negotiate permessage-deflate
heartbeat = 30, -- send a ping every 30 seconds
})
Server - upgrade inside an HTTP handler:
local http = require("@eryx/http")
local websocket = require("@eryx/websocket")
local server = http.createServer(function(req, res)
if req.path == "/ws" then
local ws = websocket.upgrade(req, res, { compress = true })
while true do
local msg = ws:receive()
if not msg then break end
ws:send("echo: " .. msg.data)
end
return
end
res:send(200, "Hello!")
end)
server:listen()
The server upgrade works with both HTTP and HTTPS servers - TLS is already handled by the HTTP server before the request arrives, so the upgraded WebSocket transparently inherits the encrypted transport.
A WebSocket connection.
Obtained from connect (client) or upgrade (server). Provides methods to send and receive messages, send pings, and perform the close handshake.
Sends a text or binary message.
If permessage-deflate was negotiated, the message is automatically
compressed before sending.
Sends a Ping control frame.
Optional payload (≤ 125 bytes).
Sends a Pong control frame.
Optional payload (≤ 125 bytes). Typically the same data received in the corresponding Ping.
Blocks until a complete data message arrives, or the connection closes.
Handles fragmented messages, automatically responds to Ping frames
with Pong, and performs the close handshake when a close frame is
received. If permessage-deflate was negotiated, compressed messages
(RSV1 set) are automatically decompressed.
The received message, or nil when the connection has been closed.
Initiates the WebSocket close handshake.
Sends a close frame, waits for the server's close reply (if not already received), then shuts down the underlying socket. Also cancels any active heartbeat.
A message returned by WebSocket.receive.
The payload. For text frames this is a UTF-8 string; for binary frames it contains raw bytes.
true when the message was sent as a binary frame.
Options for connect.
Socket timeout in seconds.
Requested sub-protocols (Sec-WebSocket-Protocol).
When true, negotiates permessage-deflate compression (RFC 7692).
Interval in seconds between automatic ping frames. nil or 0 disables.
Options for upgrade.
The sub-protocol to select. Sent back in Sec-WebSocket-Protocol.
When true, accepts permessage-deflate if the client offered it.
Interval in seconds between automatic ping frames. nil or 0 disables.