CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/hex-phoenix

Peace of mind from prototype to production - comprehensive web framework for Elixir

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Phoenix Web Framework

Phoenix is a comprehensive web framework for Elixir that provides a complete foundation for building scalable, maintainable web applications and APIs. It offers productive development through live code reloading, comprehensive generators, real-time features via channels and LiveView, robust authentication systems, and built-in security measures leveraging the fault-tolerant Erlang VM.

Package Information

  • Package Name: phoenix
  • Package Type: hex
  • Language: Elixir
  • Installation: {:phoenix, "~> 1.8"} in mix.exs

Core Imports

# In your application
defmodule MyAppWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app
end

# In controllers
defmodule MyAppWeb.PageController do
  use Phoenix.Controller, formats: [:html, :json]
end

# In channels
defmodule MyAppWeb.UserChannel do
  use Phoenix.Channel
end

# Common imports for utilities
import Phoenix.Controller
import Phoenix.Token
import Phoenix.VerifiedRoutes

Basic Usage

# Define an endpoint
defmodule MyAppWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app

  plug Plug.RequestId
  plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
  plug MyAppWeb.Router
end

# Define routes
defmodule MyAppWeb.Router do
  use Phoenix.Router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :protect_from_forgery
  end

  scope "/", MyAppWeb do
    pipe_through :browser
    get "/", PageController, :index
    resources "/users", UserController
  end
end

# Handle requests
defmodule MyAppWeb.PageController do
  use Phoenix.Controller, formats: [:html]

  def index(conn, _params) do
    render(conn, "index.html", title: "Welcome")
  end
end

Architecture

Phoenix follows the Model-View-Controller pattern with additional layers:

  • Endpoint: Entry point that handles all requests and provides configuration
  • Router: Matches URLs to controller actions using pipelines for shared functionality
  • Controller: Handles requests, processes data, and renders responses
  • View: Transforms data for presentation (optional with Phoenix 1.7+)
  • Channel: Enables bidirectional real-time communication over WebSockets
  • Context: Business logic layer that encapsulates related functionality
  • PubSub: Message broadcasting system for real-time features
  • Presence: Distributed user presence tracking across nodes

Capabilities

Core Phoenix Module

Application-level configuration and JSON library management.

defmodule Phoenix do
  def json_library() :: module
  def plug_init_mode() :: :compile | :runtime
end

Web Foundation and Request Handling

Core web server functionality, request routing, and response handling.

defmodule Phoenix.Endpoint do
  @callback start_link(keyword) :: Supervisor.on_start()
  @callback config(atom, term) :: term
  @callback url() :: String.t()
  @callback struct_url() :: URI.t()
  @callback static_path(String.t()) :: String.t()
  @callback host() :: String.t()
  @callback server_info(Plug.Conn.scheme()) :: {:ok, {String.t(), pos_integer}} | :error
  @callback subscribe(binary, keyword) :: :ok | {:error, term}
  @callback unsubscribe(binary) :: :ok | {:error, term}
  @callback broadcast(binary, binary, term) :: :ok | {:error, term}
  @callback broadcast!(binary, binary, term) :: :ok
end

defmodule Phoenix.Router do
  defmacro get(path, plug, plug_opts \\ [], options \\ [])
  defmacro post(path, plug, plug_opts \\ [], options \\ [])
  defmacro put(path, plug, plug_opts \\ [], options \\ [])
  defmacro patch(path, plug, plug_opts \\ [], options \\ [])
  defmacro delete(path, plug, plug_opts \\ [], options \\ [])
  defmacro resources(path, controller, opts \\ [])
  defmacro scope(options \\ [], do: context)
  defmacro pipeline(name, do: block)
  defmacro plug(plug, opts \\ [])
  defmacro pipe_through(pipes)
end

defmodule Phoenix.Controller do
  def action_name(conn) :: atom
  def controller_module(conn) :: atom
  def render(conn, template_or_assigns \\ [])
  def json(conn, data)
  def text(conn, data)
  def html(conn, data)
  def redirect(conn, opts)
  def put_view(conn, formats)
  def put_layout(conn, layout)
end

Web Foundation and Request Handling

Real-time Communication

WebSocket channels, broadcasting, and bidirectional communication.

defmodule Phoenix.Channel do
  @callback join(binary, map, Phoenix.Socket.t()) ::
    {:ok, Phoenix.Socket.t()} | {:error, term}
  @callback handle_in(binary, map, Phoenix.Socket.t()) ::
    {:noreply, Phoenix.Socket.t()} | {:reply, map, Phoenix.Socket.t()}

  def broadcast(socket, event, message)
  def push(socket, event, message)
end

defmodule Phoenix.Socket do
  @callback connect(map, Phoenix.Socket.t(), map) ::
    {:ok, Phoenix.Socket.t()} | :error
end

Real-time Communication

Security and Authentication

Token generation, CSRF protection, and secure headers.

defmodule Phoenix.Token do
  def sign(context, salt, data, opts \\ [])
  def verify(context, salt, token, opts \\ [])
end

defmodule Phoenix.Controller do
  def protect_from_forgery(conn, opts \\ [])
  def put_secure_browser_headers(conn, headers \\ %{})
end

Security and Authentication

Code Generation and Development Tools

Mix tasks for generating boilerplate code and development utilities.

# Mix tasks (command line)
mix phx.gen.html Accounts User users name:string email:string
mix phx.gen.json Accounts User users name:string email:string
mix phx.gen.live Accounts User users name:string email:string
mix phx.gen.auth Accounts User users
mix phx.gen.channel RoomChannel

Code Generation and Development Tools

Testing Support

Test helpers for controllers, channels, and integration testing.

defmodule Phoenix.ConnTest do
  def build_conn(method \\ :get, path \\ "/", params_or_body \\ nil)
  def get(conn, path, params \\ nil)
  def post(conn, path, params \\ nil)
  def json_response(conn, status)
end

defmodule Phoenix.ChannelTest do
  def connect(handler, params, connect_info \\ %{})
  def subscribe_and_join(socket, channel, topic, payload \\ %{})
  def push(socket, event, payload \\ %{})
end

Testing Support

Distributed Presence Tracking

Track user presence across distributed nodes with real-time updates.

defmodule Phoenix.Presence do
  @callback track(Phoenix.Socket.t(), binary, map) :: {:ok, binary}
  @callback list(Phoenix.Socket.t() | binary) :: map

  def track(socket, key, meta)
  def untrack(socket, key)
  def update(socket, key, meta_or_fun)
end

Distributed Presence Tracking

Types

# Core socket type for real-time communication
defmodule Phoenix.Socket do
  defstruct [
    :id, :assigns, :channel, :channel_pid, :endpoint,
    :handler, :joined, :join_ref, :ref, :pubsub_server,
    :topic, :transport, :transport_pid, :serializer
  ]

  @type t :: %__MODULE__{}
end

# Connection type for HTTP requests
@type Plug.Conn.t :: %Plug.Conn{}

# Token options
@type token_opts :: [
  max_age: pos_integer,
  key_iterations: pos_integer,
  key_length: pos_integer,
  key_digest: atom
]

docs

code-generation.md

index.md

presence.md

real-time.md

security.md

testing.md

web-foundation.md

tile.json