PhoenixLiveSession (phoenix_live_session v0.1.3) View Source

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 for which Phoenix.LiveView.connected?/1 returns 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", []))
end

Options

  • :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.get/3.

Callback implementation for Plug.Session.Store.init/1.

Subscribes connected LiveView socket to LiveSession.

This function can be called in two ways:any()

Link to this section 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.

Link to this function

maybe_subscribe(socket, session)

View Source

Specs

Subscribes connected LiveView socket to LiveSession.

Call this function in mount/3.

Link to this function

put(conn, sid, data, opts)

View Source

Callback implementation for Plug.Session.Store.put/4.

Link to this function

put_session(socket, key, value)

View Source

Specs

put_session(Phoenix.LiveView.Socket.t(), String.t() | atom(), term()) ::
  Phoenix.LiveView.Socket.t()
put_session(
  %{__sid__: String.t(), __opts__: list()},
  String.t() | atom(),
  term()
) :: %{}

This function can be called in two ways:any()

Using a Socket

Use like Plug.Conn.put_session/3 but on a LiveView socket previously subscribed to PhoenixLiveSession with maybe_subscribe/2. Returns socket

Using on a Session Map

If you don’t want to subscribe your Socket or if you want to store session data from outside a LiveView, use the session data map to call from the mount/3 callback directly in this function. Retrieves and returns updated session data.