AgentSkillsCN

phoenix-liveview

适用于与LiveView流、带有changesets与to_form的表单、Phx-hook JavaScript集成、live_session认证范围,或调试LiveView常见陷阱(无else-if、流ID不匹配、mount重复运行)的场景。

SKILL.md
--- frontmatter
name: phoenix-liveview
description: Use when working with LiveView streams, forms with changesets and to_form, phx-hook JavaScript integration, live_session authentication scopes, or debugging LiveView gotchas (no else-if, stream ID mismatches, mount runs twice)

Phoenix LiveView Patterns

Overview

Start with server-rendered HTML. Escalate through streams, assign_async, PubSub, and hooks only when simpler patterns can't do the job.

Escalation Ladder

LevelNeedPattern
L0Display data, no interactionAssigns in mount/3
L1Buttons, links, navigationphx-click, push_navigate/2, flash
L2Form input with validationto_form/2 + changeset + phx-change/phx-submit
L3Dynamic list add/remove/reorderstream/3, stream_insert/4, stream_delete/3
L4Data from other users/backgroundPubSub subscribe, assign_async/3, handle_info
L5Browser APIs or JS librariesphx-hook with mounted()/destroyed()

Quick Reference

OperationFunctionNotes
Add stream itemstream_insert(socket, :name, item, at: pos)Default appends (:at -1)
Remove stream itemstream_delete(socket, :name, item)Requires item with :id
Reset streamstream(socket, :name, items, reset: true)Clears existing items
Push event to clientpush_event(socket, "event-name", %{data})Received by JS hooks
Navigatepush_navigate(socket, to: path)Client-side navigation
Redirectredirect(socket, to: path)Server redirect

Common Gotchas

  • No else if in HEEx — use case/cond or multiple :if attributes
  • Stream ID mismatch — container id MUST match stream name (id="users" for :users)
  • Mount runs twice — once for HTTP, once for WebSocket; check connected?(socket) for PubSub
  • Form field access — use @form[:email] not @form.email
  • Missing to_form — always convert changesets with to_form/2
  • Hook needs id — hook element MUST have unique id or it silently won't work
  • Attribute interpolation — use {@class} not @class in HEEx attributes

Common Mistakes

MistakeFix
Forgot :id on phx-update elementAdd unique id matching stream name
Form not using to_form/2Always convert changesets with to_form/2
Missing :action on validationSet Map.put(:action, :validate) on changeset
Race condition in asyncUse assign_async/3 instead of manual async
Hook not receiving eventsCheck element has unique id and hook name matches

Reference Files

  • escalation-ladder.md — Full L0-L5 with code examples, auth scopes, real-world patterns, testing
  • references/streams.md — Comprehensive stream patterns and pagination
  • references/forms.md — Advanced form patterns including uploads and nested forms
  • references/hooks.md — JavaScript hooks with third-party library integration
  • references/authentication.md — Role-based authorization and session management

Related Skills

  • elixir-patterns: OTP patterns (GenServer, supervision) underlying LiveView
  • production-quality: Testing strategies and observability