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


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:

  1. Fast

Phoenix is the most rapid framework in the benchmark and is the most consistent among other frameworks. Its performance is getting better, and the performance is in the same ballpark with the lower-level Plug.

  1. Precompiled Templates

Phoenix doesn’t copy the strings for each rendered template. At the hardware level, you’ll see caching come into play for these strings where it never did before. It provides precompiled template for fast coding.

  1. Concurrent

The Elixir programming model makes reasoning about parallel systems almost as easy as reasoning about single-threaded ones. In aggregate, your code will spend less time waiting and more time working. Phoenix has become so popular among people because the concurrency is so easy in this framework.

  1. Beautiful Code

In Phoenix, you won’t have to read through dozens of skip_before_filter commands to know how your code works. You’ll build a pipeline for each group of routes that work the same way. Thus, allowing efficient and easy coding.

  1. Effortlessly Extensible

The Phoenix framework gives you the right set of abstractions for the extension. Rather than rely on other mechanisms like an inheritance that hide intentions, you’ll roll up your functions into explicit lists called pipelines, where each function feeds into the next.

  1. Interactive

Phoenix helps to build interactive applications. It provides lightweight processes and lightweight connections, which helps to create hundreds of thousands of operations without breaking a sweat.

  1. Reliable

Phoenix has set up most of the supervision structure for the developers. For example, earlier if you want to talk to the database, you need to keep a pool of database connections, but Phoenix has made this easier by providing one out of the box for you.

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):

Following is the command to install the Phoenix archive:

Phoenix Layers

Multiple layers are used to build a Framework. Even Phoenix web Framework is the top layer of a multi-layered system which is designed to be modular and flexible. The other layers of Phoenix include Cowboy, Plug, and Ecto.

Let’s have a better understanding of each layer:


By default, the web server used by Phoenix is Cowboy. It is a small, fast and modular HTTP server written in Erlang. It is rare when we directly interface with Cowboy directly while using Phoenix.


Plugs are some specification which is used for constructing compose-able modules to build web applications. They are reusable modules and aid to build functions to that specification. Plugs provide discrete behaviors, unlike logging or request header parsing. Because the Plug API is too small and consistent, it can be determined and executed in a set order, unlike a pipeline. Plugs can also be re-used within a project or across projects.

Plugs are written to handle almost anything, from authentication to parameter pre-processing, and even rendering.

Phoenix takes abundant advantage of Plug, i.e., the router and controllers especially.

One of the essential things about Plug is that it provides adapters to HTTP servers which will deliver application content to the end users.


Ecto is a language used as an integrated query composition tool and database wrapper for Elixir. With Ecto, one can read and write to different databases, can model the writing complex queries in a type-safe way, domain data, protect against attack vectors i.e. including SQL injection, etc.