Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Practice, Review and Next Steps

Consolidate your Phoenix skills: a request-lifecycle review, common mistakes, a decision guide for LiveView vs controllers vs channels, practice tasks and a learning path.

You have now met the whole Phoenix stack: the router and plugs, controllers and templates, Ecto, LiveView, channels and PubSub, authentication, testing and deployment. This last lesson ties them together. It reviews what happens to one request, lists the mistakes that cost beginners the most time, helps you choose the right tool for a feature, and gives you practice tasks and a path forward.

Phoenix does not run on this site, so the review code is read-only and the editors are JavaScript stand-ins.

One request, end to end

flowchart LR
  A["Browser"] --> B["Endpoint plugs: static, parsers, session"]
  B --> C["Router: match and pipeline"]
  C --> D["Controller or LiveView"]
  D --> E["Context: business rules"]
  E --> F["Ecto Repo and database"]
  D --> G["Template HEEx render"]
  G --> A

Plugs wrap the request in layers; the context is the only place that knows about the database.

The same layering applies to a WebSocket: the endpoint hands it to UserSocket, then to a channel or a LiveView process, which calls the same contexts. If you keep business rules in contexts, every entry point (controller, LiveView, channel, background job, IEx) behaves identically.

Pick the right tool

Need Best fit Why
Public page, SEO, simple form Controller + HEEx Stateless, cacheable, easy
Interactive page with server state LiveView No JavaScript framework, server is the source of truth
Live updates between users PubSub + LiveView A context broadcasts, views react
Non-browser real-time client (mobile, IoT) Channels Protocol works from any language
Who is online Presence Cluster-wide tracking with automatic cleanup
Webhook or JSON API Controller + JSON module Plain request/response
Slow work (email, image resize) Background job (Oban) Retries and persistence

Mistakes that cost the most time

  1. Subscribing or loading data in the disconnected mount. mount/3 runs twice. Guard side effects with connected?(socket) (see the lifecycle).
  2. Putting business logic in controllers or LiveViews. It becomes impossible to reuse and test. Move it to a context.
  3. Trusting params. cast/3 only the fields a user may set; set user_id yourself (see changesets).
  4. N+1 queries. Preload associations in the query, not inside a loop in the template (see Ecto queries).
  5. Large assigns in LiveView. Everything in assigns is kept in memory per user and diffed. Use streams for big lists.
  6. Forgetting halt/1 in a plug. The controller still runs after your 401.
  7. Compile-time secrets. Read secrets in runtime.exs, not prod.exs (see releases).
  8. Skipping authorization on events. Every handle_event and channel handle_in is a public endpoint; check permissions in it (see authorization).

Runnable review: layered plugs

A request flows through plugs like a pipeline, and any plug can halt it. This stand-in shows the order and the effect of halt.

Node.js A plug pipeline that can halt (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Runnable review: the context as single source of truth

This stand-in shows why contexts pay off: three entry points call the same function, so the rule (no empty titles) and the side effect (a broadcast) apply everywhere.

Node.js Three entry points, one context (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Practice tasks

Work through these in a fresh mix phx.new project. Each one touches several chapters.

  1. Bookmarks. Users save links with tags. Use phx.gen.auth, a Bookmarks context scoped by user, a LiveView with filter-as-you-type using phx-change and phx-debounce, and tests for ownership.
  2. Auction page. Bids arrive in real time. Broadcast from the context, show a countdown with Process.send_after/3, and reject late bids with a changeset error.
  3. Live dashboard. Count orders per minute from :telemetry events and show them in a LiveView that updates every second (see telemetry).
  4. Image gallery. Use uploads with previews, store originals on disk and show a stream of thumbnails.
  5. Deploy it. Build a release, run it in Docker on a small server, run migrations through MyApp.Release, and add a /readyz endpoint.

Review questions to ask yourself

  • Which processes exist for one connected LiveView user, and what happens to its state when the node restarts?
  • Where would you add a new rule so that controllers, LiveViews and jobs all obey it?
  • Which of your routes are public, and what stops a logged-out user from calling a handle_event directly?
  • What does a 10x traffic spike do to your database pool, and how would you notice it?

Where to go next

  • Elixir itself: the Elixir tutorial covers processes, OTP and supervision, which explain why Phoenix is robust.
  • Background work: learn Oban for reliable jobs and scheduled tasks.
  • Data: read about Ecto multi, upserts, window functions and composable queries.
  • Production: study clustering, hot upgrades vs rolling deploys, and OpenTelemetry tracing.
  • Frontend: colocated hooks, JS commands and Tailwind components in core_components.ex.
  • Ecosystem: Absinthe (GraphQL), Swoosh (email), Req/Finch (HTTP clients), Broadway (data pipelines), Nx (machine learning).

Return to the Phoenix track page any time to revisit a lesson.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why should business rules live in a context rather than in a controller?

    One implementation applied from every entry point keeps behavior consistent.
  2. 2. Which feature is the best fit for a native mobile app that needs live updates?

    Channels speak a simple protocol that any client library can use; LiveView is for HTML in browsers.
  3. 3. What problem does connected?(socket) guard against?

    mount/3 runs for the static render and again when the socket connects; side effects belong in the connected one.
  4. 4. Which is a real authorization gap?

    Events can be sent by anyone with a socket, so they need the same checks as routes.
  5. 5. Where should the production database URL be read?

    runtime.exs runs when the release boots, on the target machine.

Summary

  • A request passes plugs, the router, a controller or LiveView, a context and Ecto; keep rules in contexts.
  • Choose controllers for plain pages and APIs, LiveView for interactive server-driven UI, channels for non-browser clients and Presence for who is online.
  • Avoid the common traps: side effects in the static mount, unchecked events, N+1 queries, big assigns and compile-time secrets.
  • Practice by combining chapters in small projects, then deploy one for real.

Next: Back to the Phoenix track