Phoenix.Socket
Holds state for multiplexed socket connections and Channel authorization
Socket Fields
pid- The Pid of the socket’s transport processtopic- The string topic, ie"rooms:123"router- The router module where this socket originatedendpoint- The endpoint module where this socket originatedchannel- The channel module where this socket originatedauthorized- The boolean authorization status, defaultfalseassigns- The map of socket assigns, default:%{}transport- The socket’s Transport, ie:Phoenix.Transports.WebSocketpubsub_server- The registered name of the socket’s PubSub server
Summary↑
| assign(socket, key, value) | Adds key/value pair to ephemeral socket state |
| authorize(socket, topic) | Authorizes socket’s topic |
| authorized?(socket, topic) | Checks if a given String topic is authorized for this Socket |
| deauthorize(socket) | Deauthorizes topic |
| put_channel(socket, channel) | Sets channel of socket |
| put_topic(socket, topic) | Sets topic of socket |
Functions
Adds key/value pair to ephemeral socket state
Examples
iex> socket = Socket.put_topic(%Socket{}, "rooms:lobby")
%Socket{topic: "rooms:lobby"}
iex> socket.assigns[:token]
nil
iex> socket = Socket.assign(socket, :token, "bar")
iex> socket.assigns[:token]
"bar"
Authorizes socket’s topic
Examples
iex> Socket.authorize(%Socket{}, "rooms:lobby")
%Socket{topic: "rooms:lobby", authorized: true}
Checks if a given String topic is authorized for this Socket
Examples
iex> socket = %Socket{}
iex> Socket.authorized?(socket, "rooms:lobby")
false
iex> socket = Socket.authorize(socket, "rooms:lobby")
%Socket{topic: "rooms:lobby", authorized: true}
iex> Socket.authorized?(socket, "rooms:lobby")
true
Deauthorizes topic
Examples
iex> socket = Socket.authorize(%Socket{}, "rooms:lobby")
%Socket{topic: "rooms:lobby", authorized: true}
iex> Socket.deauthorize(socket)
%Socket{topic: "rooms:lobby", authorized: false}
Sets channel of socket
Sets topic of socket