Phoenix.LiveView.Socket (Phoenix LiveView v1.2.13)

Copy Markdown View Source

The LiveView socket for Phoenix Endpoints.

This is typically mounted directly in your endpoint.

socket "/live", Phoenix.LiveView.Socket,
  websocket: [connect_info: [session: @session_options]]

To share an underlying transport connection between regular Phoenix channels and LiveView processes, use Phoenix.LiveView.Socket from your own MyAppWeb.UserSocket module.

Next, declare your channel definitions and optional connect/3, and id/1 callbacks to handle your channel specific needs, then mount your own socket in your endpoint:

socket "/live", MyAppWeb.UserSocket,
  websocket: [connect_info: [session: @session_options]]

If you require session options to be set at runtime, you can use an MFA tuple. The function it designates must return a keyword list.

socket "/live", MyAppWeb.UserSocket,
  websocket: [connect_info: [session: {__MODULE__, :runtime_opts, []}]]

# ...

def runtime_opts() do
  Keyword.put(@session_options, :domain, host())
end

Socket options

Options given to use Phoenix.LiveView.Socket are forwarded to use Phoenix.Socket. See the "Using options" section of Phoenix.Socket for the supported options. For example, each file upload entry joins its own channel when using the default uploader, so allowing many concurrent uploads may require raising :max_channels_per_transport:

defmodule MyAppWeb.UserSocket do
  use Phoenix.LiveView.Socket, max_channels_per_transport: 200
end

Summary

Types

The data in a LiveView as stored in the socket.

Struct returned when assigns is not in the socket.

t()

Types

assigns()

@type assigns() :: map() | assigns_not_in_socket()

The data in a LiveView as stored in the socket.

assigns_not_in_socket()

@opaque assigns_not_in_socket()

Struct returned when assigns is not in the socket.

t()

@type t() :: %Phoenix.LiveView.Socket{
  assigns: assigns(),
  endpoint: module(),
  host_uri: URI.t() | :not_mounted_at_router,
  id: binary(),
  parent_pid: nil | pid(),
  private: map(),
  redirected: nil | tuple(),
  root_pid: pid(),
  router: module(),
  sticky?: term(),
  transport_pid: pid() | nil,
  view: module()
}