SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
%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
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
%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
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%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
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 

Kürzlich hochgeladen (20)

Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%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
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%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
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%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
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 

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