Phoenix.Socket

Holds state for every channel, pointing to its transport, pubsub server and more.

Socket Fields

Channels

Channels allow you to route pubsub events to channel handlers in your application. By default, Phoenix supports both :websocket and :longpoll transports. See the Phoenix.Channel.Transport documentation for more information on writing your own transports. Channels are defined within a socket handler, using the channel/2 macro, as seen below.

Socket Behaviour

Socket handlers are mounted in Endpoints and must define two callbacks:

Callback examples:

defmodule MyApp.UserSocket do
  use Phoenix.Socket

  channel "rooms:*", MyApp.RoomChannel

  def connect(params, socket) do
    {:ok, assign(socket, :user_id, params["user_id"])}
  end

  def id(socket), do: "users_socket:#{socket.assigns.user_id}"
end

...
# disconnect all user's socket connections and their multiplexed channels
MyApp.Endpoint.broadcast("users_socket:" <> user.id, "disconnect")

Transport Configuration

Transports are defined and configured within socket handlers. By default, Phoenix defines the :websocket, and :longpoll transports automaticaly with overridable options. Check the transport modules for transport specific options. A list of allowed origins can be specified in the :origins key for the :websocket and :longpoll transports. This will restrict clients based on the given Origin header.

transport :longpoll, Phoenix.Transports.LongPoll,
  origins: ["//example.com", "http://example.com", "https://example.com"]

transport :websocket, Phoenix.Transports.WebSocket,
  origins: ["//example.com", "http://example.com", "https://example.com"]

If no such header is sent no verification will be performed. If the Origin header does not match the list of allowed origins a 403 Forbidden response will be sent to the client. See transport/3 for more information.

Source

Summary

assign(socket, key, value)

Adds key/value pair to socket assigns

channel(topic_pattern, module, opts \\ [])

Defines a channel matching the given topic and transports

transport(name, module, config \\ [])

Defines a transport with configuration

Types

t :: %Phoenix.Socket{id: nil, assigns: %{}, channel: atom, channel_pid: pid, endpoint: atom, joined: boolean, pubsub_server: atom, ref: term, topic: String.t, transport: atom, serializer: atom, transport_pid: pid}

Functions

assign(socket, key, value)

Adds key/value pair to socket assigns.

Examples

iex> socket.assigns[:token]
nil
iex> socket = assign(socket, :token, "bar")
iex> socket.assigns[:token]
"bar"
Source

Macros

channel(topic_pattern, module, opts \\ [])

Defines a channel matching the given topic and transports.

  • topic_pattern - The string pattern, for example “rooms:“, “users:“, “system”
  • module - The channel module handler, for example MyApp.RoomChannel
  • opts - The optional list of options, see below

Options

  • :via - the transport adapters to accept on this channel. Defaults [:websocket, :longpoll]

Examples

channel "topic1:*", MyChannel
channel "topic2:*", MyChannel, via: [:websocket]
channel "topic",    MyChannel, via: [:longpoll]

Topic Patterns

The channel macro accepts topic patterns in two flavors. A splat argument can be provided as the last character to indicate a “topic:subtopic” match. If a plain string is provied, only that topic will match the channel handler. Most use-cases will use the “topic:*” pattern to allow more versatile topic scoping.

See Phoenix.Channel for more information

Source
transport(name, module, config \\ [])

Defines a transport with configuration.

Examples

# customize default `:websocket` transport options
transport :websocket, Phoenix.Transports.WebSocket,
  timeout: 10_000

# define separate transport, using websocket handler
transport :websocket_slow_clients, Phoenix.Transports.WebSocket,
  timeout: 60_000
Source