SlideShare una empresa de Scribd logo
1 de 24
Descargar para leer sin conexión
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/

Más contenido relacionado

Destacado

Rethink Frontend Development With Elm
Rethink Frontend Development With ElmRethink Frontend Development With Elm
Rethink Frontend Development With ElmBrian Hogan
 
Build Your Own Real-Time Web Service with Elixir Phoenix
Build Your Own Real-Time Web Service with Elixir PhoenixBuild Your Own Real-Time Web Service with Elixir Phoenix
Build Your Own Real-Time Web Service with Elixir PhoenixChi-chi Ekweozor
 
Web Development with CoffeeScript and Sass
Web Development with CoffeeScript and SassWeb Development with CoffeeScript and Sass
Web Development with CoffeeScript and SassBrian Hogan
 
A recommendation engine for your applications - M.Orselli - Codemotion Rome 17
A recommendation engine for your applications - M.Orselli - Codemotion Rome 17A recommendation engine for your applications - M.Orselli - Codemotion Rome 17
A recommendation engine for your applications - M.Orselli - Codemotion Rome 17Codemotion
 

Destacado (8)

Rethink Frontend Development With Elm
Rethink Frontend Development With ElmRethink Frontend Development With Elm
Rethink Frontend Development With Elm
 
Build Your Own Real-Time Web Service with Elixir Phoenix
Build Your Own Real-Time Web Service with Elixir PhoenixBuild Your Own Real-Time Web Service with Elixir Phoenix
Build Your Own Real-Time Web Service with Elixir Phoenix
 
Web Development with CoffeeScript and Sass
Web Development with CoffeeScript and SassWeb Development with CoffeeScript and Sass
Web Development with CoffeeScript and Sass
 
A recommendation engine for your applications - M.Orselli - Codemotion Rome 17
A recommendation engine for your applications - M.Orselli - Codemotion Rome 17A recommendation engine for your applications - M.Orselli - Codemotion Rome 17
A recommendation engine for your applications - M.Orselli - Codemotion Rome 17
 
Embedded Android : System Development - Part I
Embedded Android : System Development - Part IEmbedded Android : System Development - Part I
Embedded Android : System Development - Part I
 
Self-testing Code
Self-testing CodeSelf-testing Code
Self-testing Code
 
Introduction to Elm
Introduction to ElmIntroduction to Elm
Introduction to Elm
 
Elm intro
Elm introElm intro
Elm intro
 

Más de Brian Hogan

Creating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with HugoCreating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with HugoBrian Hogan
 
Automating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and AnsibleAutomating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and AnsibleBrian Hogan
 
Create Development and Production Environments with Vagrant
Create Development and Production Environments with VagrantCreate Development and Production Environments with Vagrant
Create Development and Production Environments with VagrantBrian Hogan
 
Getting Started Contributing To Open Source
Getting Started Contributing To Open SourceGetting Started Contributing To Open Source
Getting Started Contributing To Open SourceBrian Hogan
 
FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.Brian Hogan
 
Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web DesignBrian Hogan
 
Building A Gem From Scratch
Building A Gem From ScratchBuilding A Gem From Scratch
Building A Gem From ScratchBrian Hogan
 
Intro To Advanced Ruby
Intro To Advanced RubyIntro To Advanced Ruby
Intro To Advanced RubyBrian Hogan
 
Turning Passion Into Words
Turning Passion Into WordsTurning Passion Into Words
Turning Passion Into WordsBrian Hogan
 
HTML5 and CSS3 Today
HTML5 and CSS3 TodayHTML5 and CSS3 Today
HTML5 and CSS3 TodayBrian Hogan
 
Web Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To ComplexWeb Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To ComplexBrian Hogan
 
Stop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard LibraryStop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard LibraryBrian Hogan
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Brian Hogan
 
Make GUI Apps with Shoes
Make GUI Apps with ShoesMake GUI Apps with Shoes
Make GUI Apps with ShoesBrian Hogan
 
Story-driven Testing
Story-driven TestingStory-driven Testing
Story-driven TestingBrian Hogan
 
Learning To Walk In Shoes
Learning To Walk In ShoesLearning To Walk In Shoes
Learning To Walk In ShoesBrian Hogan
 
Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009Brian Hogan
 

Más de Brian Hogan (20)

Creating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with HugoCreating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with Hugo
 
Automating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and AnsibleAutomating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and Ansible
 
Create Development and Production Environments with Vagrant
Create Development and Production Environments with VagrantCreate Development and Production Environments with Vagrant
Create Development and Production Environments with Vagrant
 
Docker
DockerDocker
Docker
 
Getting Started Contributing To Open Source
Getting Started Contributing To Open SourceGetting Started Contributing To Open Source
Getting Started Contributing To Open Source
 
FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.
 
Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web Design
 
Building A Gem From Scratch
Building A Gem From ScratchBuilding A Gem From Scratch
Building A Gem From Scratch
 
Intro To Advanced Ruby
Intro To Advanced RubyIntro To Advanced Ruby
Intro To Advanced Ruby
 
Turning Passion Into Words
Turning Passion Into WordsTurning Passion Into Words
Turning Passion Into Words
 
HTML5 and CSS3 Today
HTML5 and CSS3 TodayHTML5 and CSS3 Today
HTML5 and CSS3 Today
 
Web Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To ComplexWeb Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To Complex
 
Stop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard LibraryStop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard Library
 
Intro to Ruby
Intro to RubyIntro to Ruby
Intro to Ruby
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
 
Make GUI Apps with Shoes
Make GUI Apps with ShoesMake GUI Apps with Shoes
Make GUI Apps with Shoes
 
The Why Of Ruby
The Why Of RubyThe Why Of Ruby
The Why Of Ruby
 
Story-driven Testing
Story-driven TestingStory-driven Testing
Story-driven Testing
 
Learning To Walk In Shoes
Learning To Walk In ShoesLearning To Walk In Shoes
Learning To Walk In Shoes
 
Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009
 

Último

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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
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
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
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
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 

Último (20)

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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
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-...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
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 🔝✔️✔️
 
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
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 

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/