Create a gist now

Instantly share code, notes, and snippets.

Phoenix - OpenCode XXII

Phoenix - Elixir Web framework

by

Simon Prévost, Développeur Web chez Mirego

(The slides were made for mdp)


Index

  • Elixir?!
  • Standard framework features
  • Not so standard features

Elixir!

  • Compile to Erlang VM
  • Functional
  • Meta-programming
  • Easy mapping for common tools in Erlang
  • OTP

No time for questions!


  • Routers/Controllers
  • No models. (Whaaaat?)
  • Views
  • Templates

Routers/Controllers

  • Plug-based
  • Pattern match for routes
  • Pattern match for params

Routers/Controllers

Plug

Definition from Plug’s GitHub repository:

A specification for composable modules in between Web applications Connection adapters for different Web servers in the Erlang VM

Also:

Direct interface to the underlying Web server.


Routers/Controllers

Pattern match for routes

resources "/users", UserController

get  "/users",          UserController, :index
get  "/users/:id",      UserController, :show
get  "/users/new",      UserController, :new
post "/users",          UserController, :create
get  "/users/:id/edit", UserController, :edit
put  "/users/:id",      UserController, :update
patch "/users/:id",     UserController, :update
delete "/users/:id",    UserController, :destroy

Routers/Controllers

Pattern match for routes/2

def match(conn, "GET",    ["users"])
def match(conn, "GET",    ["users", id])
def match(conn, "PUT",    ["users", id])
def match(conn, "PATCH",  ["users", id])
def match(conn, "DELETE", ["users", id])

Routers/Controllers

Pattern match for params

defmodule App.UserController do
  use Phoenix.Controller

  before_action :authenticate

  def show(conn, %{"id" => id}) do
    json conn, "{\"user_id\": \"#{id}\"}"
  end
end

View

  • Render template
  • Aka presentor/helper

    defmodule App.UserView do use App.Views

    def display(something) do String.upcase(something) end end


Template

  • Precompiled!
  • Eex, Haml and easily extensible

    def render(conn, "index.html") do """ ... """ end


  • WebSockets
  • Distributed

WebSockets

  • Topics
  • Channels

WebSockets

Topics

PubSub layer used by channels, more on that later ;)


WebSockets

Channels

defmodule App.MyChannel do
  use Phoenix.Channel

    def event(socket, "incoming:all", message) do
      broadcast socket "response:event", %{message: "Echo: " <> message.content}
      socket
    end
  end
end

Distributed

(This is not yet implemented)

  • Inspired by riak_core
  • Distributed Web framework using the Erlang VM

Distributed

(This is not yet implemented, pseudo-code)

Example:

defmodule App.UserController do
  use Phoenix.Controller

  before_action :authenticate

  def show(conn, %{"id" => "me"}) do
    json conn, JSON.encode!(conn.assigns[:current_user])
  end

  def authenticate(conn) do
    conn
    |> assign(:current_user, Service.call(conn, Authenticator, %{"bearer": "1"}))
  end
end

Conclusion

Elixir is awesome

Erlang VM is awesom(er)


Questions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment