Getting comfortable with erlang, cowboy?

Akshat Jiwan Sharma, Wed Nov 06 2013

I have been able to grasp the fundamentals of erlang programming quickly. Pattern matching, message passing and other basics of the language felt natural to me even though I had never programmed in a functional language before. So with bubbling enthusiasm I decided to write my web socket application in erlang. Well the use case was pretty good. I needed message passing and concurrency and erlang gives those two for free but I underestimated the effort that it would take to learn OTP.

My previous experience with frameworks has been quite good. I did not have much trouble learning Asp.net MVC or express after getting a hang of c# and javascript. OTP however was quite a challenge and even now I don't feel fluent in it. The main problem was that I was trying to learn too much at the same time. I switched my editor to emacs, fiddled around with rebar for dependency management and building, tired to learn relex for creating releases while I still hadn't written a hello world in cowboy. Any one who is new to web development in erlang will probably use these tools for their application and this post might help flatten learning curve a little.

The editor

You can use any text editor to program in erlang but emacs is the recommended one. After installing emacs on your machine open up the terminal and type emacs .emacs

Now copy the code from the erlang emacs manual in the file that pops up and save it. That's it you are ready for erlang development.

This was the easy part though. Emacs comes with it's own key bindings that may seem strange if you are used to modern editors like sublime text or brackets. Working with emacs becomes a lot simpler once you learn how to read the bindings. Consider a simple example C-x C-s which saves the current file you are working on.

This seems cryptic at first. Here is how you read it. Control+x followed by Control + s

M-x is read as alt+x

Setting up emacs on erlang gives you a lot of useful features like syntax highlighting, indenting code(M-C-\) and compilation(C-c C-k). For a full reference of what can be done with emacs check out this [post] (http://www.erlang.org/doc/man/erlang.el.html). Granted it may take a while to get used to it but development in erlang will be a lot more fun once you learn how to use it.

The file structure

OTP applications follow a predefined file structure. There will be at least two folders in your project directory. src and ebin. Besides this the root folder will also have configuration files for relex and rebar . The src folder contains the source files viz *.app, *_app.erl,*_sup.erl

Developing otp applications involves editing the files in the src folder compiling them and then generating a release.

Building application

Rebar takes care of building and compiling src files. For this purpose it needs a config file. A very basic config file looks like

{deps, [
    {cowboy, ".*", {git, "https://github.com/extend/cowboy.git",{branch, "master"}}},

{'lager', ".*", {  
        git, "git://github.com/basho/lager.git", "master"}  
    }  
]}.

What this does is tell rebar of dependencies of the application. In the config file above there are two dependencies cowboy and lager .So when the first time you complie your app you can type rebar get-deps compile in the terminal and it will fetch all the dependencies and generate beam files in the ebin folder. For all subsequent compilations you can use rebar compile.

Rebar does more than compilation though. For each build it can run unit tests as well. However that is a topic for another post and for now we just want the basics.

Releasing applications

The second step is generating a release of the application the you have compiled. A release is simply your application that is packaged to run on your server or another machine. It is self contained so all you need to do is copy the _rel folder and you are good to go. Relx helps in creating releases. Like rebar it has a config file. A simple relex configuration looks like

{release, {app, "1"}, [app]}.
{extended_start_script, true}.

The config is pretty straightforward the first tuple contains the atom release another tuple containing app name and a version number followed by a list containing name of applications to be released.

extended_start_script instructs relex to create a start script in the release folder.

After creating relex.config in the root just run relex from your terminal and you should have a release in your _rel folder

Starting the application.

After building and generating a release I was quite confused about what to do next. I did not know how to start the application. It turns out that the _rel folder has the start script in the bin sub folder. So if you app name is say hello then this should do the trick

_rel/bin/app console

console command simply allows all the status messages from app to be printed on the terminal.

This basic tutorial should be enough for getting your OTP applications to run. I have not covered the use of cowboy framework here intentionally. It is quite a lenghty topic and in subsequent posts I will cover it in more detail as I get to know it better.




comments powered by Disqus