Skip to content

Elixir/Phoenix Recipes

Elixir gives you fault tolerance and concurrency that other languages bolt on as afterthoughts. But AI tools sometimes generate code that fights OTP patterns rather than leveraging them. These recipes produce idiomatic Elixir — proper GenServers, Ecto changesets with real validations, and Phoenix LiveView that uses the socket correctly.

  • Phoenix API and LiveView recipes with proper channel/socket patterns
  • Ecto schema and query recipes with composable queries and proper changesets
  • OTP patterns: GenServer, Supervisor trees, and Task supervision
  • Testing recipes for ExUnit with proper sandbox and async patterns

Recipe 1: Phoenix JSON API with Ecto Changesets

Section titled “Recipe 1: Phoenix JSON API with Ecto Changesets”

Scenario: You need a REST API for a task management app with proper validation and error formatting.

Expected output: Context module, schema with changeset, controller, JSON views, OpenAPI spec, and tests.


Recipe 2: Phoenix LiveView Real-Time Dashboard

Section titled “Recipe 2: Phoenix LiveView Real-Time Dashboard”

Scenario: Your dashboard shows live metrics that currently require page refresh. You need real-time updates.

Expected output: LiveView module, 3 LiveComponents, PubSub subscriptions, JS hooks, and LiveViewTest tests.


Scenario: Your API needs per-user rate limiting but you do not want an external dependency like Redis for this.

Expected output: GenServer, ETS optimization, Plug middleware, distribution support, and tests.


Recipe 4: Ecto Multi for Complex Transactions

Section titled “Recipe 4: Ecto Multi for Complex Transactions”

Scenario: Creating an order involves updating stock, creating payment records, and sending notifications — if any step fails, everything should roll back.

Expected output: Ecto.Multi pipeline, error handling, PubSub broadcast, and transaction tests.


Recipe 5: Background Job Processing with Oban

Section titled “Recipe 5: Background Job Processing with Oban”

Scenario: You need reliable background jobs with retries, scheduling, and monitoring.

Expected output: Oban config, 3 workers, cron schedule, unique constraints, and worker tests.


Recipe 6: Authentication with Guardian and Plugs

Section titled “Recipe 6: Authentication with Guardian and Plugs”

Scenario: Your API needs JWT auth with role-based access control and token refresh.

Expected output: Guardian config, auth pipeline, role plug, auth controller, token revocation, and tests.


Scenario: Your queries are duplicated across contexts with slight variations. You need composable, reusable query building.

Expected output: QueryBuilder module, scopes, preload helpers, aggregation queries, and composition tests.


Scenario: You need multi-room chat with presence tracking and message persistence.

Expected output: Room channel, message persistence, presence tracking, typing indicators, and channel tests.


Scenario: Your application has components that crash independently. You need proper supervision for fault isolation.

Expected output: Application supervisor, WorkerSupervisor, ConnectionPool with DynamicSupervisor, and restart tests.


Scenario: Your deployment copies the project to the server and runs mix phx.server. You need proper releases.

Expected output: Release config, runtime.exs, migration scripts, Dockerfile, CI workflow, and boot tests.