ChannelClient.Socket (channel_client v0.1.0)

Copy Markdown

A GenServer holding the WebSocket connection to a Phoenix endpoint.

The socket owns the wire protocol: it assigns refs, tracks each joined topic's join_ref (as required by Phoenix Channels protocol v2), serializes outbound frames and routes decoded inbound frames to the matching ChannelClient.Channel process.

Lifecycle

The socket connects asynchronously after start_link/2 and, unless reconnect?: false is given, keeps reconnecting forever when the connection drops. Channels are unregistered on disconnect and their owners receive a %ChannelClient.Message{} with event "phx_error" (or "phx_close" for clean closes); callers are expected to re-join.

Options

  • :url - (required) WebSocket URL, ws:// or wss://.
  • :format - wire format: :json (default), :etf, :toon, :btoon, a module implementing the ChannelClient.Format behaviour, or {module, opts}. See ChannelClient.Format.
  • :json_library - JSON module implementing encode!/1 and decode!/1. Defaults to Jason. :serializer is accepted as an alias. Only used by the JSON format.
  • :vsn - Phoenix Channels protocol version, "1.0.0" or "2.0.0". Defaults to "2.0.0". Only used by the JSON format.
  • :transport - module implementing the ChannelClient.Transport behaviour. Defaults to ChannelClient.Transports.Websocket.
  • :transport_opts - options passed through to the transport.
  • :headers - extra headers for the WebSocket upgrade request.
  • :params - map of query params merged into the connect URL.
  • :heartbeat_interval - keepalive ping interval in ms (default 30_000).
  • :reconnect_interval - ms between reconnect attempts (default 60_000).
  • :reconnect? - whether to automatically reconnect (default true).
  • :inbound_plugs - plug specs run on every decoded server frame before routing; halting a frame drops it. See ChannelClient.Plug.
  • :outbound_plugs - plug specs run on every outgoing frame before encoding; halting returns {:error, {:halted, reason}} to sync callers and drops async pushes. See ChannelClient.Plug.

Summary

Functions

Returns a specification to start this module under a supervisor.

Returns whether the underlying transport is currently connected.

Stops the socket process.

Types

t()

@type t() :: %ChannelClient.Socket{
  channels: term(),
  flush_pending: term(),
  format: term(),
  format_opts: term(),
  inbound_plugs: term(),
  outbound_plugs: term(),
  reconnect: term(),
  reconnect_interval: term(),
  reconnect_timer: term(),
  ref: term(),
  status: term(),
  to_send: term(),
  transport: term(),
  transport_opts: term(),
  transport_pid: term(),
  url: term()
}

Functions

child_spec(init_arg)

@spec child_spec(keyword() | {keyword(), keyword()}) :: Supervisor.child_spec()

Returns a specification to start this module under a supervisor.

See Supervisor.

connected?(pid_or_name)

@spec connected?(pid() | atom()) :: boolean()

Returns whether the underlying transport is currently connected.

Returns false for dead or missing sockets instead of crashing.

start_link(opts, genserver_opts \\ [])

@spec start_link(keyword(), keyword()) :: {:ok, pid()} | {:error, any()}

stop(pid)

@spec stop(pid() | atom()) :: :ok

Stops the socket process.