SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
Realtime Web Apps
with Elixir and Phoenix
Brian P. Hogan
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Today
• Build a simple text-collaboration web app
• Explore a little Elixir
• Explore some ES6.
Today you’ll learn about what Phoenix is and how it works as we work through a web application together. You’ll also learn a little about the Elixir
language, and we’ll see some ES6 too, since Phoenix supports that out of the box.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Me?
• I love to make web stuff
• I love to make music
• I love to teach
So, who am I? I’m a web developer and a teacher. I’m also a book author. I’ve been working with Phoenix for about 6 months now and Elixir for just under
a year. I do most of my primary development with Ruby on Rails.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
• A functional programming language.
• Built on Erlang’s virtual machine.
• Multiprocessor and fast.
• Elegant.
So what’s Elixir? It’s a functional programming language that’s really new. It borrows some syntactical things from Ruby but leverages the speed and
stability of Erlang. It’s a very powerful language because it can also give us access to the vast array of Erlang libraries available.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
• An MVC web framework
• Built for the “internet of things”
• Scalable and fast!
• Fault-tolerant
• Built-in support for web sockets
Phoenix is a web framework created by Chris McCord, designed to handle many connections and be scalable and fault tolerant. It’s nearing version 1.0
and has some pretty robust features including out-of-the-box support for web sockets.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Our App
Today I’m going to build a small application that lets us create a text editor that broadcasts its content across the web. Anyone with the editor can type
text and push that to any other connected editor.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Getting Elixir and Phoenix
• Install Elixir from http://elixir-lang.org/install.html
• Install Phoenix from http://phoenixframework.org
• $ mix phoenix.new <path_to_app>
• $ cd <path_to_app>
• $ mix phoenix.server
To get started with Phoenix, you first need to install Elixir, which will have you install Erlang. Then you need to install Phoenix, which isn’t terribly hard now,
but will get much easier in about a month. Just follow the directions on the Phoenix web site for now.
Once you have things installed you run a command to create a new app, then change to the new directory that ets created and start the server.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
The whole process looks like this. The task creates the folder structure for you and sets up dependencies. Phoenix apps use Brunch to manage the client-
side assets, which means we have support for Sass and Babel built in.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Phoenix
Router
Controller Controller
View View
Template Template Template
Channel
Phoenix supports the traditional MVC pattern on the web, where we have a router route requests to a controller which then prepares a view and renders a
template. In Phoenix, Views control what gets rendered, and Templates are what people see.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Phoenix
Router
Controller Controller
View View
Template Template Template
Channel
We can also have the view render some data like JSON or XML without using a template by creating our own custom handlers.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Phoenix
Router
Controller Controller
View View
Template Template Template
Channel
Phoenix also has Channels, which make it possible to send realtime data to connected clients using Websockets. Those clients can send data back to these
channels in realtime as well. Clients subscribe to a Topic within a channel, and the server manages the connections.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Elixir Basics
• Basic Types
• Pattern Matching
• Functions
• Piping
Let’s look at the basics of Elixir. This isn’t an Elixir talk, but we have some time while our app does its initial compilation.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Basic Types
1 # integer

0x1F # integer

1.0 # float

true # boolean

:atom # atom / symbol

"elixir" # string

[1, 2, 3] # list

{:ok, “200 OK”} # tuple
Elixir has these data types, and a few more that we’re not gonna talk about today. It has integers, floating point numbers, booleans, atoms, which are just
pieces of data we can use to label things. It also has strings. Finally, it has lists, which act like arrays, and tuples. Lists are linked-lists, so modifying them is
easy if we add to the front, and tuples are usually reserved for fixed sets of data. Lots of functions return tuples in Elixir.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Pattern Matching
# Variable assignment

age = 42
Elixir makes heavy use of pattern matching. First, it uses pattern matching to assign variables. When we assign the value of 42 to the “age” variable, Elixiir
figures out how to make the left hand side match the right hand
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Pattern Matching
# Variable assignment

age
# We can destructure lists

[first, second, third] = [1,2,3]
We can use that to break apart lists into individual variables. We just assign a list of variables to a list of values.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Pattern Matching
# Variable assignment

age
# Many functions return tuples we can parse

{number, junk} = Float.parse("123abc")
# We can destructure lists

[first, second, third]
This works for tuples too. The Float.parse function returns a two-value tuple containing the numerical piece followed by the non-numerical part. We can
capture that result into variables so we can easily discard the piece we don’t care about.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Pattern Matching
# Variable assignment

age
# We can even pattern match strings

"/products/" <> id = "/products/1234"
# Many functions return tuples we can parse

{number, junk}
# We can destructure lists

[first, second, third]
Finally, we can even do this with strings. We can pluck out part of a string. The <> is just the string concatenation character.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Functions
# A simple function

add = fn(first, second) -> first + second end



# A simple function using pattern matching

tax = fn

(amount, "WI") -> amount * 0.05

(amount, "MN") -> amount * 0.06

(amount, 0) -> 0

end

We can define some quick functions using the fn keyword. We can even define functions that match different incoming arguments. Depending on which
signature matches, the function will run the appropriate code.
In Elixir, the last expression executed is the return value. No need for explicit returning.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Functions
defmodule Math do

def add(first, second) do

first + second

end

end



defmodule Tax do

def calculate(amount, "WI"), do: amount * 0.05

def calculate(amount, "MN"), do: amount * 0.06

def calculate(amount, state), do: amount

end
Our functions are usually organized into modules. We can define methods with a block syntax, or, if the method body only has one line, we can use a
shorter one-line syntax. It’s our choice.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Piping
# Add up only even numbers

Enum.reduce(Enum.filter([1,2,3,4,5,6], fn(x) ->
rem(x, 2) == 0 end), 0, fn(x, acc) -> x + acc end )



Let’s add up all the even numbers in a list. First, we have to filter out all the even numbers by using Elixir’s Enum.filter function. Then we use Enum.reduce
to sum up the elements in the list. Filter and reduce are pretty common nowadays, as you can use them in JavaScript, Ruby, and many other languages.
But this is pretty hard to read. We could resort to using intermediate variables, but that isn’t the “functional” way. We want to make data flow through our
program without using state unless we have no choice. And we do have a choice here.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Piping
# Add up only even numbers

Enum
rem(x,


# Piping cleans this up

[1,2,3,4,5,6]

|> Enum.filter(fn(x) -> rem(x, 2) == 0 end)

|> Enum.reduce(0, fn(x, acc) -> x + acc end)
But we can rearrange this using Pipes. In Elixir, we can pipe the output of one function to the first argument of another function. This lets us express our
algorithms as a series of steps instead of a series of nested parentheses, resulting in more readable code.
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Let’s Build An App!
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Where to go next?
• Programming Elixir (https://pragprog.com/book/
elixir/programming-elixir)
• #elixir-lang on IRC (Freenode)
• Phoenix Guides (http://www.phoenixframework.org/
v0.11.0/docs/overview)
Brian P. Hogan
@bphogan
http://bphogan.com/presentations/phoenix
Thanks!
• Twitter: @bphogan
• http://spkr8.com/t/57991
http://webdevelopmentrecipes.com/

Weitere ähnliche Inhalte

Andere mochten auch (14)

Growth Hacking
Growth Hacking Growth Hacking
Growth Hacking
 
Google's Avinash Kaushik on Web Analytics
Google's Avinash Kaushik on Web AnalyticsGoogle's Avinash Kaushik on Web Analytics
Google's Avinash Kaushik on Web Analytics
 
Datomic
DatomicDatomic
Datomic
 
Jim rohn
Jim  rohnJim  rohn
Jim rohn
 
Tesco
TescoTesco
Tesco
 
Datomic
DatomicDatomic
Datomic
 
Datomic
DatomicDatomic
Datomic
 
Waldorf Education
Waldorf EducationWaldorf Education
Waldorf Education
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Tesco
TescoTesco
Tesco
 
Selena Gomez
Selena GomezSelena Gomez
Selena Gomez
 
Sap fiori
Sap fioriSap fiori
Sap fiori
 
French Property Market 2014
French Property Market 2014French Property Market 2014
French Property Market 2014
 
ReactJs
ReactJsReactJs
ReactJs
 

Kürzlich hochgeladen

Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 

Kürzlich hochgeladen (20)

Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 

Realtime Web Apps With Elixir And Phoenix

  • 1. Realtime Web Apps with Elixir and Phoenix Brian P. Hogan
  • 2. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Today • Build a simple text-collaboration web app • Explore a little Elixir • Explore some ES6. Today you’ll learn about what Phoenix is and how it works as we work through a web application together. You’ll also learn a little about the Elixir language, and we’ll see some ES6 too, since Phoenix supports that out of the box.
  • 3. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Me? • I love to make web stuff • I love to make music • I love to teach So, who am I? I’m a web developer and a teacher. I’m also a book author. I’ve been working with Phoenix for about 6 months now and Elixir for just under a year. I do most of my primary development with Ruby on Rails.
  • 4. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix • A functional programming language. • Built on Erlang’s virtual machine. • Multiprocessor and fast. • Elegant. So what’s Elixir? It’s a functional programming language that’s really new. It borrows some syntactical things from Ruby but leverages the speed and stability of Erlang. It’s a very powerful language because it can also give us access to the vast array of Erlang libraries available.
  • 5. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix • An MVC web framework • Built for the “internet of things” • Scalable and fast! • Fault-tolerant • Built-in support for web sockets Phoenix is a web framework created by Chris McCord, designed to handle many connections and be scalable and fault tolerant. It’s nearing version 1.0 and has some pretty robust features including out-of-the-box support for web sockets.
  • 6. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Our App Today I’m going to build a small application that lets us create a text editor that broadcasts its content across the web. Anyone with the editor can type text and push that to any other connected editor.
  • 7. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Getting Elixir and Phoenix • Install Elixir from http://elixir-lang.org/install.html • Install Phoenix from http://phoenixframework.org • $ mix phoenix.new <path_to_app> • $ cd <path_to_app> • $ mix phoenix.server To get started with Phoenix, you first need to install Elixir, which will have you install Erlang. Then you need to install Phoenix, which isn’t terribly hard now, but will get much easier in about a month. Just follow the directions on the Phoenix web site for now. Once you have things installed you run a command to create a new app, then change to the new directory that ets created and start the server.
  • 8. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix The whole process looks like this. The task creates the folder structure for you and sets up dependencies. Phoenix apps use Brunch to manage the client- side assets, which means we have support for Sass and Babel built in.
  • 9. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Phoenix Router Controller Controller View View Template Template Template Channel Phoenix supports the traditional MVC pattern on the web, where we have a router route requests to a controller which then prepares a view and renders a template. In Phoenix, Views control what gets rendered, and Templates are what people see.
  • 10. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Phoenix Router Controller Controller View View Template Template Template Channel We can also have the view render some data like JSON or XML without using a template by creating our own custom handlers.
  • 11. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Phoenix Router Controller Controller View View Template Template Template Channel Phoenix also has Channels, which make it possible to send realtime data to connected clients using Websockets. Those clients can send data back to these channels in realtime as well. Clients subscribe to a Topic within a channel, and the server manages the connections.
  • 12. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Elixir Basics • Basic Types • Pattern Matching • Functions • Piping Let’s look at the basics of Elixir. This isn’t an Elixir talk, but we have some time while our app does its initial compilation.
  • 13. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Basic Types 1 # integer
 0x1F # integer
 1.0 # float
 true # boolean
 :atom # atom / symbol
 "elixir" # string
 [1, 2, 3] # list
 {:ok, “200 OK”} # tuple Elixir has these data types, and a few more that we’re not gonna talk about today. It has integers, floating point numbers, booleans, atoms, which are just pieces of data we can use to label things. It also has strings. Finally, it has lists, which act like arrays, and tuples. Lists are linked-lists, so modifying them is easy if we add to the front, and tuples are usually reserved for fixed sets of data. Lots of functions return tuples in Elixir.
  • 14. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Pattern Matching # Variable assignment
 age = 42 Elixir makes heavy use of pattern matching. First, it uses pattern matching to assign variables. When we assign the value of 42 to the “age” variable, Elixiir figures out how to make the left hand side match the right hand
  • 15. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Pattern Matching # Variable assignment
 age # We can destructure lists
 [first, second, third] = [1,2,3] We can use that to break apart lists into individual variables. We just assign a list of variables to a list of values.
  • 16. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Pattern Matching # Variable assignment
 age # Many functions return tuples we can parse
 {number, junk} = Float.parse("123abc") # We can destructure lists
 [first, second, third] This works for tuples too. The Float.parse function returns a two-value tuple containing the numerical piece followed by the non-numerical part. We can capture that result into variables so we can easily discard the piece we don’t care about.
  • 17. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Pattern Matching # Variable assignment
 age # We can even pattern match strings
 "/products/" <> id = "/products/1234" # Many functions return tuples we can parse
 {number, junk} # We can destructure lists
 [first, second, third] Finally, we can even do this with strings. We can pluck out part of a string. The <> is just the string concatenation character.
  • 18. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Functions # A simple function
 add = fn(first, second) -> first + second end
 
 # A simple function using pattern matching
 tax = fn
 (amount, "WI") -> amount * 0.05
 (amount, "MN") -> amount * 0.06
 (amount, 0) -> 0
 end
 We can define some quick functions using the fn keyword. We can even define functions that match different incoming arguments. Depending on which signature matches, the function will run the appropriate code. In Elixir, the last expression executed is the return value. No need for explicit returning.
  • 19. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Functions defmodule Math do
 def add(first, second) do
 first + second
 end
 end
 
 defmodule Tax do
 def calculate(amount, "WI"), do: amount * 0.05
 def calculate(amount, "MN"), do: amount * 0.06
 def calculate(amount, state), do: amount
 end Our functions are usually organized into modules. We can define methods with a block syntax, or, if the method body only has one line, we can use a shorter one-line syntax. It’s our choice.
  • 20. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Piping # Add up only even numbers
 Enum.reduce(Enum.filter([1,2,3,4,5,6], fn(x) -> rem(x, 2) == 0 end), 0, fn(x, acc) -> x + acc end )
 
 Let’s add up all the even numbers in a list. First, we have to filter out all the even numbers by using Elixir’s Enum.filter function. Then we use Enum.reduce to sum up the elements in the list. Filter and reduce are pretty common nowadays, as you can use them in JavaScript, Ruby, and many other languages. But this is pretty hard to read. We could resort to using intermediate variables, but that isn’t the “functional” way. We want to make data flow through our program without using state unless we have no choice. And we do have a choice here.
  • 21. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Piping # Add up only even numbers
 Enum rem(x, 
 # Piping cleans this up
 [1,2,3,4,5,6]
 |> Enum.filter(fn(x) -> rem(x, 2) == 0 end)
 |> Enum.reduce(0, fn(x, acc) -> x + acc end) But we can rearrange this using Pipes. In Elixir, we can pipe the output of one function to the first argument of another function. This lets us express our algorithms as a series of steps instead of a series of nested parentheses, resulting in more readable code.
  • 23. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Where to go next? • Programming Elixir (https://pragprog.com/book/ elixir/programming-elixir) • #elixir-lang on IRC (Freenode) • Phoenix Guides (http://www.phoenixframework.org/ v0.11.0/docs/overview)
  • 24. Brian P. Hogan @bphogan http://bphogan.com/presentations/phoenix Thanks! • Twitter: @bphogan • http://spkr8.com/t/57991 http://webdevelopmentrecipes.com/