Overview
Want to master Phoenix LiveView through working examples? This by-example guide teaches 95% of Phoenix LiveView through 85 annotated code examples organized by complexity level.
What Is By-Example Learning?
By-example learning is an example-first approach where you learn through annotated, runnable code rather than narrative explanations. Each example is self-contained, immediately executable in your Phoenix application, and heavily commented to show:
- What each line does - Inline comments explain the purpose and mechanism
- Expected outputs - Using
# =>notation to show socket states and DOM updates - State transitions - Socket assigns and component lifecycle made visible
- Key takeaways - 1-2 sentence summaries of core patterns
This approach is ideal for developers with Elixir and Phoenix experience who want to quickly understand LiveView's real-time capabilities, state management patterns, and interactive features through working code.
Unlike narrative tutorials that build understanding through explanation and storytelling, by-example learning lets you see the code first, run it second, and understand it through direct interaction. You learn by doing, not by reading about doing.
Learning Path
%% Color Palette: Blue #0173B2, Orange #DE8F05, Teal #029E73, Purple #CC78BC, Brown #CA9161
graph TD
A["Beginner<br/>Examples 1-30<br/>Core Concepts"] --> B["Intermediate<br/>Examples 31-60<br/>Forms & State"]
B --> C["Advanced<br/>Examples 61-85<br/>Production Patterns"]
style A fill:#0173B2,color:#fff
style B fill:#DE8F05,color:#fff
style C fill:#029E73,color:#fff
Progress from LiveView fundamentals through forms and state management to advanced patterns like PubSub, file uploads, and JavaScript interop. Each level builds on the previous, increasing in sophistication and introducing more real-time web development patterns.
Coverage Philosophy
This by-example guide provides 95% coverage of Phoenix LiveView through practical, annotated examples. The 95% figure represents the depth and breadth of concepts covered, not a time estimate—focus is on outcomes and understanding, not duration.
What's Covered
- Core LiveView - Mount lifecycle, socket state, assigns, rendering with HEEx
- Templates and rendering - HEEx syntax, dynamic content, conditionals, loops, function components
- Events and interactivity - Click events, form events, key events, debouncing, throttling
- Forms and validation - Changesets, live validation, error display, file uploads
- State management - Temporary assigns, streams, pagination, live navigation
- Real-time features - PubSub subscriptions, broadcast updates, multi-user sync
- LiveComponents - Stateful components, lifecycle, component communication, slots
- JavaScript interop - JS commands, client hooks, pushEvent/handleEvent
- Testing - LiveView testing patterns, form testing, component testing
- Production patterns - Handle_params, telemetry, rate limiting, session management
What's NOT Covered
This guide focuses on Phoenix LiveView specifically, not the broader Phoenix ecosystem. For additional topics:
- Phoenix fundamentals - Routing, controllers, Ecto covered in Phoenix tutorials
- Elixir language - Pattern matching, processes, OTP covered in Elixir tutorials
- Deep framework internals - LiveView source code, protocol implementation details
- Deployment specifics - Production deployment covered in Phoenix deployment guides
The 95% coverage goal maintains humility—no tutorial can cover everything. This guide teaches the core LiveView patterns that unlock the remaining 5% through your own exploration and project work.
How to Use This Guide
- Sequential or selective - Read examples in order for progressive learning, or jump to specific topics when building real-time features
- Run everything - Implement examples in your Phoenix application to see real-time updates yourself
- Modify and explore - Change event handlers, add assigns, break things intentionally. Learn through experimentation.
- Use as reference - Bookmark examples for quick lookups when implementing interactive features
- Complement with narrative tutorials - By-example learning is code-first; pair with comprehensive tutorials for deeper explanations
Best workflow: Open your Phoenix application in one window, this guide in another. Implement each example as you read it. When you encounter something unfamiliar, run the example, modify it, see what changes.
Relationship to Other Tutorials
Understanding where by-example fits in the tutorial ecosystem helps you choose the right learning path:
| Tutorial Type | Coverage | Approach | Target Audience | When to Use |
|---|---|---|---|---|
| By Example | 95% through 85 examples | Code-first, annotated examples | Elixir + Phoenix developers | Quick LiveView mastery, reference, feature impl |
| Quick Start | 5-30% touchpoints | Hands-on project | Phoenix developers | First LiveView taste, decide if worth learning |
| Beginner | 0-60% comprehensive | Narrative, explanatory | New to LiveView | Deep understanding, real-time concepts |
| Intermediate | 60-85% | Practical applications | Past LiveView basics | Production patterns, complex interactions |
| Advanced | 85-95% | Complex systems | Experienced LiveView devs | Performance optimization, scaling |
| Cookbook | Problem-specific | Recipe-based | All levels | Solve specific real-time problems |
By Example vs. Quick Start: By Example provides 95% coverage through examples vs. Quick Start's 5-30% through a single project. By Example is code-first reference; Quick Start is hands-on introduction.
By Example vs. Beginner Tutorial: By Example is code-first for experienced Elixir/Phoenix developers; Beginner Tutorial is narrative-first for developers new to real-time web. By Example shows patterns; Beginner Tutorial explains concepts.
By Example vs. Cookbook: By Example is learning-oriented (understand patterns); Cookbook is problem-solving oriented (fix specific issues). By Example teaches LiveView; Cookbook provides solutions.
Prerequisites
Required:
- Elixir proficiency - Comfortable with pattern matching, processes, modules
- Phoenix fundamentals - Understand routing, controllers, Ecto, templates
- Ability to run Phoenix applications - Can start a Phoenix server and navigate to LiveViews
Recommended (helpful but not required):
- Familiarity with reactive web frameworks (React, Vue, Svelte)
- Understanding of WebSocket communication
- Experience with server-side rendering concepts
No prior LiveView experience required - This guide assumes you're new to LiveView but experienced with Elixir and Phoenix. You should be comfortable reading Elixir code, understanding Phoenix applications, and learning through hands-on experimentation.
Structure of Each Example
Every example follows this consistent format:
### Example N: Concept Name
Brief explanation of the concept in 2-3 sentences. Explains **what** the LiveView pattern is and **why** it matters for real-time web applications.
[OPTIONAL: Mermaid diagram when state transitions or component relationships need visualization]
**Code**:
```elixir
defmodule MyAppWeb.ExampleLive do
use MyAppWeb, :live_view
# Mount callback - initializes socket state
def mount(_params, _session, socket) do
# Initial assigns set here
socket = assign(socket, :count, 0) # => count is 0
{:ok, socket} # => LiveView mounted, ready to render
end
# Event handler - updates state from user interaction
def handle_event("increment", _params, socket) do
# Update count assign
socket = update(socket, :count, &(&1 + 1)) # => count incremented by 1
{:noreply, socket} # => Re-render with new state
end
# Template rendering
def render(assigns) do
~H"""
<div>
<p>Count: <%= @count %></p>
<button phx-click="increment">Increment</button>
</div>
"""
end
end
```
**Key Takeaway**: 1-2 sentence summary highlighting the most important insight or pattern from this example.The brief explanation provides context. The code is heavily annotated with inline comments and # => output notation showing socket state changes. The key takeaway distills the pattern to its essence.
Mermaid diagrams appear when visual representation clarifies state transitions - showing lifecycle flows, component hierarchies, or event propagation. Not every example needs a diagram; they're used strategically to enhance understanding.
Learning Strategies
For React/Vue Developers
You're used to component state and reactive updates. LiveView will feel familiar yet fundamentally different:
- Server-side state: State lives on server, not client browser
- Automatic diffing: LiveView handles DOM updates, no virtual DOM needed
- Event binding: Use
phx-*attributes instead ofonClickhandlers - No JavaScript required: Most interactions work without custom client code
Focus on Examples 1-10 (mount and lifecycle), Examples 11-20 (templates and rendering), and Examples 21-30 (events) to build LiveView intuition.
For HTMX Users
You understand HTML-over-the-wire. LiveView extends this with persistent connections:
- WebSocket instead of polling: Persistent connection for instant updates
- Stateful interactions: Server maintains state between interactions
- Real-time updates: PubSub enables multi-user synchronization
- Component composition: Reusable LiveComponents for complex UIs
Focus on Examples 31-40 (forms), Examples 51-55 (PubSub), and Examples 61-70 (LiveComponents) to leverage your HTML-first mindset.
For Traditional Server-Side Developers
You're used to request/response cycles. LiveView maintains connection state:
- Persistent connections: WebSocket keeps connection alive between interactions
- No full page reloads: Only changed parts of DOM update
- Socket state: Think session state but with automatic synchronization
- Event-driven updates: User actions trigger server functions that update UI
Focus on Examples 1-15 (core concepts), Examples 41-50 (state management), and Examples 71-75 (JavaScript interop) to understand the LiveView model.
For Elixir Backend Developers
You know Elixir and Phoenix. LiveView adds real-time interactivity:
- Processes for connections: Each LiveView is a GenServer process
- Assigns as state: Socket assigns are like GenServer state
- Pattern match events: Handle_event uses pattern matching like handle_call
- PubSub for broadcast: Phoenix.PubSub enables real-time multi-user features
Focus on Examples 51-55 (PubSub), Examples 61-70 (LiveComponents), and Examples 81-85 (production patterns) to see LiveView's OTP foundations.
Code-First Philosophy
This tutorial prioritizes working code over theoretical discussion:
- No lengthy prose: Patterns are demonstrated, not explained at length
- Runnable examples: Every example runs in a Phoenix LiveView application
- Learn by doing: Understanding comes from implementing and modifying code
- Pattern recognition: See the same patterns in different contexts across 85 examples
If you prefer narrative explanations, complement this guide with comprehensive LiveView tutorials. By-example learning works best when you learn through experimentation.
Ready to Start?
Jump into the beginner examples to start learning Phoenix LiveView through code:
- Beginner Examples (1-30) - Core concepts, templates, events, interactivity
- Intermediate Examples (31-60) - Forms, state management, PubSub, file uploads
- Advanced Examples (61-85) - LiveComponents, JavaScript interop, testing, production patterns
Each example is self-contained and runnable. Start with Example 1, or jump to topics that interest you most.
Examples by Level
Beginner (Examples 1–30)
- Example 1: Hello World LiveView
- Example 2: Mount Lifecycle with Initial State
- Example 3: Rendering with HEEx Sigil
- Example 4: Handle Event Basics
- Example 5: Update Assigns with update/3
- Example 6: LiveView Lifecycle Flow
- Example 7: Initial vs Dynamic State
- Example 8: Debugging LiveView State
- Example 9: Multiple Assigns in One Operation
- Example 10: Connected vs Disconnected Mount
- Example 11: HEEx Template Syntax Deep Dive
- Example 12: Dynamic Content Rendering
- Example 13: Conditional Rendering with if/unless
- Example 14: Looping with for Comprehension
- Example 15: Function Components Basics
- Example 16: Slots Basics
- Example 17: LiveComponent Basics
- Example 18: Component Attributes
- Example 19: CSS Classes and Dynamic Styling
- Example 20: SVG and Icons in Templates
- Example 21: Click Events with phx-click
- Example 22: Form Events - phx-change and phx-submit
- Example 23: Keyboard Events
- Example 24: Focus Events
- Example 25: Window Events
- Example 26: Event Values and Targets
- Example 27: Debouncing Events
- Example 28: Throttling Events
- Example 29: Event Parameters with phx-value-*
- Example 30: Preventing Default Behavior
Intermediate (Examples 31–60)
- Example 31: Form Basics with Changesets
- Example 32: Form Validation - Live Error Display
- Example 33: Multi-field Forms
- Example 34: Nested Forms - Embedded Schemas
- Example 35: Form Recovery - phx-auto-recover
- Example 36: Submit Without Page Reload
- Example 37: Form Input Types - Text, Checkbox, Select
- Example 38: Custom Form Components
- Example 39: File Upload Basics
- Example 40: Form Progress Tracking
- Example 41: Temporary Assigns
- Example 42: assign_new for Lazy Evaluation
- Example 43: Update Patterns - update/3
- Example 44: Stream Collections
- Example 45: Reset Stream on Disconnect
- Example 46: Pagination with Streams
- Example 47: Infinite Scroll
- Example 48: Live Navigation - patch vs navigate
- Example 49: Query Parameters
- Example 50: Flash Messages
- Example 51: Phoenix.PubSub Basics
- Example 52: Subscribe to Multiple Topics
- Example 53: Broadcast Updates
- Example 54: handle_info for PubSub Messages
- Example 55: Multi-user Synchronization
- Example 56: Upload Configuration
- Example 57: Progress Tracking
- Example 58: File Validation
- Example 59: Consume Uploaded Entries
- Example 60: Multiple Upload Configurations
Advanced (Examples 61–85)
- Example 61: Stateful LiveComponent with Own State
- Example 62: LiveComponent Lifecycle - update/2 Flow
- Example 63: Component-to-Component Communication via Parent
- Example 64: send_update for External Component Updates
- Example 65: LiveComponent Events with Payload
- Example 66: Slots and Named Slots
- Example 67: Render Slots with Slot Attributes
- Example 68: Dynamic Components
- Example 69: Component Composition Patterns
- Example 70: Component Testing
- Example 71: JS Commands - push and navigate
- Example 72: Client Hooks Basics
- Example 73: Hook Lifecycle - mounted, updated, destroyed
- Example 74: pushEvent from Client
- Example 75: handleEvent on Client
- Example 76: LiveView Testing Basics - render and render_click
- Example 77: Testing Forms with render_submit
- Example 78: Testing File Uploads
- Example 79: Testing LiveComponents
- Example 80: Testing with Async/Await Patterns
- Example 81: handle_params for URL Changes
- Example 82: LiveView Telemetry for Monitoring
- Example 83: Rate Limiting and Security
- Example 84: Optimizing Rendering with Temporary Assigns
- Example 85: Session and Token Management
Last updated January 31, 2026