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

Phoenix for Laravel Developers

Tony Messias
December 31, 2017

Phoenix for Laravel Developers

This talk I gave at the 2017 madewithlove retreat (https://2017.mwl.be/) and also at the Maceió DEV Meetup #21 (https://www.meetup.com/pt-BR/maceio-dev-meetup/events/245530346/)

The idea is to compare building the same application in two web frameworks in different languages. One I know and work daily, which is Laravel, and the other one is new to me and I had no experience in Elixir, the language the framework was built in.

The idea is not to compare both languages or frameworks. My goal was mainly learn something new building the same thing twice and see how different languages and frameworks can be used to build similar things.

---

## Slides links

Slide 13:
- https://www.youtube.com/watch?v=7Zlp9rKHGD4

Slide 18:
- https://www.youtube.com/watch?v=aA-XHI-EYcM

Slide 20:
- https://github.com/tonysm/slackish-laravel
- https://github.com/tonysm/slackish-phoenix

Slide 32:
- http://phoenixframework.org/blog/the-road-to-2-million-websocket-connections

Slide 38:
- https://www.youtube.com/watch?v=H686MDn4Lo8

---

## References for this talk

Functional Programming; What? Why? When? (Robert C Martin) https://www.youtube.com/watch?v=7Zlp9rKHGD4
Real World Elixir Deployment (Pete Gamache) https://www.youtube.com/watch?v=H686MDn4Lo8
Erlang: The Movie https://www.youtube.com/watch?v=xrIjfIjssLE
Lonestar ElixirConf 2017- KEYNOTE: Phoenix 1.3 by Chris McCord https://www.youtube.com/watch?v=tMO28ar0lW8
GOTO 2016 • Phoenix a Web Framework for the New Web • José Valim https://www.youtube.com/watch?v=bk3icU8iIto
ElixirConf 2016 - Giving up the Object-Oriented Ghost by Morgan Lanco https://www.youtube.com/watch?v=_VpZ6gQsyDY
GOTO 2017 • Elixir: The only Sane Choice in an Insane World • Brian Cardarella https://www.youtube.com/watch?v=gom6nEvtl3U
Elixir, quem é esse pokemon? - Bruno Volcov https://www.youtube.com/watch?v=aA-XHI-EYcM
Ecto, você sabe o que é? - Amanda Sposito https://www.youtube.com/watch?v=hQM4VdEpz6g
Programming Phoenix (book) by Chris McCord, Bruce Tate, and José Valim https://pragprog.com/book/phoenix/programming-phoenix

Tony Messias

December 31, 2017
Tweet

More Decks by Tony Messias

Other Decks in Programming

Transcript

  1. In fact, I'm actually showing that you can build shiny

    things with tools you already know.
  2. What is a function? • In math, functions are all

    about transforming input to output (sounds familiar?) • You might be used to expressing functions in math like so: y = x * 2 or g = x ^ 2 • But there is another notation that is much more expressive (IMO): f(x) = x * 2 f(5) = 5 * 2 f(5) = 10
  3. f(x) = x * 2 --- const f = (x)

    => x * 2 var x = 5 assertEquals(f(x), 10) assertEquals(f(x), f(x)) source
  4. class Value { constructor(val) { this.value = val; } }

    const f = (x) => x.value++ * 2 const x = new Value(5); console.log(f(x) == f(x)); // True or False? console.log(x.value); // What is the value?
  5. class Value { constructor(val) { this.value = val; } }

    const f = (x) => x.value++ * 2 const x = new Value(5); console.log(f(x) == f(x)); // false console.log(x.value); // 7
  6. defmodule SlackishphxWeb.CompanyController do use SlackishphxWeb, :controller plug SlackishphxWeb.Plugs.RequireAuth def create(conn,

    %{"company" => params}) do case Slackishphx.Auth.create_company(conn.assigns[:user], params) do {:ok, company} -> conn |> create_default_channel(company) |> switch_company(conn.assigns[:user], company) {:error, changeset} -> conn |> render("new.html", changeset: changeset) end end end
  7. defmodule SlackishphxWeb.CompanyController do use SlackishphxWeb, :controller plug SlackishphxWeb.Plugs.RequireAuth def create(conn,

    %{"company" => params}) do case Slackishphx.Auth.create_company(conn.assigns[:user], params) do {:ok, company} -> conn |> create_default_channel(company) |> switch_company(conn.assigns[:user], company) {:error, changeset} -> conn |> render("new.html", changeset: changeset) end end end
  8. defmodule SlackishphxWeb.CompanyController do use SlackishphxWeb, :controller plug SlackishphxWeb.Plugs.RequireAuth def create(conn,

    %{"company" => params}) do case Slackishphx.Auth.create_company(conn.assigns[:user], params) do {:ok, company} -> conn |> create_default_channel(company) |> switch_company(conn.assigns[:user], company) {:error, changeset} -> conn |> render("new.html", changeset: changeset) end end end
  9. MVC

  10. slackishphx/lib ├── slackishphx │ ├── application.ex │ ├── auth │

    │ ├── auth.ex │ │ ├── company.ex │ │ └── user.ex │ ├── chat │ │ ├── channel.ex │ │ └── chat.ex │ └── repo.ex ├── slackishphx.ex ├── slackishphx_web │ ├── channels │ ├── controllers │ ├── endpoint.ex │ ├── gettext.ex │ ├── plugs │ ├── router.ex │ ├── templates │ └── views └── slackishphx_web.ex
  11. slackishphx/lib ├── slackishphx │ ├── application.ex │ ├── auth │

    │ ├── auth.ex │ │ ├── company.ex │ │ └── user.ex │ ├── chat │ │ ├── channel.ex │ │ └── chat.ex │ └── repo.ex ├── slackishphx.ex ├── slackishphx_web │ ├── channels │ ├── controllers │ ├── endpoint.ex │ ├── gettext.ex │ ├── plugs │ ├── router.ex │ ├── templates │ └── views └── slackishphx_web.ex
  12. slackishphx/lib ├── slackishphx │ ├── application.ex │ ├── auth │

    │ ├── auth.ex │ │ ├── company.ex │ │ └── user.ex │ ├── chat │ │ ├── channel.ex │ │ └── chat.ex │ └── repo.ex ├── slackishphx.ex ├── slackishphx_web │ ├── channels │ ├── controllers │ ├── endpoint.ex │ ├── gettext.ex │ ├── plugs │ ├── router.ex │ ├── templates │ └── views └── slackishphx_web.ex
  13. defmodule Slackishphx.Auth.User do use Ecto.Schema # ... schema "users" do

    field :name, :string field :email, :string field :google_id, :string field :google_token, :string field :avatar_path, :string belongs_to :current_company, Company has_many :companies, Company, foreign_key: :owner_id timestamps() end end
  14. defmodule Slackishphx.Auth.User do # ... @doc false def register_from_google_changeset(%User{} =

    user, attrs) do user |> cast(attrs, [:name, :email, :google_id, ...]) |> validate_required([:name, :email, ...]) end end
  15. defmodule Slackishphx.Auth do @moduledoc """ The Auth context. """ def

    create_company(%User{} = user, params) do changeset = user |> build_assoc(:companies) |> Company.create_company_changeset(params) Repo.insert(changeset) end end
  16. Comparison Laravel (PHP) Phoenix (Elixir) Webapp (sessions) API (everything needs

    to go through HTTP) Pusher (websockets) Worker (broadcasting is delivered here) Database (sqlite) Queue Webapp (sessions) Channels (websockets) Database (sqlite)
  17. Some similarities broadcast(new NewMessage( $request->user(), $channel, $request->input('content'), $request->input('uuid'), Carbon::now() ));

    broadcast!(socket, "rooms:#{room_id}:new", %{ message: message, user: user, uuid: uuid, time: DateTime.utc_now } )
  18. Improvement points to Laravel Broadcast::channel('App.User.{id}', function ($user, $id) { return

    (int) $user->id === (int) $id; }); Broadcast::channel('channels.{channel}', function (\App\User $user, \App\Channel $channel) { if (!$channel->exists || !$user->canJoin($channel)) { return false; } $user->joinChannel($channel); return [ 'id' => $user->id, 'name' => $user->name, 'avatar_path' => $user->avatar_path, ]; }); channel "companies:*", SlackishphxWeb.CompanyChannel channel "rooms:*", SlackishphxWeb.RoomChannel
  19. References • Functional Programming; What? Why? When? (Robert C Martin)

    https://www.youtube.com/watch?v=7Zlp9rKHGD4 • Real World Elixir Deployment (Pete Gamache) https://www.youtube.com/watch?v=H686MDn4Lo8 • Erlang: The Movie https://www.youtube.com/watch?v=xrIjfIjssLE • Lonestar ElixirConf 2017- KEYNOTE: Phoenix 1.3 by Chris McCord https://www.youtube.com/watch?v=tMO28ar0lW8 • GOTO 2016 • Phoenix a Web Framework for the New Web • José Valim https://www.youtube.com/watch?v=bk3icU8iIto
  20. References • ElixirConf 2016 - Giving up the Object-Oriented Ghost

    by Morgan Lanco https://www.youtube.com/watch?v=_VpZ6gQsyDY • GOTO 2017 • Elixir: The only Sane Choice in an Insane World • Brian Cardarella https://www.youtube.com/watch?v=gom6nEvtl3U • Elixir, quem é esse pokemon? - Bruno Volcov https://www.youtube.com/watch?v=aA-XHI-EYcM • Ecto, você sabe o que é? - Amanda Sposito https://www.youtube.com/watch?v=hQM4VdEpz6g • Programming Phoenix (book) by Chris McCord, Bruce Tate, and José Valim https://pragprog.com/book/phoenix/programming-phoenix