On Github boulder-elixir / PhoenixPresentation
Created by Josh Bavari / @jbavari
mix local.hex
mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez
mix phoenix.new hello_phoenix
Phoenix makes explicit calls with pipes
lters data along the way, and sends response
connection
|> phoenix
=> Response
connection
|> endpoint
|> router
|> pipelines
|> controller
|> action
|> view
|> template
A look at an endpoint
defmodule MyApp.Endpoint do
use Phoenix.Endpoint, otp_app: :MyApp
socket "/socket", MyApp.UserSocket
# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phoenix.digest
# when deploying your static files in production.
plug Plug.Static,
at: "/", from: :MyApp, gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt)
# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Phoenix.LiveReloader
plug Phoenix.CodeReloader
end
plug Plug.RequestId
plug Plug.Logger
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Poison
plug Plug.MethodOverride
plug Plug.Head
plug Plug.Session,
store: :cookie,
key: "_MyApp_key",
signing_salt: "PD+nvtps"
plug MyApp.Router
end
A look at a router
defmodule MyApp.Router do
use MyApp.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", MyApp do
pipe_through :browser # Use the default browser stack
resources "/user", UsersController
resources "/songs", SongController
get "/", PageController, :index
end
# Other scopes may use custom stacks.
# scope "/api", MyApp do
# pipe_through :api
# end
end
# From channel itself:
broadcast! socket, "new:msg", %{user: msg["user"], body: msg["body"]}
# From IEx / Controller
Chat.Endpoint.broadcast("rooms:lobby", "new:msg", %{message: "hello from the console"})
defmodule MyApp do
use Application
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
# Start the endpoint when the application starts
supervisor(MyApp.Endpoint, []),
# Start the Ecto repository
worker(MyApp.Repo, []),
# Here you could define other workers and supervisors as children
worker(MyApp.BackgroundWorker, [arg1, arg2, arg3]),
worker(MyApp.Cleanup, [arg1, arg2, arg3]),
worker(MyApp.Emailer, [arg1, arg2, arg3]),
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
MyApp.Endpoint.config_change(changed, removed)
:ok
end
end
Many of the things you need to add into Node / Rails / Etc has been in Erlang for 20+ years. State machines, process watching / restarting, pattern matching, and stateful processes are available now!
We'll take a peek at the Phoenix chat example