Phoenix.Socket
Holds state for every channel, pointing to its transport, pubsub server and more.
Socket Fields
id- The string id of the socketassigns- The map of socket assigns, default:%{}channel- The channel module where this socket originatedchannel_pid- The channel pidendpoint- The endpoint module where this socket originatedjoined- If the socket has effectively joined the channelpubsub_server- The registered name of the socket’s PubSub serverref- The latest ref sent by the clienttopic- The string topic, for example"rooms:123"transport- The socket’s transport, for example:Phoenix.Transports.WebSockettransport_pid- The pid of the socket’s transport processserializer- ThePhoenix.Socket.Messageserializer, for example:Phoenix.Transports.WebSocketSerializer
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:
connect/2- receives the socket params and authenticates the connection. Often used to wire up default%Phoenix.Socket{}assigns for all channels.id/1- receives the socket returned byconnect/2, and returns the string id of this connection. Used for forcing a disconnect for connection and all child channels. For sockets requiring no authentication,nilcan be returned.
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.
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 ↑
Functions
Adds key/value pair to socket assigns.
Examples
iex> socket.assigns[:token]
nil
iex> socket = assign(socket, :token, "bar")
iex> socket.assigns[:token]
"bar"
Macros
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 exampleMyApp.RoomChannelopts- 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
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