phoenix: inflame the web - alex troush
TRANSCRIPT
![Page 1: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/1.jpg)
Phoenix: Inflame the Web
@troush69 | github.com/Troush
![Page 2: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/3.jpg)
Ahhahhhahah…
![Page 4: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/4.jpg)
Presentation.Supervisor.start_link(self(), {:slide, 1})
![Page 5: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/5.jpg)
Все любят этот слайд40core/128gb box. 2 million clients, limited by ulimit
![Page 6: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/6.jpg)
Phoenix Structure
The EndpointThe RouterControllersViewsTemplates
![Page 7: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/7.jpg)
mix phoenix.new kyiv_elixir
![Page 8: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/8.jpg)
Mix file
![Page 9: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/10.jpg)
KyivElixir Application
![Page 11: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/12.jpg)
![Page 13: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/13.jpg)
Plug Module
![Page 14: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/14.jpg)
Plug Function
![Page 15: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/15.jpg)
Plug Function
![Page 16: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/18.jpg)
Complex Action
![Page 19: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/19.jpg)
Pattern Match Them ALL
![Page 20: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/20.jpg)
Override Action
Dispatch Function
![Page 21: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/21.jpg)
Core Association
![Page 22: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/22.jpg)
Let’s Talk Ecto
![Page 23: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/23.jpg)
Ecto is a domain specific language for writing queries and interacting with databases in Elixir
![Page 24: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/24.jpg)
Ecto Example. Controller delete action
![Page 25: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/25.jpg)
Owner.ex model file
![Page 26: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/27.jpg)
Override render
function
![Page 28: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/28.jpg)
![Page 29: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/29.jpg)
Templates
are what they sound like :)are precompiled and fast
![Page 30: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/30.jpg)
form_for
![Page 31: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/31.jpg)
Current Folder Structure
![Page 32: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/32.jpg)
1.3 Folder Structure
![Page 33: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/33.jpg)
Your controller is not a Repo interface
![Page 34: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/34.jpg)
Your controller is not a Repo interface
![Page 35: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/35.jpg)
Your controller is not a Repo interface
![Page 36: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/36.jpg)
Umbrella Apps
![Page 37: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/37.jpg)
Phoenix is not your App
![Page 38: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/38.jpg)
New Umbrella App
![Page 39: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/39.jpg)
![Page 40: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/40.jpg)
![Page 41: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/41.jpg)
# issue/lib/issue/ticket.ex
![Page 42: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/42.jpg)
$ cd apps/
$ mix phoenix.new issue_web
![Page 43: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/43.jpg)
![Page 44: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/44.jpg)
Don’t write large apps
![Page 45: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/45.jpg)
Application is your microservice
![Page 46: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/46.jpg)
Channels
![Page 47: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/47.jpg)
Phoenix.Transports.*
![Page 48: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/48.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/49.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/50.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/51.jpg)
![Page 52: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/52.jpg)
$ mix phoenix.gen.html Issue issues name:string description:string$ mix phoenix.gen.channel Issue
![Page 53: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/53.jpg)
![Page 54: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/54.jpg)
![Page 55: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/55.jpg)
#channels/issue_channel.ex
![Page 56: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/56.jpg)
![Page 57: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/57.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/58.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/59.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/60.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/61.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/62.jpg)
handle_out(event, payload, socket)
![Page 63: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/63.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/64.jpg)
Conclusion
R.I.Pweb/
models/
Channelsare cool
simple
handle_in / handle_out
broadcast
![Page 65: Phoenix: Inflame the Web - Alex Troush](https://reader033.vdocuments.us/reader033/viewer/2022052514/58e49d7c1a28aba3458b55c5/html5/thumbnails/65.jpg)
Questions?