PhoenixLiveSession (phoenix_live_session v0.1.1)
Store for Plug.Sessions with PubSub features for Phoenix.LiveView
Setup
Use this in your Endpoint module when defining options for Plug.Sessions like so:
# lib/my_app_web/endpoint.ex
@session_options [
store: PhoenixLiveSession,
pub_sub: MyApp.PubSub,
signing_salt: "your-salt",
]Usage in Phoenix Controllers
You don’t need to do anything special to use PhoenixLiveSession in regular Phoenix controllers. Your sessions will continue to work the same as with other stores.
Usage in LiveViews
Use maybe_subscribe/2 in your mount/3 function to subscribe to
LiveSession updates.
Only sockets with connected? == true are subscribed by maybe_subscribe/2.
Once you’ve subscribed to a LiveSession, you can handle session
updates with handle_info/2 and push session updates with put_session/3.
Example
def mount(_params, session, socket) do
socket = socket
|> PhoenixLiveSession.maybe_subscribe(session)
|> put_session_assigns(session)
{:ok, socket}
end
def handle_info({:live_session_updated, session}, socket) do
{:noreply, put_session_assigns(socket, session)}
end
def handle_event("add_to_cart", %{"product_id" => product_id}, socket) do
updated_cart = [product_id | socket.assigns.cart]
PhoenixLiveSession.put_session(socket, "cart", updated_cart)
{:noreply, socket}
end
defp put_sesion_assigns(socket, session) do
socket
|> assign(:shopping_cart, Map.get(session, "shopping_cart", []))
endOptions
:pub_sub- Required.- Module for handling PubSub (e.g.MyApp.PubSub).:table- ETS table name. Defaults to:phoenix_live_sessions:lifetime- Lifetime (in ms) of sessions before they are cleared. Reads and writes refresh session lifetime. Defaults to two days.clean_interval- Interval (in ms) after which expired PhoenixLiveSession are cleared. Defaulst to 60 seconds.
Caveats
Since sessions are stored in memory, they will be lost when restarting your server and are not shared between servers in multi-node setups.
Link to this section Summary
Functions
Callback implementation for Plug.Session.Store.delete/3.
Callback implementation for Plug.Session.Store.get/3.
Callback implementation for Plug.Session.Store.init/1.
Subscribes connected LiveView socket to LiveSession.
Callback implementation for Plug.Session.Store.put/4.
Use like Plug.Conn.put_session/3 but on a LiveView socket previously
subscribed to PhoenixLiveSession with maybe_subscribe/2.
Link to this section Functions
delete(conn, sid, opts)
Callback implementation for Plug.Session.Store.delete/3.
get(conn, sid, opts)
Callback implementation for Plug.Session.Store.get/3.
init(opts)
Callback implementation for Plug.Session.Store.init/1.
maybe_subscribe(socket, arg2)
Specs
maybe_subscribe(Phoenix.LiveView.Socket.t(), Plug.Session.Store.session()) :: Phoenix.LiveView.Socket.t()
Subscribes connected LiveView socket to LiveSession.
Call this function in mount/3.
put(conn, sid, data, opts)
Callback implementation for Plug.Session.Store.put/4.
put_session(socket, key, value)
Specs
put_session(Phoenix.LiveView.Socket.t(), String.t() | atom(), term()) :: Phoenix.LiveView.Socket.t()
Use like Plug.Conn.put_session/3 but on a LiveView socket previously
subscribed to PhoenixLiveSession with maybe_subscribe/2.