SlideShare uma empresa Scribd logo
1 de 45
Baixar para ler offline
Tony Messias
Maceió DEV Meetup #21
Phoenix for Laravel
developers
This is not a hands-on talk.
I'm not going to teach you Elixir (or
Phoenix) in 30~ish minutes.
I'm not saying that one is
better than the other.
In fact, I'm actually showing that you can
build shiny things with tools you already
know.
This talk in a mind-map.
Functional
Programming
Functional
Programming
Haven't the Gods of programming decided
that OOP has won this battle?
Why is functional programming
in such a “hype” now?
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
f(x) = x * 2
---
const f = (x) => x * 2
var x = 5
assertEquals(f(x), 10)
assertEquals(f(x), f(x))
source
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?
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
Immutability ease
distributing computation.
Concurrency
and
Parallelism
Parallelism Concurrency
source
before I forget...
Slack'ish in Laravel
tonysm/slackish-laravel
Slack'ish in Phoenix
tonysm/slackish-phoenix
Elixir, the good parts
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
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
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
Phoenix, the good parts
MVC
Phoenix is not your application.
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
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
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
Phoenix Channels
http://phoenixframework.org/blog/the-road-to-2-million-websocket-connections
Ecto
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
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
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
The not so good parts...
deployment
source
Comparisons*
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)
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
}
)
Improvement points to Laravel
Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('channels.{channel}', function (AppUser
$user, AppChannel $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
That's it!
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
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

Mais conteúdo relacionado

Mais procurados

PofEAA and SQLAlchemy
PofEAA and SQLAlchemyPofEAA and SQLAlchemy
PofEAA and SQLAlchemyInada Naoki
 
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript SecurityJohannes Hoppe
 
SQLAlchemy Core: An Introduction
SQLAlchemy Core: An IntroductionSQLAlchemy Core: An Introduction
SQLAlchemy Core: An IntroductionJason Myers
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I thinkWim Godden
 
Unbreakable: The Craft of Code
Unbreakable: The Craft of CodeUnbreakable: The Craft of Code
Unbreakable: The Craft of CodeJoe Morgan
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFabio Collini
 
Introduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic MigrationsIntroduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic MigrationsJason Myers
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wildJoe Morgan
 
Telephone billing system in c++
Telephone billing system in c++Telephone billing system in c++
Telephone billing system in c++vikram mahendra
 
Introduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMIntroduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMJason Myers
 
20180310 functional programming
20180310 functional programming20180310 functional programming
20180310 functional programmingChiwon Song
 
The Ring programming language version 1.2 book - Part 30 of 84
The Ring programming language version 1.2 book - Part 30 of 84The Ring programming language version 1.2 book - Part 30 of 84
The Ring programming language version 1.2 book - Part 30 of 84Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 41 of 181
The Ring programming language version 1.5.2 book - Part 41 of 181The Ring programming language version 1.5.2 book - Part 41 of 181
The Ring programming language version 1.5.2 book - Part 41 of 181Mahmoud Samir Fayed
 
Error Management: Future vs ZIO
Error Management: Future vs ZIOError Management: Future vs ZIO
Error Management: Future vs ZIOJohn De Goes
 
20180721 code defragment
20180721 code defragment20180721 code defragment
20180721 code defragmentChiwon Song
 
UI Framework Development using GWT and HTML Canvas - By Iarosla Kobyliukh
UI Framework Development using GWT and HTML Canvas - By Iarosla KobyliukhUI Framework Development using GWT and HTML Canvas - By Iarosla Kobyliukh
UI Framework Development using GWT and HTML Canvas - By Iarosla KobyliukhGWTcon
 
Curso Symfony - Clase 2
Curso Symfony - Clase 2Curso Symfony - Clase 2
Curso Symfony - Clase 2Javier Eguiluz
 

Mais procurados (19)

PofEAA and SQLAlchemy
PofEAA and SQLAlchemyPofEAA and SQLAlchemy
PofEAA and SQLAlchemy
 
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
 
SQLAlchemy Core: An Introduction
SQLAlchemy Core: An IntroductionSQLAlchemy Core: An Introduction
SQLAlchemy Core: An Introduction
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I think
 
Unbreakable: The Craft of Code
Unbreakable: The Craft of CodeUnbreakable: The Craft of Code
Unbreakable: The Craft of Code
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
 
Introduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic MigrationsIntroduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic Migrations
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wild
 
Telephone billing system in c++
Telephone billing system in c++Telephone billing system in c++
Telephone billing system in c++
 
Introduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMIntroduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORM
 
20180310 functional programming
20180310 functional programming20180310 functional programming
20180310 functional programming
 
The Ring programming language version 1.2 book - Part 30 of 84
The Ring programming language version 1.2 book - Part 30 of 84The Ring programming language version 1.2 book - Part 30 of 84
The Ring programming language version 1.2 book - Part 30 of 84
 
The Ring programming language version 1.5.2 book - Part 41 of 181
The Ring programming language version 1.5.2 book - Part 41 of 181The Ring programming language version 1.5.2 book - Part 41 of 181
The Ring programming language version 1.5.2 book - Part 41 of 181
 
Error Management: Future vs ZIO
Error Management: Future vs ZIOError Management: Future vs ZIO
Error Management: Future vs ZIO
 
Django quickstart
Django quickstartDjango quickstart
Django quickstart
 
20180721 code defragment
20180721 code defragment20180721 code defragment
20180721 code defragment
 
UI Framework Development using GWT and HTML Canvas - By Iarosla Kobyliukh
UI Framework Development using GWT and HTML Canvas - By Iarosla KobyliukhUI Framework Development using GWT and HTML Canvas - By Iarosla Kobyliukh
UI Framework Development using GWT and HTML Canvas - By Iarosla Kobyliukh
 
Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
 
Curso Symfony - Clase 2
Curso Symfony - Clase 2Curso Symfony - Clase 2
Curso Symfony - Clase 2
 

Semelhante a Phoenix for laravel developers

Elixir & Phoenix - fast, concurrent and explicit
Elixir & Phoenix - fast, concurrent and explicitElixir & Phoenix - fast, concurrent and explicit
Elixir & Phoenix - fast, concurrent and explicitTobias Pfeiffer
 
Working With The Symfony Admin Generator
Working With The Symfony Admin GeneratorWorking With The Symfony Admin Generator
Working With The Symfony Admin GeneratorJohn Cleveley
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
Very basic functional design patterns
Very basic functional design patternsVery basic functional design patterns
Very basic functional design patternsTomasz Kowal
 
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...dantleech
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Domenic Denicola
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksMongoDB
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk PemulaOon Arfiandwi
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONvikram mahendra
 
Incredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and GeneratorsIncredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and Generatorsdantleech
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterpriseDave Artz
 
Re-Design with Elixir/OTP
Re-Design with Elixir/OTPRe-Design with Elixir/OTP
Re-Design with Elixir/OTPMustafa TURAN
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
Echtzeitapplikationen mit Elixir und GraphQL
Echtzeitapplikationen mit Elixir und GraphQLEchtzeitapplikationen mit Elixir und GraphQL
Echtzeitapplikationen mit Elixir und GraphQLMoritz Flucht
 
The secrets of inverse brogramming
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogrammingRichie Cotton
 
The Steel industry, Elixir, PostgreSQL & file_fdw
The Steel industry, Elixir, PostgreSQL & file_fdwThe Steel industry, Elixir, PostgreSQL & file_fdw
The Steel industry, Elixir, PostgreSQL & file_fdwFlorian Kraft
 

Semelhante a Phoenix for laravel developers (20)

Magic of Ruby
Magic of RubyMagic of Ruby
Magic of Ruby
 
Elixir & Phoenix - fast, concurrent and explicit
Elixir & Phoenix - fast, concurrent and explicitElixir & Phoenix - fast, concurrent and explicit
Elixir & Phoenix - fast, concurrent and explicit
 
Working With The Symfony Admin Generator
Working With The Symfony Admin GeneratorWorking With The Symfony Admin Generator
Working With The Symfony Admin Generator
 
Tres Gemas De Ruby
Tres Gemas De RubyTres Gemas De Ruby
Tres Gemas De Ruby
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Very basic functional design patterns
Very basic functional design patternsVery basic functional design patterns
Very basic functional design patterns
 
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New Tricks
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
 
Django - sql alchemy - jquery
Django - sql alchemy - jqueryDjango - sql alchemy - jquery
Django - sql alchemy - jquery
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
 
Incredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and GeneratorsIncredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and Generators
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] Enterprise
 
Re-Design with Elixir/OTP
Re-Design with Elixir/OTPRe-Design with Elixir/OTP
Re-Design with Elixir/OTP
 
Refactoring
RefactoringRefactoring
Refactoring
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Echtzeitapplikationen mit Elixir und GraphQL
Echtzeitapplikationen mit Elixir und GraphQLEchtzeitapplikationen mit Elixir und GraphQL
Echtzeitapplikationen mit Elixir und GraphQL
 
The secrets of inverse brogramming
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogramming
 
The Steel industry, Elixir, PostgreSQL & file_fdw
The Steel industry, Elixir, PostgreSQL & file_fdwThe Steel industry, Elixir, PostgreSQL & file_fdw
The Steel industry, Elixir, PostgreSQL & file_fdw
 

Mais de Luiz Messias

Queues & Async Apps
 Queues & Async Apps Queues & Async Apps
Queues & Async AppsLuiz Messias
 
Laravel's ecosystem
Laravel's ecosystemLaravel's ecosystem
Laravel's ecosystemLuiz Messias
 
Command-Oriented Architecture
Command-Oriented ArchitectureCommand-Oriented Architecture
Command-Oriented ArchitectureLuiz Messias
 
Introduction to Elasticsearch
Introduction to ElasticsearchIntroduction to Elasticsearch
Introduction to ElasticsearchLuiz Messias
 
APIs seguras com OAuth2
APIs seguras com OAuth2APIs seguras com OAuth2
APIs seguras com OAuth2Luiz Messias
 
Google App Engine e PHP
Google App Engine e PHPGoogle App Engine e PHP
Google App Engine e PHPLuiz Messias
 

Mais de Luiz Messias (7)

Turbolinks
TurbolinksTurbolinks
Turbolinks
 
Queues & Async Apps
 Queues & Async Apps Queues & Async Apps
Queues & Async Apps
 
Laravel's ecosystem
Laravel's ecosystemLaravel's ecosystem
Laravel's ecosystem
 
Command-Oriented Architecture
Command-Oriented ArchitectureCommand-Oriented Architecture
Command-Oriented Architecture
 
Introduction to Elasticsearch
Introduction to ElasticsearchIntroduction to Elasticsearch
Introduction to Elasticsearch
 
APIs seguras com OAuth2
APIs seguras com OAuth2APIs seguras com OAuth2
APIs seguras com OAuth2
 
Google App Engine e PHP
Google App Engine e PHPGoogle App Engine e PHP
Google App Engine e PHP
 

Último

Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
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
 
+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
 
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.
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
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
 

Último (20)

Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
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 ...
 
+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...
 
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...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
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 ...
 
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
 
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
 
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
 
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
 
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 ☂️
 
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
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
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
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
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...
 

Phoenix for laravel developers

  • 1. Tony Messias Maceió DEV Meetup #21 Phoenix for Laravel developers
  • 2.
  • 3. This is not a hands-on talk.
  • 4. I'm not going to teach you Elixir (or Phoenix) in 30~ish minutes.
  • 5. I'm not saying that one is better than the other.
  • 6. In fact, I'm actually showing that you can build shiny things with tools you already know.
  • 7. This talk in a mind-map.
  • 10. Haven't the Gods of programming decided that OOP has won this battle?
  • 11. Why is functional programming in such a “hype” now?
  • 12. 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
  • 13. f(x) = x * 2 --- const f = (x) => x * 2 var x = 5 assertEquals(f(x), 10) assertEquals(f(x), f(x)) source
  • 14. 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?
  • 15. 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
  • 20. Slack'ish in Laravel tonysm/slackish-laravel Slack'ish in Phoenix tonysm/slackish-phoenix
  • 22. 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
  • 23. 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
  • 24. 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
  • 26. MVC
  • 27. Phoenix is not your application.
  • 28. 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
  • 29. 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
  • 30. 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
  • 33. Ecto
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. The not so good parts...
  • 40. 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)
  • 42. Improvement points to Laravel Broadcast::channel('App.User.{id}', function ($user, $id) { return (int) $user->id === (int) $id; }); Broadcast::channel('channels.{channel}', function (AppUser $user, AppChannel $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
  • 44. 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
  • 45. 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