Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Phoenix

 Phoenix

LambdaZg Meetup

Saša Jurić

November 25, 2015
Tweet

More Decks by Saša Jurić

Other Decks in Programming

Transcript

  1. Phoenix
    @sasajuric

    View Slide

  2. concurrency
    scalability
    fault tolerance
    predictable latency
    distributed systems
    high availability

    View Slide

  3. “MVC”
    router
    sessions
    assets
    real-time communication
    dev productivity

    View Slide

  4. Phoenix/Elixir
    Nginx
    Redis
    Sidekick/Resque
    Cron
    theerlangelist.com

    View Slide

  5. OTP
    Erlang VM (BEAM)
    Cowboy
    Elixir
    Plug
    Phoenix
    Ecto

    View Slide

  6. defmodule Math do
    def sum(x, y) do
    x + y
    end
    end
    Math.sum(2, 3)

    View Slide

  7. m1 = Map.new
    m2 = Map.put(m1, :x, 1)
    m3 = Map.put(m2, :y, 2)

    View Slide

  8. bar(foo(x), y, z)
    foo(x) |> bar(y, z)

    View Slide

  9. a |> b |> c
    c(b(a))

    View Slide

  10. a
    |> b
    |> c

    View Slide

  11. Map.new
    |> Map.put(:x, 1)
    |> Map.put(:y, 2)
    m1 = Map.new
    m2 = Map.put(m1, :x, 1)
    m3 = Map.put(m2, :y, 2)

    View Slide

  12. defmodule MyRouter do
    get "/some_path", MyModule, :some_fun
    end
    defmodule MyRouter do
    def route(conn, "GET", "/some_path") do
    MyModule.some_fun(conn)
    end
    end

    View Slide

  13. defmodule MyRouter do
    use Phoenix.Router
    get "/some_path", MyModule, :some_fun
    end

    View Slide

  14. scheduler scheduler scheduler scheduler
    CPU CPU CPU CPU
    VM

    View Slide

  15. Plug.Adapters.Cowboy.http(
    MyServer,
    [],
    port: 5454
    )

    View Slide

  16. %Plug.Conn{
    remote_ip: {151, 236, 219, 228},
    host: "www.example.com",
    request_path: "/bar/baz",
    method: "GET",
    # ...
    }

    View Slide

  17. defmodule MyServer do
    def init(opts), do: opts
    def call(conn, _opts) do
    conn
    |> Plug.Conn.put_resp_header("foo", "bar")
    |> Plug.Conn.put_resp_cookie("baz", "42")
    |> Plug.Conn.resp(200, "Hi!")
    end
    end

    View Slide

  18. listener
    request 1 request 2 request 3
    conn
    |>...
    conn
    |>...
    conn
    |>...

    View Slide

  19. defmodule MyServer do
    use Plug.Builder
    plug Plug.RequestId
    plug Plug.Logger
    plug Plug.Static, at: "/static", from: "/folder"
    plug MyAuth
    plug :my_handle
    def my_handle(conn, opts) do
    # ...
    end
    end

    View Slide

  20. Endpoint
    Router
    Controller View
    conn
    Model

    View Slide

  21. $ mix phoenix.new hello --no-ecto
    # ...
    Fetch and install dependencies? [Yn]
    * running npm install && node node_modules/brunch/bin/brunch build
    * running mix deps.get
    We are all set! Run your Phoenix application:
    $ cd hello
    $ mix phoenix.server
    You can also run your app inside IEx (Interactive Elixir) as:
    $ iex -S mix phoenix.server

    View Slide

  22. client server
    join "room:23"
    send "room:23", "Hello"
    send "room:23", "Hi"
    Socket

    View Slide

  23. socket
    channel
    client
    channel
    PubSub

    View Slide

  24. PubSub
    server 1
    server 2
    server 3

    View Slide

  25. maturity
    CPU bound tasks
    learning curve

    View Slide

  26. elixir-lang.org
    Getting started

    Mix & OTP
    www.phoenixframework.org

    View Slide

  27. View Slide