Phoenix.Socket
Holds state for multiplexed socket connections and Channel/Topic authorization
Summary
| add_channel(socket, channel, topic) | Adds authorized channel/topic pair to Socket’s channel list |
| assign(socket, key, value) | Adds key/value pair to ephemeral socket state |
| assign(socket, channel, topic, key, value) | |
| authenticated?(socket, channel, topic) | Checks if a given String channel/topic pair is authorized for this Socket |
| delete_channel(socket, channel, topic) | Removes a channel/topic pair from authorized channels list |
| get_assign(socket, key) | Returns the value for the given assign key, scoped to the active multiplexed channel/topic pair or for a specific channel/topic |
| get_assign(socket, channel, topic, key) | |
| set_current_channel(socket, channel, topic) | Sets current channel of multiplexed socket connection |
Functions
Adds authorized channel/topic pair to Socket’s channel list
Examples
iex> Socket.add_channel(%Socket{channels: []}, "rooms", "lobby")
%Socket{channels: [{"rooms", "lobby"}]}
Adds key/value pair to ephemeral socket state
Examples
iex> socket = Socket.set_current_channel(%Socket{}, "rooms", "lobby")
%Socket{channel: "rooms", topic: "lobby"}
iex> Socket.get_assign(socket, :token)
nil
iex> socket = Socket.assign(socket, :token, "bar")
iex> Socket.get_assign(socket, :token)
"bar"
iex> Socket.get_assign(socket, "rooms", "lobby", :token)
"bar"
Checks if a given String channel/topic pair is authorized for this Socket
Examples
iex> socket = %Socket{}
iex> Socket.authenticated?(socket, "rooms", "lobby")
false
iex> socket = Socket.add_channel(socket, "rooms", "lobby")
%Socket{channels: [{"rooms", "lobby"}]}
iex> Socket.authenticated?(socket, "rooms", "lobby")
true
Removes a channel/topic pair from authorized channels list
Examples
iex> socket = Socket.add_channel(%Socket{channels: []}, "rooms", "lobby")
%Socket{channels: [{"rooms", "lobby"}]}
iex> Socket.delete_channel(socket, "rooms", "lobby")
%Socket{channels: []}
Returns the value for the given assign key, scoped to the active multiplexed channel/topic pair or for a specific channel/topic
Examples
iex> socket = Socket.set_current_channel(%Socket{}, "rooms", "lobby")
%Socket{channel: "rooms", topic: "lobby"}
iex> Socket.get_assign(socket, :token)
nil
iex> socket = Socket.assign(socket, :token, "bar")
iex> Socket.get_assign(socket, :token)
"bar"
iex> Socket.get_assign(socket, "rooms", "lobby", :token)
"bar"