SlideShare a Scribd company logo
Phoenix demysitify, with fun
Who am I
@taiansu
|>
|> Ruby
|> React.js, Redux, Rx.js
|> RailsGirls.tw & Elixir.tw
http://taian.su
facebook.com/sutaian
Elixir Taiwan
» Facebook: Elixir Taiwan
» Slack: elixirtw.slack.com
» KKTIX: elixirtw.kktix.cc
Phoenix?
Web MVC Elixir
, web ...
Elixir
Phoenix demysitify, with fun
Phoenix demysitify, with fun
Elixir / Phoenix
Dave Thomas droped by Taiwan
at 2014
Elixir =~
Ruby syntax + Erlang speciality
and some more
Erlang
from 1986
open-sourced in 1998
Build for Telecom switch system
at beginning
» Functional
» Soft real time
» Concurrent
» 99.999% up-time, including upgrade and hot-fix
» Hot update
On the fly update, literally*.
Erlang AR drone in-flight upgrade
Actor model
Compare with system process
» C# process:
300µs per process
50µs per message
» Erlang VM process:
1µs up to 2,500 processes
3µs up to 30,000 processes
0.8µs per message
We do not have ONE web server handling 2 millions sessions.
We have 2 million web servers handling one session each.
» Joe Armstrong
process in Erlang/Elixir
=~
object instance in OO application
with :observer.start
github.com/taiansu/game_of_life_elixir
Weakness
» Hard real time
» Processing big chunk of data (Video, Audio)
» Uncommon syntax
» String
Syntax
-module(mym).
-export([max/2]).
max(X, Y) ->
if
X > Y -> X;
X < Y -> Y;
true -> nil
end.
Erlang string: a list of integer
[1, 2, 3] % [1, 2, 3]
[119, 97, 116, 63, 33] % "wat?!"
<<119, 97, 116, 63, 33>> % Bit-syntax from R16
wat.
Elixir
by José Valim at 2013
Rails core team member
Leverage the Erlang VM
Pattern matching
What's the "=" means, seriously?
Pattern matching basic
# Ruby
x, x, y = [1, 2, 3] # => x = 2, y = 3
# Elixir
[x, x, y] = [1, 2, 3] # => (MatchError) no match of right hand side value
[x, x, y] = [1, 1, 3] # => x = 1, y = 3
[head | tail] = [1, 2, 3, 4] # => head = 1, tail = [2, 3, 4]
{:ok, contents} = File.read("my_app.config")
Pattern matching: recursive
defmodule Factorial do
def calc(0), do: 1
def calc(val), do: val * calc(val - 1)
end
Factorial.calc(100)
Pattern matching with function, contd.
parse_response = fn
{:err, msg} ->
show_error(msg)
{:ok, article = %{title: title, content: content}} ->
# article = %{title: "abc", content: "123"}
# title = "abc"
# content = "123"
render(title, content)
_ ->
IO.puts "Huston, we have a problem."
end
parse_response.(response)
OTP compliant
GenServer, Supervisor, GenEvent, Application
Elixir only
Case: useless variable
request = generate_request
response = get_response(request)
body = parse_body(response, :html)
html = render(body)
# otherwise
html = render(parse_body(get_response(generate_request), :html))
Pipe operator: |>
html = generate_request
|> get_response
|> parse_body(:html)
|> render
Meta-programming
macro, quote and unquote, to AST
live demo ( if enough time )
quote do: 1 + 2 * 3
github.com/taiansu/pipe_to
2
|> Enum.drop(1..10, _)
|> Enum.at(1)
Code with doctest
defmodule Num do
@doc """
Demonstrate doctest feature
## Example
iex> Num.is_even?(1)
true
"""
def is_even?(num) do
rem(num, 2) == 0
end
end
doc
test
Trinity
Phoenix
by Chris McCord
Disclaim
I ❤ Ruby, still.
Phoenix demysitify, with fun
Phoenix is Rails with less magic
Phoenix demysitify, with fun
Phoenix demysitify, with fun
Demysitify: Both are MVC framework with
» Routes
» Controller
» Model
» View
» assets
Core idea of phoenix:
Your entire web application is a function
url is your params, and return value is that html
path = "/order/search?start_date=2017-12-02&end_date=2017-12-03"
html = path
|> router
|> controller
|> model
|> controller
|> view
The conn variable pipe through the whole system
Routes & Controller
Basically the same
Model and ORM
SQL wrapper named Ecto
Ecto
defmodule MyApp.Order
schema "orders" do
belongs_to :user, MyApp.User
has_many :order_items, MyApp.OrderItem
field :code, :string
field :payment_method, :string
end
# query functions here
end
Ecto query functions
def query_cart_of_user(query  __MODULE__, user_id) do
from o in query,
where: o.user_id == ^user_id
end
def order_by_created_at(query  __MODULE__) do
from o in query,
order_by: o.created_at
end
## in Controller
orders = %Order{}
|> query_cart_of_user(20)
|> order_by_created_at
|> Repo.all
View and template
File: template/order/show.html.eex
<h2>
Hi! this is the show page!<%= order_name(@conn, @order) %>
</h2>
File: /view/order_view.ex
defmodule MyApp.OrderView do
use MyApp.Web, :view
def render("show.html", params  []) do
~E{
<h2>Hi! this is the show page!<%= order_name(@conn, @order) %></h2>
}
end
def order_name(conn, order) do
user = conn.assigns[:current_user]
"#{user.name}: #{order.name} x #{order.quantity}"
end
end
Channel
just like ActionCable
Phoenix demysitify, with fun
Conclusion
What I've learned
» Different paradiam expand your horizon
» Which is a knowledge you can bring with you anywhere
» Composability over REUSE
» Think in process way
» And...
Code with Fun
Just like Ruby tought us, thanks Matz.
Code with Functional
Thanks for your time!
Any Questions?
more plans on going, join Elixir.tw!

More Related Content

PPTX
Creating your own framework on top of Symfony2 Components
PDF
Symfony bundle fo asynchronous job processing
PDF
Actor Clustering with Docker Containers and Akka.Net in F#
PDF
Ruby conf 2011, Create your own rails framework
PDF
Elm 0.17 at Dublin Elm Meetup May 2016
PDF
Concurrecny inf sharp
PDF
Akka and futures
PDF
Rails3 changesets
Creating your own framework on top of Symfony2 Components
Symfony bundle fo asynchronous job processing
Actor Clustering with Docker Containers and Akka.Net in F#
Ruby conf 2011, Create your own rails framework
Elm 0.17 at Dublin Elm Meetup May 2016
Concurrecny inf sharp
Akka and futures
Rails3 changesets

What's hot (20)

PPTX
Fullstack Conference - Proxies before proxies: The hidden gems of Javascript...
PDF
Webpack Encore Symfony Live 2017 San Francisco
PDF
Handling external APIs with Elixir - Alex Rozumii
PDF
Back to the futures, actors and pipes: using Akka for large-scale data migration
PDF
TDC2018SP | Trilha Go - Case Easylocus
PPTX
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
PDF
Case EasyLocus
PDF
Some tips to improve developer experience with Symfony
PPTX
From Ruby to Node.js
PPTX
Asynchronous Task Queues with Celery
PDF
3 things you must know to think reactive - Geecon Kraków 2015
PDF
Workshop 17: EmberJS parte II
PDF
Master the New Core of Drupal 8 Now: with Symfony and Silex
PDF
Akka Futures and Akka Remoting
PDF
Active object of Symbian in the lights of client server architecture
PDF
The quest for global design principles (SymfonyLive Berlin 2015)
PDF
Laravel Design Patterns
PDF
Building a chatbot – step by step
Fullstack Conference - Proxies before proxies: The hidden gems of Javascript...
Webpack Encore Symfony Live 2017 San Francisco
Handling external APIs with Elixir - Alex Rozumii
Back to the futures, actors and pipes: using Akka for large-scale data migration
TDC2018SP | Trilha Go - Case Easylocus
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
Case EasyLocus
Some tips to improve developer experience with Symfony
From Ruby to Node.js
Asynchronous Task Queues with Celery
3 things you must know to think reactive - Geecon Kraków 2015
Workshop 17: EmberJS parte II
Master the New Core of Drupal 8 Now: with Symfony and Silex
Akka Futures and Akka Remoting
Active object of Symbian in the lights of client server architecture
The quest for global design principles (SymfonyLive Berlin 2015)
Laravel Design Patterns
Building a chatbot – step by step
Ad

Viewers also liked (16)

PPTX
Nintendo presentation 3.0
PPTX
QCon - 一次 Clojure Web 编程实战
PDF
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
PDF
Java 与 CPU 高速缓存
PDF
PDF
Elixir introd
PDF
Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014
PPTX
Phoenix: Inflame the Web - Alex Troush
PDF
Introducing Elixir the easy way
PDF
Elixir & Phoenix – fast, concurrent and explicit
PDF
Erlang scheduler
PDF
Bottleneck in Elixir Application - Alexey Osipenko
ODP
Elixir basics
PDF
Learn Elixir at Manchester Lambda Lounge
PPTX
Nintendo Case Study
Nintendo presentation 3.0
QCon - 一次 Clojure Web 编程实战
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
Java 与 CPU 高速缓存
Elixir introd
Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014
Phoenix: Inflame the Web - Alex Troush
Introducing Elixir the easy way
Elixir & Phoenix – fast, concurrent and explicit
Erlang scheduler
Bottleneck in Elixir Application - Alexey Osipenko
Elixir basics
Learn Elixir at Manchester Lambda Lounge
Nintendo Case Study
Ad

Similar to Phoenix demysitify, with fun (20)

PDF
Phoenix for Rails Devs
PPTX
RubyConf Bangladesh 2017 - Elixir for Rubyists
PDF
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
PDF
Elixir Into Production
PDF
Re-Design with Elixir/OTP
PDF
Rails vs Web2py
PDF
Introduction to Elixir
PDF
Intro to-rails-webperf
KEY
Código Saudável => Programador Feliz - Rs on Rails 2010
PDF
Building Cloud Castles
PDF
Introducing Elixir and OTP at the Erlang BASH
PDF
Echtzeitapplikationen mit Elixir und GraphQL
PDF
An Introduction to Celery
PDF
Phoenix for laravel developers
PPTX
DDD, CQRS and testing with ASP.Net MVC
PPTX
Tools for Making Machine Learning more Reactive
PDF
Models, controllers and views
PDF
How to disassemble one monster app into an ecosystem of 30
PDF
9 Python programming notes for ktu physics and computer application semester 4
PDF
Refactoring @ Mindvalley: Smells, Techniques and Patterns
Phoenix for Rails Devs
RubyConf Bangladesh 2017 - Elixir for Rubyists
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
Elixir Into Production
Re-Design with Elixir/OTP
Rails vs Web2py
Introduction to Elixir
Intro to-rails-webperf
Código Saudável => Programador Feliz - Rs on Rails 2010
Building Cloud Castles
Introducing Elixir and OTP at the Erlang BASH
Echtzeitapplikationen mit Elixir und GraphQL
An Introduction to Celery
Phoenix for laravel developers
DDD, CQRS and testing with ASP.Net MVC
Tools for Making Machine Learning more Reactive
Models, controllers and views
How to disassemble one monster app into an ecosystem of 30
9 Python programming notes for ktu physics and computer application semester 4
Refactoring @ Mindvalley: Smells, Techniques and Patterns

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Tartificialntelligence_presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Getting Started with Data Integration: FME Form 101
PDF
Machine learning based COVID-19 study performance prediction
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Network Security Unit 5.pdf for BCA BBA.
Univ-Connecticut-ChatGPT-Presentaion.pdf
Spectral efficient network and resource selection model in 5G networks
A comparative study of natural language inference in Swahili using monolingua...
Building Integrated photovoltaic BIPV_UPV.pdf
A Presentation on Artificial Intelligence
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Tartificialntelligence_presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
Heart disease approach using modified random forest and particle swarm optimi...
Reach Out and Touch Someone: Haptics and Empathic Computing
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Spectroscopy.pptx food analysis technology
Programs and apps: productivity, graphics, security and other tools
Mobile App Security Testing_ A Comprehensive Guide.pdf
Getting Started with Data Integration: FME Form 101
Machine learning based COVID-19 study performance prediction
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Phoenix demysitify, with fun