- Home
- Skills
- J Morgan6
- Elixir Phoenix Guide
- Phoenix Liveview Essentials
phoenix-liveview-essentials_skill
- Shell
56
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill j-morgan6/elixir-phoenix-guide --skill phoenix-liveview-essentials- SKILL.md10.9 KB
Overview
This skill is a mandatory checklist and compact reference for building reliable Phoenix LiveViews. It enforces lifecycle best practices, assign initialization, and safe side-effect handling to prevent common runtime crashes. Use it before writing LiveView modules or .heex templates to avoid typical pitfalls and ensure predictable behavior.
How this skill works
The skill inspects your LiveView patterns against a set of hard rules: always add @impl true to callbacks, initialize all assigns in mount/3, and guard side effects with connected?(socket). It explains two-phase rendering, when mount and handle_params run twice, and shows safe patterns for assigns, PubSub subscriptions, streams, forms, and error handling. Follow its examples to return correct tuples, use with for error flows, and avoid unsafe operations during the static render phase.
When to use it
- Before creating any LiveView module or .heex template
- When initializing assigns and planning side effects (PubSub, timers, expensive queries)
- When implementing mount/3, handle_event/3, handle_info/2, or handle_params/3
- When wiring forms, uploads, streams, or live navigation
- When testing LiveViews with Phoenix.LiveViewTest
Best practices
- Always add @impl true before every LiveView callback to satisfy compiler checks
- Initialize every assign in mount/3 so render/1 never reads uninitialized data
- Check connected?(socket) before subscribing, starting timers, or running expensive queries
- Use Map.get(socket.assigns, :key, default) in helpers for optional assigns
- Return correct tuples: {:ok, socket} from mount, {:noreply, socket} from event/info handlers
- Use with or pattern matching to handle errors and assign failures to the socket instead of crashing
Example use cases
- Mount a posts LiveView: initialize posts to [] and subscribe to PubSub only when connected
- Build a form LiveView: assign form = to_form(changeset) in mount and validate with phx-change handlers
- Stream large lists: use stream(socket, :posts, list) and stream_insert/stream_delete in handle_event
- Handle broadcasts: subscribe in mount when connected and update assigns in handle_info
- Implement navigation: use push_navigate/2 for full LiveView changes and push_patch/2 for intra-view patches
FAQ
LiveView does a static render on the initial HTTP request. If assigns are missing the static render can crash with KeyError, so mount must set safe defaults for both phases.
When should I subscribe to PubSub or start timers?
Only when connected?(socket) is true. Subscribing or starting timers during the static render has no effect and can cause duplicate or expensive operations.