Building an app with elixir-1

Nehal Hasnayeen
Brew Some Elixir
Published in
3 min readApr 30, 2017

Next post, Source code

Disclaimer: I’m a complete noob in elixir, so this is not an ideal way of doing things in elixir. I’m just writing my experience so that anyone new can start experimenting quickly with elixir and don’t get frustrated.

I started learning elixir recently and I thought I’d make a record log of my progress, so I’ll write about everything I’m doing in the process. I’ve already checked out some basic, now I’m going to build a simple app with elixir to learn more about it. The idea is to build a app to get language trend from twitter’s overall tweets. I’ve no idea how to build this thing but I’ll try to figure it out on the way. Also, I was planing to learn about rethinkdb for quite some time but never started, so I decided to use rethinkdb in this project for data storage to have a taste of it (I don’t even know is it needed at all ¯\_(ツ)_/¯). So in this post I’ll write about setting up the project.

Step-1: Install elixir and rethinkdb

First we need elixir runtime to run elixir program. But now a days I use docker, so I’m not gonna install elixir on my host machine rather I’m going to use docker official elixir image to run my application in docker container. I’m not gonna install rethinkdb either rather I will use docker official rethinkdb image. We need to persist our data from rethinkdb so first we’ll create a docker volume

$ docker volume create rethinkdb-data-volume

Next create a rethinkdb container and attached the volume

$ docker run -name elixir-db -v “rethinkdb-data-volume:/data” -d rethinkdb

Next create a elixir container in a directory on your host machine where you’ll keep your source code and attach the rethinkdb container

$ docker run -it -name elixir-app -v /path/to/projects/dir:/src — link elixir-db:rdb -d elixir

And finally let’s enter into container’s bash terminal

$ docker exec -it elixir-app /bin/bash

we’ll be at root `/` directory of our system and our source code directory is copied into `src` folder

$ cd src/

Step-2: Create project

Mix is a build tools for elixir just like npm of nodejs. We will use it to `install` our dependencies, `create` our project, `compile` our application and many other things. Lets create a project skeleton first

$ mix new language_trends

this will create a directory name language_trends (only letter, numbers and underscore allowed). If we enter the folder we’ll see following files

* README.md
* .gitignore
* mix.exs
* config
* config/config.exs
* lib
* lib/language_trends.ex
* test
* test/test_helper.exs
* test/language_trends_test.exs

`mix.exs` is like package.json of npm where we define our meta information of the project and required dependency. For more info on `mix.exs` read this. Our source code will live inside `lib` directory where there is already a file named `language_trends.ex`.

Note: `.ex` file is compiled while `.exs` is for scripting purpose

Now we will include rethinkdb client for elixir. Add {:rethinkdb, github: “hamiltop/rethinkdb-elixir”} in `dep` section in `mix.exs` file

 defp deps do
[
{:rethinkdb, github: “hamiltop/rethinkdb-elixir”}
]
end

now run below command to install the dependency

$ mix deps.get

now compile the dependent libraries

$ mix deps.compile

and finally compile our source code

$ mix compile

this’ll compile our source code and place all the compiled file in `_build` directory. After compiling we can start an `iex` session inside the project by running

$ iex -S mix

Note: `iex` is an repl (read, eval, print, loop) for elixir

Inside `iex` our app is loaded so we can run our function from the app

iex(1)> LanguageTrends.hello()
:world

we can see the atom `:world` is returned by our function.

That’s it for today, we have setup our environment to write our code for the application. In next post we’ll start building our app. Happy Coding!!

You can find the source code here.

Let me know about your thoughts in comments below.

--

--