Generators make the whole thing look SEXY !

Tworit Kumar Dash
Zairza
Published in
4 min readJul 12, 2016

This blog post is about the programming language Elixir and about its framework called Phoenix. Don’t just read it. Try it as well. Elixir is a dynamic, fault tolerant functional language. It runs on Erlang VM. It runs inside processes (Surprisingly not OS processes !). Those processes are isolated. Dividing the whole thing into processes is called light weight threading here (Not Threading). It runs inside processes which make it much faster. WhatsApp uses this technology and surprisingly it handles only 2 million requests concurrently.

Pretty asynchronous and fast ! It has observer methods to check all the processes running. Phoenix is a web framework written in Elixir. It is the best of both worlds. Both worlds include a pretty sexy MVC architecture for storing, editing, updating and deleting database records as well as an awesome channel feature for real-time communications. It has got a lot of generators which actually make life easy. The generators with mix tasks from Phoenix are similar to the generators used in ruby on rails. Let’s write a simple CRUD application using Phoenix in just 10 minutes.

Before that let’s have a look at the pattern matching feature from Elixir. below is a program for Fibonacci Series.

Here, the fibo method has 2 declarations. Both are fibo/3 (They accept 3 arguments each), but one of them only works if the argument “index” is less than or equal to 1. It is a new approach of pattern matching and it’s different from traditional function overriding recipes. The fibo/3 with index less than or equals to 1 runs at the last step. Before that, the normal fibo/3 is run by default. The code is pretty readable and understandable.

Now, let’s create a demo application with the Phoenix framework and enter into it. First we have to run

$ mix phoenix.new demo

“demo” is the name of the application. The output to this command in the terminal is given in the following text file.

Then we enter into the project repository.

$ cd demo

Now we will create a tweet application, where we can make a new tweet, edit it, delete it and show it on another page with details. Phoenix has got pretty good generators to do it. Let’s first run the server.

$ mix phoenix.server

Or you can run the server inside the elixir interpreter with a mix task (A new cool feature !). Unlike having a separate terminal window to run rails console in ruby on rails, it can run both at one place.

$ iex -S mix phoenix.server

It will run the server and we can view the webpages at http://localhost:4000/ . Now, we need a database for this development project and fill it with a tweet table with a body for the tweet. And, after that we need to save the database changes and the routes for url endpoints. Pretty easy. Let’s have the magic. Now we will generate everything with a single command. It will generate a model (the medium of interaction from the tweet table from the database ), a controller and a view with HTML pages.

$ mix phoenix.gen.html Tweet tweets body:string

It will create a model with Tweet as a table and body as one of its attributes. Then let’s add all the resources for tweet in the routes. Edit the routes file by adding resources for tweets. Resources add routes for the entire CRUD. You can see the routes by the following command.

$ mix phoenix.routes

Now, let’s update our database.

$ mix ecto.create $ mix ecto.migrate

The first one will create a database called demo_dev and the second one will migrate the changes as per the tweet model’s schema.

Now we are done. The tweet CRUD is live at http://localhost:4000/tweets . We don’t need to restart the server as internally phoenix does it for us. A few screenshots are given below. Now you can see it provides us with everything.

New tweet link
Creating a tweet.
Tweet list with show, update and delete links !

It’s pretty simple and fast. Who says “Programming a computer is difficult” ? It’s fun and easy. Happy Hacking. Try Phoenix. Good Bye !

--

--

Tworit Kumar Dash
Zairza
Editor for

Ph.D. Student Researcher, Geek, Rubyist, Hardware and OpenSource enthusiast, Blogger, Physics Freak