| name | phoenix-api-channels |
| description | Phoenix controllers, JSON APIs, Channels, and Presence on the BEAM |
| user-invocable | false |
| disable-model-invocation | true |
| version | 1.0.0 |
| category | toolchain |
| author | Claude MPM Team |
| license | MIT |
| progressive_disclosure | {"entry_point":{"summary":"Phoenix REST/JSON + Channels/Presence on the BEAM with contexts, plugs, auth, and PubSub.","when_to_use":["Building JSON APIs with Phoenix controllers and versioned routes","Adding WebSocket/Channels for chat, notifications, or collaborative features","Needing Presence tracking, fan-out broadcasts, and PubSub-backed updates","Integrating Ecto contexts for persistence and domain boundaries"],"quick_start":["mix phx.new my_api --no-html --no-live && cd my_api","mix deps.get && mix ecto.create","Define contexts + schemas; add routes/controllers; wire `socket` + Channel modules","Start server: mix phx.server (REST + WebSocket on http://localhost:4000)"]},"token_estimate":{"entry":170,"full":5200}} |
Phoenix APIs, Channels, and Presence (Elixir/BEAM)
Phoenix excels at REST/JSON APIs and WebSocket Channels with minimal boilerplate, leveraging the BEAM for fault tolerance, lightweight processes, and supervised PubSub/Presence.
Core pillars
- Controllers for JSON APIs with plugs, pipelines, and versioning.
- Contexts own data (Ecto schemas + queries) and expose a narrow API to controllers/channels.
- Channels + PubSub for fan-out real-time updates; Presence for tracking users/devices.
- Auth via plugs (session/cookie for browser, token/Bearer for APIs), with signed params.
Project Setup
mix phx.new my_api --no-html --no-live
cd my_api
mix deps.get
mix ecto.create
mix phx.server
Key files:
lib/my_api_web/endpoint.ex — plugs, sockets, instrumentation
lib/my_api_web/router.ex — pipelines, scopes, versioning, sockets
lib/my_api_web/controllers/* — REST/JSON controllers
lib/my_api/* — contexts + Ecto schemas (ownership of data logic)
lib/my_api_web/channels/* — Channel modules
Routing and Pipelines
Separate browser vs API pipelines; version APIs with scopes.
defmodule MyApiWeb.Router do
use MyApiWeb, :router
pipeline :api do
plug :accepts, ["json"]
plug :fetch_session
plug :protect_from_forgery
plug MyApiWeb.Plugs.RequireAuth
end
scope "/api", MyApiWeb do
pipe_through :api
scope "/v1", V1, as: :v1 do
resources "/users", UserController, except: [:new, :edit]
post "/sessions", SessionController, :create
end
end
socket "/socket", MyApiWeb.UserSocket,
websocket: [connect_info: [:peer_data, :x_headers]],
longpoll: false
end
Tips
- Keep pipelines short; push auth/guards into plugs.
- Expose
socket "/socket" for Channels; restrict transports as needed.
Controllers and Plugs
Controllers stay thin; contexts own the logic.
defmodule MyApiWeb.V1.UserController do
use MyApiWeb, :controller
alias MyApi.Accounts
action_fallback MyApiWeb.FallbackController
def index(conn, _params) do
users = Accounts.list_users()
render(conn, :index, users: users)
end
def create(conn, params) do
with {:ok, user} <- Accounts.register_user(params) do
conn
|> put_status(:created)
|> put_resp_header("location", ~p\"/api/v1/users/#{user.id}\")
|> render(:show, user: user)
end
end
end