Phoenix Tutorial for Beginners
What is Phoenix?
Phoenix is an extremely productive web development framework written in Elixir (a functional programming language). It implements the server-side Model View Controller (MVC) pattern. Many of its concepts and components are familiar to those with other web frameworks like Ruby on Rails or Python's Django. It is used to develop low-latency, fault-tolerant, distributed systems, which are necessary qualities of modern web applications. Phoenix provides high developer productivity, scalability, and top application performance web applications. It also has some exciting new twists like channels for implementing real-time features and pre-compiled templates for blazing speed. Phoenix is based on the Plug library and the Cowboy Erlang framework. Likewise, the request/response functionality provided by the underlying Cowboy server, Phoenix also offers soft real-time communication for the external clients through web sockets or long polling by using its language agnostic features.Phoenix Parts
Phoenix Framework consists of various parts, where each part has its purpose and role to play in developing a web application. Let’s cover each every part in depth throughout these guides. Given below is the quick breakdown of the parts:- Endpoint
- Endpoint refers to the start and end of the request lifecycle in an application.
- It handles all aspects of requests until there comes the point where the router takes over the requests.
- Endpoint provides a core set of plugs to apply to all requests.
- It dispatches all the requests into a designated router.
- Router
- A router parses the incoming requests and then, dispatches them to the correct controller or action, passing the parameters as needed.
- It assists the developers to generate route paths or URLs for resources.
- Router defines named pipelines through which we may pass our requests.
- The Router Pipelines allow easy application of groups of plugs to a set of routes.
- Controllers
- Controllers provide functions which are known as actions, to handle the incoming requests.
- The actions are used to prepare data and pass it into views, helps in invoke rendering via views and perform redirects.
- Views
- Views are used for rendering the templates.
- It acts as a presentation layer.
- Views define helper functions which are available in templates, to decorate data for presentation.
- Templates
- Templates are fast and precompiled files which contains the contents that are served in response.
- It provides the basic structure for a response, and allows dynamic data to be substituted in.
- Channels
- Channels are analogous to controllers except that they allow bi-directional communication with persistent connections.
- Channels are used to manage sockets for easy real-time communication.
- PubSub
- PubSubunderlies the channel layer and allows clients to subscribe to topics.
- It abstracts the underlying pubsub adapter for third-party integration.
Difference between Phoenix and Ruby on Rails
Phoenix |
Ruby on Rails |
Phoenix is an extremely productive web development framework written in a functional programming language Elixir. | Ruby on Rails is a web application framework written in Ruby. |
In Phoenix, the routing is done with what are referred to as “plugs.” | In Ruby on Rails, the routing is done through middleware. |
Phoenix would be done with the Plug.Logger plug. | The middleware Rails::Rack::Logger which logs a request in Rails. |
A View in Phoenix is in charge of rendering templates and providing functions that make the raw data easier for the templates to use. | A view in Phoenix resembles a helper in Ruby on Rails, with the addition of rendering the template. |
In Phoenix, the model does not handle the persistence to the database. | In Ruby, the schema in the model is needed because the fields are not automatically loaded into the model like in Ruby on Rails. |
In Phoenix, the developers explicitly do the validations and transformations using change sets. | In Ruby on Rails, validating and transforming data can be the source of hard to find bugs. |
Features of Phoenix
Phoenix is gaining its popularity because of its salient features. This framework is about building fast, concurrent, beautiful, interactive, and reliable applications. Let’s break each of these points:- Fast
- Precompiled Templates
- Concurrent
- Beautiful Code
- Effortlessly Extensible
- Interactive
- Reliable
Phoenix Installation
For installing Phoenix, you will need to install the Hex package manager as well. Hex Package is necessary to get a Phoenix app running and to install extra dependencies one might need along the way. See the below command to install Hex (If you have Hex already installed, it will upgrade Hex to the latest version):$ mix local.hexFollowing is the command to install the Phoenix archive:
$ mix archive.install hex phx_new 1.4.6