Web development with Elixir

I created a small hobby project, an online comic book reader, using the fairly new programming language Elixir and a framework called Phoenix which I knew nothing about. These are my experiences and thoughts from that journey.

Elixir

I found myself liking Elixir a lot right form the start. It is a dynamic functional language which runs on the Erlang VM and is often called a breed between Ruby and Erlang – hold on, they are mixed in the good way. I feel like I’m in a point in my life when I tend to like getting away from object-oriented programming and I feel that Elixir makes this a really enjoyable experience. It feels like you can learn the basis of Elixir very fast and that the basic constructs of the language are beautiful and simple.

As an example, Elixir uses pattern matching in a lot of places which enables easy deconstruction of data. Actually, assignment in Elixir (x = “hello”) is actually pattern matching. As a concrete example, the function File.copy will return an :ok or :error atom depending on if the operation fails or not. Because of this, you can easily pattern match on this operation and avoid handling exceptions (however, exceptions are available if you fancy it).

The pipe operator “|>” is another big thing in Elixir. It takes the result from a previous computation and feeds it into the first argument of a function. So you can do things like:

This is not only synthetic sugar but it makes you think about your problem in a neat way. You say “I have this list, first I want to filter out some things, then I want to transform the items with map, then …” and then do just that.

There are lots of other nice details like immutability without single assignment. To pick two from the getting started guide, I would point to Protocols and Processes for something to look at.

Phoenix & Ecosystem

When starting with Elixir, I Googled around for different web frameworks. I can’t really argue exactly why I picked Phoenix but it turns out to have been an excellent choice (it should be noted that Phoenix is still in development and has not reached a 1.0 release yet). What I found more interesting than the framework itself is how Elixir ties everything together with all of its “standardised” components.

Plug

Part of the Elixir project is Plug. It is “a specification for composable modules between web applications and a connection adapter for different web servers in the Erlang VM” (I’ve heard it’s like Ring from Clojure) and it is used in Phoenix. This means that you can compose Plugs to do everything from parsing request bodies and session management to create your own Plugs for custom authentication/authorisation – or anything you like. I believe that having this standardised is a great benefit for everyone since much of the code can be re-used if for instance Phoenix would cease to exist.

Ecto

Ecto is a domain specific language for writing queries and interacting with databases. I used it with PostgreSQL, which was a first for me, and everything worked without any problems at all. At first I thought Ecto was some kind of ORM (the irony of Elixir not having objects) but it is not. You do not write plain SQL code but instead use a DSL, as mentioned before. This did freak me out a little (I’m a bit of an SQL fanatic) but it has actually turned out to be a very nice experience, at least for my small hobby project.

IEx

IEx is the interactive Elixir shell. I’m sure you’re all familiar with REPLs but I found IEx to be particularly usable since you can use it to hook it in to your Phoenix project. This means that in your REPL, you can access everything in the project like inspecting routes and interacting with your controllers.

EEx

EEx is used as the templating system in Phoenix. It is part of Elixir itself and provides yet another standardised component. Nothing fancy you haven’t seen before but does the job, and again, it is nice to have a standardised way of doing this.

Mix

Mix is the official build tool that provides tasks for creating, compiling, testing Elixir projects, as well as handle dependencies, and more. In Phoenix you use it to build your project, start the web server, manage all your dependencies from Hex or GitHub and even tell Ecto to perform database migrations – in essence, you do everything with it.

Documentation

The getting started for Elixir is short and sweet. I actually read all of its chapters from start to end which I almost never do. It is short enough that you can plough through it with ease and concise enough to cover all the basis and give you a hint how everything works. A colleague to me recommended the book Programming Elixir: Functional |> Concurrent |> Pragmatic |> Fun for a more in-depth look. I haven’t used the actual API documentation that much, but it looks well written for everything in Elixir and its eco system as far as I can see.

When it comes to Phoenix on the other hand, it is still a bit sparse. I know this has been addressed and that they won’t call a release 1.0 until they have proper documentation. For now, you have to live with its GitHub page and the work-in-progress Phoenix guides – it will take you a long way but you might get stuck.

Community
From what I’ve heard, the community is friendly and helpful. I’ve only interacted with it very little. I’ve asked for help in the Elixir IRC-channel, used the Phoenix mailing list and submitted a bug report on GitHub and all of it has been handled quickly and pleasantly.

Getting started
Besides all links mentioned above, I think this tutorial is a great start, but be warned that Phoenix has changed some since the writing of it so you may have to change a couple of things while following it. If you want to check out my project, you can find it on GitHub.

As a conclusion, I would recommend everyone to give Elixir a look – or as Kenta would say: “try it – you’ll like it”.

6 Comments

  1. Christian Melgarejo

    Wow, Ecto is actually sort of a LINQ-esque for Elixir, now I’m even more engaged to try this :)
    Quick question, with Ecto is there a way to execute directly my own defined functions on the database without having to use execute, as in with a defined module, have you tried that?
    Thanks!

  2. Gordon Illan

    Is Phoenix the web server or does it work with YAWS?

    • Anton Fagerberg

      Phoenix is not the server. Phoenix uses Plug and Plug should be able to work with any web server (in theory). However, Plug needs some adapter code for each web server and as far as I know, Cowboy is the only one supported.

      So I’m guessing the answer is no if there isn’t any community developed adapter code somewhere.

  3. Couple of months ago I built some basic application using Phoenix and I really enjoyed it.. What holds me back is the relative lack of 3rd party libraries as can be found for Rails. For instance, I learned that there is currently no plugin that can take care of data filtering (filtering data based on user choices). If I need this, then I will have to write it on my own. What amazed me most is speed and the high req/seconds that Phoenix can deliver.
    I must ask you, what do you think today about Elixir / Phoenix? Do you believe that this is the way to go for future and even large scale projects? Thank you.

    • Anton Fagerberg

      I haven’t really used Phoenix since I wrote this post but you’re right that there’s much less third-party support when compared to things like Rails. Elixir (and Phoenix) are still very young projects.

      Regarding the future, I can imagine that Elixir / Phoenix could be the way to go. Perhaps even replace Ruby on Rails one day, but only time will tell. It’s definitely a couple of years down the line. Performance wise I’m confident that it will be able to scale very well on large projects. It’s more about user adaptation and third-party support and it’s impossible to tell whether that will happen or not :)

Leave a Reply