Skip to content
Welcome to the Gluegun documentation!

gluegun/websocket

WebSocket helpers for Gun connections.

Gun supports WebSocket over HTTP/1.1 only. WebSocket over HTTP/2 (RFC 8441) is not supported by Gun. Call upgrade_with_protocol with the protocol returned by connection.await_up to reject HTTP/2 before calling Gun.

Once an HTTP/1.1 connection is upgraded to WebSocket the underlying TCP connection is exclusively used for WebSocket frames. You cannot send concurrent HTTP requests on that same connection after upgrading.

import gluegun/connection
import gluegun/websocket
import gluegun/message
let assert Ok(conn) =
connection.options()
|> connection.open(host: "echo.example.com", port: 80)
let assert Ok(protocol) = connection.await_up(conn, connection.Milliseconds(5000))
let assert Ok(stream) = websocket.upgrade_with_protocol(conn, protocol, "/ws", [])
let assert Ok(Nil) = websocket.await_upgrade(conn, stream, connection.Milliseconds(5000))
let assert Ok(Nil) = websocket.send(conn, stream, message.Text("hello"))
let assert Ok(message.Text(reply)) = websocket.receive(conn, stream, connection.Milliseconds(5000))

High-level options for opening and upgrading a WebSocket connection.

A reusable WebSocket handle.

Wraps the upgraded Gun connection, WebSocket stream, and receive timeout so higher-level helpers can send and receive frames without repeating them.

Typed options for Gun WebSocket upgrades.

Wait for the WebSocket upgrade confirmation (101 Switching Protocols).

Call this after upgrade/3. Returns Ok(Nil) when the server confirms the WebSocket handshake. Returns an error on timeout, connection failure, or if a non-upgrade message arrives first.

pub fn await_upgrade(gluegun/internal.Connection, gluegun/internal.Stream, gluegun/connection.Timeout) -> Result(Nil, gluegun/error.GluegunError)

Send a close WebSocket frame using a reusable socket.

pub fn close(gluegun/websocket.Socket) -> Result(Nil, gluegun/error.GluegunError)

Open a connection, perform a WebSocket upgrade, and return a reusable socket.

pub fn connect(host: String, port: Int, path: String, options: gluegun/websocket.Options) -> Result(gluegun/websocket.Socket, gluegun/error.GluegunError)

Construct default high-level WebSocket connection options.

pub fn options() -> gluegun/websocket.Options

Send a ping WebSocket frame using a reusable socket.

pub fn ping(gluegun/websocket.Socket, BitArray) -> Result(Nil, gluegun/error.GluegunError)

Send a pong WebSocket frame using a reusable socket.

pub fn pong(gluegun/websocket.Socket, BitArray) -> Result(Nil, gluegun/error.GluegunError)

Receive the next WebSocket frame from the stream.

Returns Ok(frame) when a WebSocket frame arrives. Returns Error(InvalidMessage(...)) if a non-WebSocket message arrives (e.g. an HTTP response or upgrade acknowledgement that arrived out of order). Returns Error(Timeout) or stream errors on failures.

If the upgrade acknowledgement has not yet been received, call await_upgrade/3 before calling receive.

pub fn receive(gluegun/internal.Connection, gluegun/internal.Stream, gluegun/connection.Timeout) -> Result(gluegun/message.Frame, gluegun/error.GluegunError)

Receive the next application frame, handling ping/pong control frames.

Incoming pings are answered with a pong carrying the same payload. Incoming pongs are skipped. Text, binary, close, and close-with-reason frames are returned to the caller.

pub fn receive_app_frame(gluegun/websocket.Socket) -> Result(gluegun/message.Frame, gluegun/error.GluegunError)

Receive the next WebSocket frame using a reusable socket.

pub fn receive_frame(gluegun/websocket.Socket) -> Result(gluegun/message.Frame, gluegun/error.GluegunError)

Send a single WebSocket frame on the stream.

Supported frame types: Text, Binary, Ping, Pong, Close, CloseWithReason. The frame is forwarded directly to Gun's ws_send.

pub fn send(gluegun/internal.Connection, gluegun/internal.Stream, gluegun/message.Frame) -> Result(Nil, gluegun/error.GluegunError)

Send a binary WebSocket frame using a reusable socket.

pub fn send_binary(gluegun/websocket.Socket, BitArray) -> Result(Nil, gluegun/error.GluegunError)

Send a single WebSocket frame using a reusable socket.

pub fn send_frame(gluegun/websocket.Socket, gluegun/message.Frame) -> Result(Nil, gluegun/error.GluegunError)

Send one or more WebSocket frames on the stream.

Gun accepts either a single frame or a list of frames. send delegates to this function with a one-element list.

pub fn send_many(gluegun/internal.Connection, gluegun/internal.Stream, List(gluegun/message.Frame)) -> Result(Nil, gluegun/error.GluegunError)

Send a text WebSocket frame using a reusable socket.

pub fn send_text(gluegun/websocket.Socket, String) -> Result(Nil, gluegun/error.GluegunError)

Initiate a WebSocket upgrade on an assumed HTTP/1.1 connection.

Prefer upgrade_with_protocol after connection.await_up when the connection may negotiate HTTP/2. This function keeps the original HTTP/1.1 default path for callers that constrain the connection to HTTP/1.1.

pub fn upgrade(gluegun/internal.Connection, String, List(#(String, String))) -> Result(gluegun/internal.Stream, gluegun/error.GluegunError)

Construct default WebSocket upgrade options.

pub fn upgrade_options() -> gluegun/websocket.UpgradeOptions

Initiate a WebSocket upgrade on an assumed HTTP/1.1 connection with options.

pub fn upgrade_with_options(gluegun/internal.Connection, String, List(#(String, String)), gluegun/websocket.UpgradeOptions) -> Result(gluegun/internal.Stream, gluegun/error.GluegunError)

Initiate a WebSocket upgrade when the negotiated protocol is known.

Sends the WebSocket upgrade request to the server and returns the stream reference. Call await_upgrade next to confirm the handshake completed.

Returns InvalidMessage for HTTP/2 because Gun does not support WebSocket over HTTP/2. Use this after connection.await_up when protocol negotiation may choose HTTP/2.

pub fn upgrade_with_protocol(gluegun/internal.Connection, gluegun/connection.Protocol, String, List(#(String, String))) -> Result(gluegun/internal.Stream, gluegun/error.GluegunError)

Initiate a WebSocket upgrade with options when the negotiated protocol is known.

Returns InvalidMessage for HTTP/2 because Gun does not support WebSocket over HTTP/2.

pub fn upgrade_with_protocol_and_options(gluegun/internal.Connection, gluegun/connection.Protocol, String, List(#(String, String)), gluegun/websocket.UpgradeOptions) -> Result(gluegun/internal.Stream, gluegun/error.GluegunError)

Set Gun's WebSocket closing timeout.

pub fn with_closing_timeout(gluegun/websocket.UpgradeOptions, gluegun/connection.Timeout) -> gluegun/websocket.UpgradeOptions

Enable or disable WebSocket compression.

pub fn with_compress(gluegun/websocket.UpgradeOptions, Bool) -> gluegun/websocket.UpgradeOptions

Set Gun connection options used when opening the connection.

pub fn with_connect_options(gluegun/websocket.Options, gluegun/connection.ConnectOptions) -> gluegun/websocket.Options

Set the default WebSocket protocol callback module.

pub fn with_default_protocol_module(gluegun/websocket.UpgradeOptions, String) -> gluegun/websocket.UpgradeOptions

Set the initial WebSocket flow-control allowance.

pub fn with_flow(gluegun/websocket.UpgradeOptions, Int) -> gluegun/websocket.UpgradeOptions

Set headers sent with the WebSocket upgrade request.

pub fn with_headers(gluegun/websocket.Options, List(#(String, String))) -> gluegun/websocket.Options

Set Gun's WebSocket keepalive timeout.

pub fn with_keepalive(gluegun/websocket.UpgradeOptions, gluegun/connection.Timeout) -> gluegun/websocket.UpgradeOptions

Add a WebSocket subprotocol callback module.

pub fn with_protocol_module(gluegun/websocket.UpgradeOptions, String, String) -> gluegun/websocket.UpgradeOptions

Set Gun's raw reply_to option.

pub fn with_reply_to_dynamic(gluegun/websocket.UpgradeOptions, gleam/dynamic.Dynamic) -> gluegun/websocket.UpgradeOptions

Enable or disable silencing automatic ping frames.

pub fn with_silence_pings(gluegun/websocket.UpgradeOptions, Bool) -> gluegun/websocket.UpgradeOptions

Open a WebSocket, run a callback, then close the WebSocket and connection.

pub fn with_socket(host: String, port: Int, path: String, options: gluegun/websocket.Options, callback: fn(gluegun/websocket.Socket) -> Result(a, gluegun/error.GluegunError)) -> Result(a, gluegun/error.GluegunError)

Set the timeout used when awaiting connection readiness, upgrade, and frames.

pub fn with_timeout(gluegun/websocket.Options, gluegun/connection.Timeout) -> gluegun/websocket.Options

Set Gun's raw tunnel option.

pub fn with_tunnel_dynamic(gluegun/websocket.UpgradeOptions, gleam/dynamic.Dynamic) -> gluegun/websocket.UpgradeOptions

Set Gun WebSocket upgrade options used for the upgrade request.

pub fn with_upgrade_options(gluegun/websocket.Options, gluegun/websocket.UpgradeOptions) -> gluegun/websocket.Options

Set Gun's raw user_opts option.

pub fn with_user_opts_dynamic(gluegun/websocket.UpgradeOptions, gleam/dynamic.Dynamic) -> gluegun/websocket.UpgradeOptions