OffBroadwayWebSocket.Client (off_broadway_websocket v1.2.2)

Copy Markdown View Source

Provides functions to establish and configure WebSocket connections using the gun library, with customizable timeouts and connection options.

Summary

Functions

connect(url, path, gun_opts, await_timeout, headers \\ [])

@spec connect(
  url :: String.t(),
  path :: String.t(),
  gun_opts :: map(),
  await_timeout :: non_neg_integer(),
  headers :: [{String.t(), String.t()}]
) :: {:ok, %{conn_pid: pid(), stream_ref: reference()}} | {:error, term()}

Establishes a WebSocket connection using :gun.

The gun_opts map is forwarded directly to :gun.open/3 so TLS, HTTP and WebSocket options can be configured as required. The call waits up to await_timeout milliseconds for :gun.await_up/2 to succeed before returning an error. Additional upgrade headers may be supplied via headers.

Parameters

  • url: The base URL for the WebSocket connection (e.g., "wss://example.com").
  • path: The WebSocket path to upgrade to (e.g., "/ws").
  • gun_opts: Options passed to :gun.open/3.
  • await_timeout: How long to wait for the connection to become ready.
  • headers: Optional headers for the WebSocket upgrade.

Returns

  • } on a successful connection and upgrade.
  • if the connection or upgrade fails.

connect_once(state)

@spec connect_once(OffBroadwayWebSocket.State.t()) :: {:ok, map()} | {:error, term()}