WebSockets API
Module and Core Types
HTTP.WebSockets — Module
HTTP.WebSocketsWebSocket client and server helpers layered on top of the HTTP request/response stack and Reseau transports.
Use open for client connections and listen!, listen, or serve! for servers. Most applications will work with WebSocket values directly rather than the lower-level Conn codec state.
HTTP.WebSockets.Conn — Type
ConnLow-level WebSocket codec connection state.
This is primarily useful for advanced integrations that need direct access to frame-level state. Most client and server code should work with WebSocket instead.
HTTP.WebSockets.CloseFrameBody — Type
CloseFrameBody(code, reason="")Structured close payload carrying the WebSocket close status code and optional UTF-8 reason text.
HTTP.WebSockets.WebSocketError — Type
WebSocketErrorException raised when a WebSocket closes or encounters a protocol/payload error.
Inspect err.message.code and err.message.reason to distinguish normal closures from errors.
Client Operations
HTTP.WebSockets.open — Function
open(url; kwargs...) -> WebSocket
open(f, url; kwargs...) -> AnyOpen a client WebSocket connection to url.
Keyword arguments cover handshake headers, redirect behavior, cookies, proxy selection, TLS verification, timeout controls, and frame limits. request_timeout applies an overall handshake deadline, while response_header_timeout and write_idle_timeout configure HTTP handshake phases. read_idle_timeout bounds both handshake response-header reads and post-upgrade inbound WebSocket inactivity. When called with a function, the socket is closed automatically with status code 1000 when f returns.
HTTP.WebSockets.send — Function
send(ws, message) -> IntSend one text or binary message on ws.
message may be an AbstractString, an AbstractVector{UInt8}, or an iterable of chunks. Iterable inputs are sent as one fragmented message and the returned value is the total payload bytes sent.
HTTP.WebSockets.receive — Function
receive(ws) -> Union{String, Vector{UInt8}}Receive the next complete message from ws.
Text messages are returned as String; binary messages are returned as Vector{UInt8}. Throws WebSocketError once the connection has closed.
HTTP.WebSockets.ping — Function
ping(ws, data=UInt8[]) -> nothingSend a WebSocket ping control frame with optional payload bytes.
HTTP.WebSockets.pong — Function
pong(ws, data=UInt8[]) -> nothingSend a WebSocket pong control frame with optional payload bytes.
Server Operations
HTTP.WebSockets.Server — Type
Server(; network="tcp", address="127.0.0.1:0", handler, tls_config=nothing, ...)WebSocket server handle returned by listen! and serve!.
Hold onto the returned value so you can inspect its bound address with server_addr, wait on it with wait(server), or stop it with forceclose.
HTTP.WebSockets.listen! — Function
listen!(handler, host="127.0.0.1", port=8080; kwargs...) -> ServerStart a background WebSocket server and return its Server handle.
Pass tls_config to serve wss:// traffic, subprotocols to advertise supported subprotocols, and check_origin to customize origin validation. Pass listenany=true to ignore port and bind to an OS-assigned ephemeral port; read the actual address afterwards with server_addr.
HTTP.WebSockets.listen — Function
listen(handler, host="127.0.0.1", port=8080; kwargs...) -> ServerStart a WebSocket server and block until it exits.
HTTP.WebSockets.serve! — Function
serve! is an alias for listen!.
HTTP.WebSockets.upgrade — Function
upgrade(f, stream::HTTP.Stream; kwargs...)Upgrade an in-flight HTTP/1.1 server stream to a WebSocket connection and run f(ws::WebSocket). This is the manual counterpart to listen!: it lets a single HTTP.listen! / HTTP.Router server mix ordinary HTTP routes with WebSocket routes by upgrading the connection from inside a stream handler.
Guard the call with isupgrade and call upgrade before writing any response to the stream:
HTTP.listen!("127.0.0.1", 8080) do stream
if HTTP.WebSockets.isupgrade(stream.message)
HTTP.WebSockets.upgrade(stream) do ws
for msg in ws
HTTP.WebSockets.send(ws, msg)
end
end
else
HTTP.setstatus(stream, 200)
HTTP.startwrite(stream)
write(stream, "ok")
end
endf runs synchronously; the connection is closed when it returns. Keyword arguments mirror listen!: subprotocols, check_origin, maxframesize, and maxfragmentation. WebSocket upgrades over HTTP/2 are not supported.
HTTP.WebSockets.isupgrade — Function
isupgrade(message) -> BoolReturn true when message is a WebSocket upgrade. For a request this checks for a valid client upgrade handshake — use it to guard upgrade inside an HTTP.listen! stream handler. For a response it checks for a 101 Switching Protocols handshake response.
HTTP.WebSockets.server_addr — Function
server_addr(server) -> StringReturn the host:port address currently bound by server.
HTTP.WebSockets.forceclose — Function
forceclose(server) -> nothingImmediately stop accepting new WebSocket connections and close active sessions.