65
Phoenix: Inflame the Web @troush69 | github.com/Troush

Phoenix: Inflame the Web - Alex Troush

Embed Size (px)

Citation preview

Page 1: Phoenix: Inflame the Web - Alex Troush

Phoenix: Inflame the Web

@troush69 | github.com/Troush

Page 2: Phoenix: Inflame the Web - Alex Troush

Alex

~7 Commits to Elixir Core

~1 Commit to Phoenix.HTML

~2 Commits to Ecto

3 month of production Phoenix

~ 1 year of Elixir lurking

Page 3: Phoenix: Inflame the Web - Alex Troush

Ahhahhhahah…

Page 4: Phoenix: Inflame the Web - Alex Troush

Presentation.Supervisor.start_link(self(), {:slide, 1})

Page 5: Phoenix: Inflame the Web - Alex Troush

Все любят этот слайд40core/128gb box. 2 million clients, limited by ulimit

Page 6: Phoenix: Inflame the Web - Alex Troush

Phoenix Structure

The EndpointThe RouterControllersViewsTemplates

Page 7: Phoenix: Inflame the Web - Alex Troush

mix phoenix.new kyiv_elixir

Page 8: Phoenix: Inflame the Web - Alex Troush

Mix file

Page 9: Phoenix: Inflame the Web - Alex Troush

In Elixir (actually, in Erlang/OTP), an application is a component implementing some specific functionality, that can be started and stopped as a unit, and which can be re-used in other systems.

Page 10: Phoenix: Inflame the Web - Alex Troush

KyivElixir Application

Page 11: Phoenix: Inflame the Web - Alex Troush

Endpoint● handles all aspects of

requests up until the point where the router takes over

● provides a core set of plugs to apply to all requests

● dispatches requests into a designated router

Page 12: Phoenix: Inflame the Web - Alex Troush
Page 13: Phoenix: Inflame the Web - Alex Troush

Plug Module

Page 14: Phoenix: Inflame the Web - Alex Troush

Plug Function

Page 15: Phoenix: Inflame the Web - Alex Troush

Plug Function

Page 16: Phoenix: Inflame the Web - Alex Troush

The Router● parses incoming requests and

dispatches them to the correct controller/action, passing parameters as needed

● provides helpers to generate route paths or urls to resources

● defines named pipelines through which we may pass our requests

● Pipelineso allow easy application of

groups of plugs to a set of routes

Page 17: Phoenix: Inflame the Web - Alex Troush

Controllers● provide functions, called

actions, to handle requests● Actions

o prepare data and pass it into views

o invoke rendering via views

o perform redirects

Page 18: Phoenix: Inflame the Web - Alex Troush

Complex Action

Page 19: Phoenix: Inflame the Web - Alex Troush

Pattern Match Them ALL

Page 20: Phoenix: Inflame the Web - Alex Troush

Override Action

Dispatch Function

Page 21: Phoenix: Inflame the Web - Alex Troush

Core Association

Page 22: Phoenix: Inflame the Web - Alex Troush

Let’s Talk Ecto

Page 23: Phoenix: Inflame the Web - Alex Troush

Ecto is a domain specific language for writing queries and interacting with databases in Elixir

Page 24: Phoenix: Inflame the Web - Alex Troush

Ecto Example. Controller delete action

Page 25: Phoenix: Inflame the Web - Alex Troush

Owner.ex model file

Page 26: Phoenix: Inflame the Web - Alex Troush

Views● render templates● act as a presentation

layer● define helper

functions, available in templates, to decorate data for presentation

Page 27: Phoenix: Inflame the Web - Alex Troush

Override render

function

Page 28: Phoenix: Inflame the Web - Alex Troush
Page 29: Phoenix: Inflame the Web - Alex Troush

Templates

are what they sound like :)are precompiled and fast

Page 30: Phoenix: Inflame the Web - Alex Troush

form_for

Page 31: Phoenix: Inflame the Web - Alex Troush

Current Folder Structure

Page 32: Phoenix: Inflame the Web - Alex Troush

1.3 Folder Structure

Page 33: Phoenix: Inflame the Web - Alex Troush

Your controller is not a Repo interface

Page 34: Phoenix: Inflame the Web - Alex Troush

Your controller is not a Repo interface

Page 35: Phoenix: Inflame the Web - Alex Troush

Your controller is not a Repo interface

Page 36: Phoenix: Inflame the Web - Alex Troush

Umbrella Apps

Page 37: Phoenix: Inflame the Web - Alex Troush

Phoenix is not your App

Page 38: Phoenix: Inflame the Web - Alex Troush

New Umbrella App

Page 39: Phoenix: Inflame the Web - Alex Troush
Page 40: Phoenix: Inflame the Web - Alex Troush
Page 41: Phoenix: Inflame the Web - Alex Troush

# issue/lib/issue/ticket.ex

Page 42: Phoenix: Inflame the Web - Alex Troush

$ cd apps/

$ mix phoenix.new issue_web

Page 43: Phoenix: Inflame the Web - Alex Troush
Page 44: Phoenix: Inflame the Web - Alex Troush

Don’t write large apps

Page 45: Phoenix: Inflame the Web - Alex Troush

Application is your microservice

Page 46: Phoenix: Inflame the Web - Alex Troush

Channels

Page 47: Phoenix: Inflame the Web - Alex Troush

Phoenix.Transports.*

Page 48: Phoenix: Inflame the Web - Alex Troush

Phoenix.Socket.Transport behaviour

Implementing the transport behaviour

Establishing the socket connection

Handling of incoming messages

Handling of outgoing messages

Managing channels

Providing secure defaults

Page 49: Phoenix: Inflame the Web - Alex Troush

Phoenix.Transports

The transport allows to abstract away how upper layer i.e. channels receives or sends messages. Channels only need to call the right function of transport module and let it handle the raw work of communication.

Page 50: Phoenix: Inflame the Web - Alex Troush

Channel

Channel has two components, one is the channel behavior implemented by the developer and the second is the channel GenServer that is spawned up by phoenix that consumes the developer written module.

Communicating with the transport (receiving from & sending to transport)

Listening for requests to send a message that was broadcasted

Page 51: Phoenix: Inflame the Web - Alex Troush
Page 52: Phoenix: Inflame the Web - Alex Troush

$ mix phoenix.gen.html Issue issues name:string description:string$ mix phoenix.gen.channel Issue

Page 53: Phoenix: Inflame the Web - Alex Troush
Page 54: Phoenix: Inflame the Web - Alex Troush
Page 55: Phoenix: Inflame the Web - Alex Troush

#channels/issue_channel.ex

Page 56: Phoenix: Inflame the Web - Alex Troush
Page 57: Phoenix: Inflame the Web - Alex Troush

Phoenix.Channel Callbacks

handle_in(event, msg, arg2)

handle_out(term, arg1)

join(topic, auth_msg, arg2)

terminate(msg, arg1)

Page 58: Phoenix: Inflame the Web - Alex Troush

join(topic, auth_msg, socket)

To authorize a socket in join/3, return {:ok, socket}.

To refuse authorization in join/3, return {:error, reply}.

Page 59: Phoenix: Inflame the Web - Alex Troush

handle_in(event, payload, socket)After a client has successfully joined a channel, incoming events from the client are routed through the channel’s handle_in/3 callbacks

Page 60: Phoenix: Inflame the Web - Alex Troush

Replay

In addition to pushing messages out when you receive a handle_in event, you can also reply directly to a client event for request/response style messaging

Page 61: Phoenix: Inflame the Web - Alex Troush

Intercept & handle_out/3

When an event is broadcasted with broadcast/3, each channel subscriber can choose to intercept the event and have their handle_out/3 callback triggered

intercept ["new_msg", "user_joined"]

So on “new_msg” and “user_joined” topics handle_out will be triggered

Page 62: Phoenix: Inflame the Web - Alex Troush

handle_out(event, payload, socket)

Page 63: Phoenix: Inflame the Web - Alex Troush

Terminate

On termination, the channel callback terminate/2 will be invoked with the error reason and the socket.

If we are terminating because the client left, the reason will be {:shutdown, :left}. Similarly, if we are terminating because the client connection was closed, the reason will be {:shutdown, :closed}.

If any of the callbacks return a :stop tuple, it will also trigger terminate with the reason given in the tuple.

Page 64: Phoenix: Inflame the Web - Alex Troush

Conclusion

R.I.Pweb/

models/

Channelsare cool

simple

handle_in / handle_out

broadcast

Page 65: Phoenix: Inflame the Web - Alex Troush

Questions?